description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
n1, n2 = nums1, nums2
s, t, sum1, sum2 = len(n1) - 1, len(n2) - 1, 0, 0
li = []
ans = 0
while s >= 0 and t >= 0:
if n1[s] > n2[t]:
sum1 += n1[s]
s -= 1
elif n1[s] < n2[t]:
sum2 += n2[t]
t -= 1
else:
ans += max(sum1 + n1[s], sum2 + n1[s])
sum1 = sum2 = 0
s -= 1
t -= 1
if s >= 0:
ans += max(sum1 + sum(n1[: s + 1]), sum2)
if t >= 0:
ans += max(sum1, sum2 + sum(n2[: t + 1]))
return ans % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
KMAX = 10**9 + 7
i1, i2 = 0, 0
N1, N2 = len(nums1), len(nums2)
res = 0
total1, total2 = 0, 0
while i1 < N1 or i2 < N2:
if i1 == N1:
total2 += nums2[i2]
i2 += 1
continue
if i2 == N2:
total1 += nums1[i1]
i1 += 1
continue
if nums1[i1] == nums2[i2]:
res += nums1[i1] + max(total1, total2)
i1 += 1
i2 += 1
total1, total2 = 0, 0
elif nums1[i1] < nums2[i2]:
total1 += nums1[i1]
i1 += 1
else:
total2 += nums2[i2]
i2 += 1
res += max(total1, total2)
return res % KMAX
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, A: List[int], B: List[int]) -> int:
maxi = max(A[-1], B[-1])
A.append(maxi + 1)
B.append(maxi + 1)
i = j = 0
s1 = s2 = 0
MOD = 10**9 + 7
while i < len(A) and j < len(B):
if A[i] < B[j]:
s1 += A[i]
i += 1
elif A[i] > B[j]:
s2 += B[j]
j += 1
else:
s1 = s2 = (max(s1, s2) + A[i]) % MOD
i += 1
j += 1
return (MOD + s1 - A[-1]) % MOD
def maxSum_foolish_DP(self, A: List[int], B: List[int]) -> int:
N = [A, B]
NN = [{v: i for i, v in enumerate(A)}, {v: i for i, v in enumerate(B)}]
@functools.lru_cache(None)
def helper(index, is_bottom, changed):
nonlocal N, NN
if index < 0:
return 0
value = N[is_bottom][index]
if changed:
if value not in NN[1 ^ is_bottom]:
return float("-inf")
return helper(NN[1 ^ is_bottom][value], 1 ^ is_bottom, 0)
else:
return value + max(
helper(index - 1, is_bottom, 0), helper(index - 1, is_bottom, 1)
)
return max(
helper(len(A) - 1, 0, 0),
helper(len(A) - 1, 0, 1),
helper(len(B) - 1, 1, 0),
helper(len(B) - 1, 1, 1),
) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR IF VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
H1 = {}
H2 = {}
for i in range(len(nums1)):
H1[nums1[i]] = i
for i in range(len(nums2)):
H2[nums2[i]] = i
S1 = set(H1.keys())
S2 = set(H2.keys())
C = sorted(list(S1.intersection(S2)))
if len(C) == 0:
return max(sum(nums1), sum(nums2))
ind1 = ind2 = 0
n1 = []
n2 = []
n = 0
for i in C:
i1 = H1[i] + 1
i2 = H2[i] + 1
n += max(sum(nums1[ind1:i1]), sum(nums2[ind2:i2]))
n1.append(sum(nums1[ind1 : H1[i] + 1]))
n2.append(sum(nums2[ind2 : H2[i] + 1]))
ind1 = H1[i] + 1
ind2 = H2[i] + 1
n1.append(sum(nums1[ind1:]))
n2.append(sum(nums2[ind2:]))
n += max(sum(nums1[ind1:]), sum(nums2[ind2:]))
return n % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
changeSum = 0
partial1, partial2 = 0, 0
i, j = 0, 0
while i < len(nums1) and j < len(nums2):
if nums1[i] == nums2[j]:
print((partial1, partial2))
changeSum = (changeSum + nums1[i] + max(partial1, partial2)) % (
10**9 + 7
)
i += 1
j += 1
partial1, partial2 = 0, 0
elif nums1[i] < nums2[j]:
partial1 += nums1[i]
i += 1
else:
partial2 += nums2[j]
j += 1
while i < len(nums1):
partial1 += nums1[i]
i += 1
while j < len(nums2):
partial2 += nums2[j]
j += 1
changeSum = (changeSum + max(partial1, partial2)) % (10**9 + 7)
return changeSum % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
matrix = []
matrix.append(nums1[:])
matrix.append(nums2[:])
dics = []
dics.append({num: i for i, num in enumerate(nums1)})
dics.append({num: i for i, num in enumerate(nums2)})
sys.setrecursionlimit(10**6)
@lru_cache(None)
def dfs(row, index):
if index >= len(matrix[row]):
return 0
cur = matrix[row][index]
res = cur + dfs(row, index + 1)
if cur in dics[1 - row]:
res = max(res, cur + dfs(1 - row, dics[1 - row][cur] + 1))
return res
mod = 10**9 + 7
return max(dfs(0, 0), dfs(1, 0)) % mod
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
n1 = len(nums1)
n2 = len(nums2)
M = 1000000007
dp1 = [0] * (n1 + 1)
dp2 = [0] * (n2 + 1)
i1 = i2 = 0
while i1 < n1 and i2 < n2:
if nums1[i1] < nums2[i2]:
dp1[i1 + 1] = dp1[i1] + nums1[i1]
i1 += 1
elif nums1[i1] > nums2[i2]:
dp2[i2 + 1] = dp2[i2] + nums2[i2]
i2 += 1
else:
dp1[i1 + 1] = dp2[i2 + 1] = max(
dp1[i1] + nums1[i1], dp2[i2] + nums2[i2]
)
i1 += 1
i2 += 1
while i1 < n1:
dp1[i1 + 1] = dp1[i1] + nums1[i1]
i1 += 1
while i2 < n2:
dp2[i2 + 1] = dp2[i2] + nums2[i2]
i2 += 1
return max(dp1[n1], dp2[n2]) % M
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
d1 = {}
d2 = {}
t1 = {}
t2 = {}
l1 = len(nums1)
l2 = len(nums2)
for i in range(l1):
d1[nums1[i]] = i
for i in range(l2):
d2[nums2[i]] = i
def runList1(i):
if i == l1:
return 0
if i in t1:
return t1[i]
maxList2 = 0
if i + 1 < l1 and nums1[i + 1] in d2:
maxList2 = runList2(d2[nums1[i + 1]])
result = max(runList1(i + 1), maxList2) + nums1[i]
t1[i] = result
return result
def runList2(i):
if i == l2:
return 0
if i in t2:
return t2[i]
maxList1 = 0
if i + 1 < l2 and nums2[i + 1] in d1:
maxList1 = runList1(d1[nums2[i + 1]])
result = max(runList2(i + 1), maxList1) + nums2[i]
t2[i] = result
return result
value = max(runList1(0), runList2(0))
return value % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
dp = collections.defaultdict(int)
i, j = len(nums1) - 2, len(nums2) - 2
dp[nums1[-1]] = nums1[-1]
dp[nums2[-1]] = nums2[-1]
while i >= 0 or j >= 0:
v1, v2 = nums1[i] if i >= 0 else -1, nums2[j] if j >= 0 else -1
if v1 > v2:
dp[v1] = v1 + dp[nums1[i + 1]]
i -= 1
elif v1 < v2:
dp[v2] = v2 + dp[nums2[j + 1]]
j -= 1
else:
dp[v1] = v1 + max(dp[nums1[i + 1]], dp[nums2[j + 1]])
i -= 1
j -= 1
return max(dp[nums1[0]], dp[nums2[0]]) % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
graph = {}
u1, u2 = nums1[0], nums2[0]
for n in range(len(nums1) - 1):
u, v = nums1[n], nums1[n + 1]
if u in graph:
graph[u].append(v)
else:
graph[u] = [v]
for n in range(len(nums2) - 1):
u, v = nums2[n], nums2[n + 1]
if u in graph:
graph[u].append(v)
else:
graph[u] = [v]
DP = {}
MODN = 1000000007
def solve(u):
if u in DP:
return DP[u]
ans = u
if u in graph:
for v in graph[u]:
ans = max(ans, u + solve(v))
DP[u] = ans
return DP[u]
ans = max(solve(u1), solve(u2)) % MODN
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
dp1, dp2 = collections.defaultdict(lambda: 0), collections.defaultdict(
lambda: 0
)
i = j = 0
while i < len(nums1) and j < len(nums2):
x, y = nums1[i], nums2[j]
if x < y:
if i == 0:
dp1[x] = dp2[y] + x
else:
dp1[x] = max(dp1[nums1[i - 1]], dp2[y]) + x
i += 1
elif x > y:
if j == 0:
dp2[y] = dp1[x] + y
else:
dp2[y] = max(dp2[nums2[j - 1]], dp1[x]) + y
j += 1
else:
dp1[x] = max(
(dp2[nums2[j - 1]] if j != 0 else 0) + x,
(dp1[nums1[i - 1]] if i != 0 else 0) + x,
)
dp2[y] = max(
(dp1[nums1[i - 1]] if i != 0 else 0) + y,
(dp2[nums2[j - 1]] if j != 0 else 0) + y,
)
i += 1
j += 1
x, y = max(dp1.values()), max(dp2.values())
while i < len(nums1):
x += nums1[i]
i += 1
while j < len(nums2):
y += nums2[j]
j += 1
return max(x, y) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
BIG_INT = 10**9 + 7
maxSums = {}
num1Values = {}
num2Values = {}
num1Values[0] = -1
for index, num in enumerate(nums1):
num1Values[num] = index
num2Values[0] = -1
for index, num in enumerate(nums2):
num2Values[num] = index
num1Length = len(nums1)
num2Length = len(nums2)
nums1.append(0)
nums2.append(0)
def computeMaxSum(num1Left, num2Left):
if num1Left * num2Left == 0:
return 0
else:
if (num1Left, num2Left) in maxSums:
return maxSums[num1Left, num2Left]
current1 = 0 if num1Left == num1Length + 1 else nums1[-num1Left - 1]
current2 = 0 if num2Left == num2Length + 1 else nums2[-num2Left - 1]
if current1 == current2:
maxSums[num1Left, num2Left] = max(
computeMaxSum(num1Left - 1, num2Left) + nums1[-num1Left],
computeMaxSum(num1Left, num2Left - 1) + nums2[-num2Left],
)
elif current1 > current2:
if current1 in num2Values:
new2Index = num2Values[current1]
maxSums[num1Left, num2Left] = computeMaxSum(
num1Left, num2Length - new2Index
)
else:
maxSums[num1Left, num2Left] = (
computeMaxSum(num1Left - 1, num2Left) + nums1[-num1Left]
)
elif current2 in num1Values:
new1Index = num1Values[current2]
maxSums[num1Left, num2Left] = computeMaxSum(
num1Length - new1Index, num2Left
)
else:
maxSums[num1Left, num2Left] = (
computeMaxSum(num1Left, num2Left - 1) + nums2[-num2Left]
)
return maxSums[num1Left, num2Left]
return computeMaxSum(num1Length + 1, num2Length + 1) % BIG_INT
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
changeSum = 0
partialSums = [[0, 0]]
i, j, k = 0, 0, 0
while i < len(nums1) and j < len(nums2):
if nums1[i] == nums2[j]:
changeSum = (changeSum + nums1[i]) % (10**9 + 7)
i += 1
j += 1
partialSums.append([0, 0])
elif nums1[i] < nums2[j]:
partialSums[-1][0] += nums1[i]
i += 1
else:
partialSums[-1][1] += nums2[j]
j += 1
while i < len(nums1):
partialSums[-1][0] += nums1[i]
i += 1
while j < len(nums2):
partialSums[-1][1] += nums2[j]
j += 1
print(changeSum)
print(partialSums)
for i in partialSums:
changeSum += max(i)
return changeSum % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
intersecting_index = []
l = 0
m = 0
while l < len(nums1) and m < len(nums2):
if nums1[l] == nums2[m]:
intersecting_index.append([l, m])
l += 1
m += 1
elif nums1[l] > nums2[m]:
m += 1
else:
l += 1
prefix_1 = [(0) for i in range(len(nums1) + 1)]
prefix_2 = [(0) for i in range(len(nums2) + 1)]
for i in range(1, len(nums1) + 1):
prefix_1[i] = prefix_1[i - 1] + nums1[i - 1]
for i in range(1, len(nums2) + 1):
prefix_2[i] = prefix_2[i - 1] + nums2[i - 1]
ans = []
prev_1 = 0
prev_2 = 0
for i in intersecting_index:
ans.append(
[
prefix_1[i[0] + 1] - prefix_1[prev_1],
prefix_2[i[1] + 1] - prefix_2[prev_2],
]
)
prev_1 = i[0] + 1
prev_2 = i[1] + 1
ans.append([prefix_1[-1] - prefix_1[prev_1], prefix_2[-1] - prefix_2[prev_2]])
s = 0
for i in ans:
s = (max(i[0], i[1]) + s) % (pow(10, 9) + 7)
return s
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
MOD = 10**9 + 7.0
def csums(nums):
arr = [None] * len(nums)
for i in range(len(nums)):
prev = 0 if i == 0 else arr[i - 1]
arr[i] = prev + nums[i]
return arr
s1, s2 = csums(nums1), csums(nums2)
mp1 = {v: idx for idx, v in enumerate(nums1)}
mp2 = {v: idx for idx, v in enumerate(nums2)}
sps = sorted(set(mp1.keys()) & set(mp2.keys()))
ans = 0
if not sps:
ans = max(s1[-1], s2[-1]) % MOD
else:
idx1 = idx2 = None
for idx, sp in enumerate(sps):
idx1, idx2 = mp1[sp], mp2[sp]
sum1 = s1[idx1] - (0 if idx == 0 else s1[mp1[sps[idx - 1]]])
sum2 = s2[idx2] - (0 if idx == 0 else s2[mp2[sps[idx - 1]]])
ans = (ans + max(sum1, sum2)) % MOD
left1 = s1[-1] - s1[idx1]
left2 = s2[-1] - s2[idx2]
ans += max(left1, left2)
return int(ans % MOD)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NONE FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
dp1 = [0] * (len(nums1) + 1)
dp2 = [0] * (len(nums2) + 1)
dp1[-1] = 0
dp2[-1] = 0
to1 = {}
to2 = {}
inds2 = {y: j for j, y in enumerate(nums2)}
for i, x in enumerate(nums1):
if x in inds2:
to2[i] = inds2[x]
to1[inds2[x]] = i
i = len(nums1) - 1
j = len(nums2) - 1
while i >= 0 and j >= 0:
if i not in to2:
dp1[i] = nums1[i] + dp1[i + 1]
i -= 1
elif j not in to1:
dp2[j] = nums2[j] + dp2[j + 1]
j -= 1
else:
dp1[i] = dp2[j] = nums1[i] + max(dp1[i + 1], dp2[j + 1])
i -= 1
j -= 1
while i >= 0:
dp1[i] = nums1[i] + dp1[i + 1]
i -= 1
while j >= 0:
dp2[j] = nums2[j] + dp2[j + 1]
j -= 1
return max(dp1[0], dp2[0]) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
i1, i2, n1, n2, f1, f2 = 0, 0, len(nums1), len(nums2), 0, 0
while i1 < n1 or i2 < n2:
if i1 == n1:
f2 += sum(nums2[i2:])
i2 = n2
continue
if i2 == n2:
f1 += sum(nums1[i1:])
i1 = n1
continue
if nums1[i1] < nums2[i2]:
f1 += nums1[i1]
i1 += 1
continue
if nums1[i1] > nums2[i2]:
f2 += nums2[i2]
i2 += 1
continue
f1 = f2 = max(f2 + nums2[i2], f1 + nums1[i1])
i1 += 1
i2 += 1
return max(f1, f2) % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
sum1 = 0
sum2 = 0
i1 = len(nums1) - 1
i2 = len(nums2) - 1
paths = []
while i1 >= 0 and i2 >= 0:
if nums1[i1] > nums2[i2]:
sum1 += nums1[i1]
i1 -= 1
elif nums1[i1] < nums2[i2]:
sum2 += nums2[i2]
i2 -= 1
else:
paths.append((sum1 + nums1[i1], sum2 + nums1[i1]))
sum1 = sum2 = 0
i1 -= 1
i2 -= 1
if i1 >= 0:
paths.append((sum1 + sum(nums1[: i1 + 1]), sum2))
if i2 >= 0:
paths.append((sum1, sum2 + sum(nums2[: i2 + 1])))
ans = 0
for l, r in paths:
ans += max(l, r)
ans %= 10**9 + 7
return ans
def _dp(ii, its1, val):
nums = nums1 if its1 else nums2
oums = nums2 if its1 else nums1
if ii == len(nums):
return val
if nums[ii] in oums:
ix = oums.index(nums[ii])
opt = _dp(ix + 1, not its1, val + nums[ii])
else:
opt = 0
cur = _dp(ii + 1, its1, val + nums[ii])
return max([opt, cur])
return max([_dp(0, True, 0), _dp(0, False, 0)])
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR RETURN FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
i, j = 0, 0
n1, n2 = len(nums1), len(nums2)
total1 = 0
total2 = 0
while i < n1 or j < n2:
v1 = nums1[i] if i < n1 else math.inf
v2 = nums2[j] if j < n2 else math.inf
if v1 < v2:
total1 += v1
i += 1
elif v2 < v1:
total2 += v2
j += 1
elif v1 == v2:
total = max(total1 + v1, total2 + v2)
total1 = total
total2 = total
i += 1
j += 1
return max(total1, total2) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
prev = collections.defaultdict(set)
MOD = 10**9 + 7
if not nums1:
return sum(nums2)
elif not nums2:
return sum(num1)
ans = 0
combined = sorted(set(nums1 + nums2))
dp = collections.defaultdict(lambda: 0)
dp[-1] = 0
for i in range(len(nums1)):
if i == 0:
prev[nums1[i]].add(-1)
prev[nums1[i]].add(nums1[i - 1])
for i in range(len(nums2)):
if i == 0:
prev[nums2[i]].add(-1)
prev[nums2[i]].add(nums2[i - 1])
for num in combined:
for p in prev[num]:
dp[num] = max(dp[p] + num, dp[num])
ans = max(ans, dp[num])
return ans % MOD
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR RETURN FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
rev1 = {v: k for k, v in enumerate(nums1)}
rev2 = {v: k for k, v in enumerate(nums2)}
def dp(val, memo):
if val in memo:
return memo[val]
ans = 0
if val in rev1:
ans = val
if rev1[val] < len(nums1) - 1:
ans = max(ans, dp(nums1[rev1[val] + 1], memo) + val)
if val in rev2:
ans = max(ans, val)
if rev2[val] < len(nums2) - 1:
ans = max(ans, dp(nums2[rev2[val] + 1], memo) + val)
ans = ans
memo[val] = ans
return ans
memo = {}
return max(dp(nums1[0], memo), dp(nums2[0], memo)) % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
common = set(nums1).intersection(set(nums2))
segments1 = []
tmp = 0
for i in nums1:
tmp += i
if i in common:
segments1.append(tmp)
tmp = 0
segments1.append(tmp)
segments2 = []
tmp = 0
for i in nums2:
tmp += i
if i in common:
segments2.append(tmp)
tmp = 0
segments2.append(tmp)
ans = 0
for i in range(len(segments1)):
ans += max(segments1[i], segments2[i])
return ans % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
def f(i, p):
if p == 0 and i >= len(nums1) or p == 1 and i >= len(nums2):
return 0
if (i, p) in dp:
return dp[i, p]
ans = f(i + 1, p)
if p == 0:
if nums1[i] in b:
ans = max(ans, f(b[nums1[i]] + 1, 1 - p))
elif nums2[i] in a:
ans = max(ans, f(a[nums2[i]] + 1, 1 - p))
dp[i, p] = ans + (nums1[i] if p == 0 else nums2[i])
return dp[i, p]
a, b, dp = {}, {}, {}
for i, j in enumerate(nums1):
a[j] = i
for i, j in enumerate(nums2):
b[j] = i
return max(f(0, 0), f(0, 1)) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR DICT DICT DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
a, b = nums1, nums2
m, n = len(a), len(b)
p1 = {x: i for i, x in enumerate(a)}
p2 = {x: i for i, x in enumerate(b)}
@lru_cache(None)
def dp(i, use_a):
if use_a:
if i == m:
return 0
ans = a[i] + dp(i + 1, True)
if a[i] in p2:
j = p2[a[i]] + 1
ans = max(ans, a[i] + dp(j, False))
else:
if i == n:
return 0
ans = b[i] + dp(i + 1, False)
if b[i] in p1:
j = p1[b[i]] + 1
ans = max(ans, b[i] + dp(j, True))
return ans
return max(dp(0, True), dp(0, False)) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
current_value = 0
ptr1 = 0
ptr2 = 0
nums1Total = 0
nums2Total = 0
while ptr1 < len(nums1) and ptr2 < len(nums2):
value1 = nums1[ptr1]
value2 = nums2[ptr2]
if value1 == value2:
current_value += max(nums1Total, nums2Total) + value1
ptr1 += 1
ptr2 += 1
nums1Total = 0
nums2Total = 0
elif value1 < value2:
nums1Total += value1
ptr1 += 1
else:
nums2Total += value2
ptr2 += 1
while ptr1 < len(nums1):
nums1Total += nums1[ptr1]
ptr1 += 1
while ptr2 < len(nums2):
nums2Total += nums2[ptr2]
ptr2 += 1
current_value += max(nums1Total, nums2Total)
return current_value % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
dp1 = [(0) for _ in range(len(nums1) + 1)]
dp2 = [(0) for _ in range(len(nums2) + 1)]
i1, i2 = len(nums1) - 1, len(nums2) - 1
while i1 >= 0 and i2 >= 0:
if nums1[i1] == nums2[i2]:
dp1[i1] = max(nums1[i1] + dp1[i1 + 1], nums2[i2] + dp2[i2 + 1])
dp2[i2] = max(nums1[i1] + dp1[i1 + 1], nums2[i2] + dp2[i2 + 1])
i1 -= 1
i2 -= 1
elif nums1[i1] > nums2[i2]:
dp1[i1] = nums1[i1] + dp1[i1 + 1]
i1 -= 1
else:
dp2[i2] = nums2[i2] + dp2[i2 + 1]
i2 -= 1
if i1 >= 0:
return max(sum(nums1[: i1 + 1]) + dp1[i1 + 1], dp2[i2 + 1]) % (10**9 + 7)
else:
return max(sum(nums2[: i2 + 1]) + dp2[i2 + 1], dp1[i1 + 1]) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
m = len(nums2)
t1 = [(0) for _ in range(n + 1)]
t2 = [(0) for _ in range(m + 1)]
i, j = 1, 1
while i < n + 1 and j < m + 1:
if nums1[i - 1] == nums2[j - 1]:
t1[i] = max(t1[i - 1], t2[j - 1]) + nums1[i - 1]
t2[j] = max(t1[i - 1], t2[j - 1]) + nums2[j - 1]
i += 1
j += 1
elif nums1[i - 1] < nums2[j - 1]:
t1[i] = t1[i - 1] + nums1[i - 1]
i += 1
else:
t2[j] = t2[j - 1] + nums2[j - 1]
j += 1
while i < n + 1:
t1[i] = t1[i - 1] + nums1[i - 1]
i += 1
while j < m + 1:
t2[j] = t2[j - 1] + nums2[j - 1]
j += 1
return max(t1[-1], t2[-1]) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
def findIndex(arr, ele, start, end):
while start < end:
middle = (start + end - 1) // 2
if ele == arr[middle]:
return middle
if ele < arr[middle]:
end = middle
elif ele > arr[middle]:
start = middle + 1
return -1
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
temp, total = 0, 0
i, j = 0, 0
n1, n2 = len(nums1), len(nums2)
path1, path2 = nums1, nums2
while i < n1:
index = findIndex(path2, path1[i], j, n2)
if index == -1:
total += path1[i]
temp += path1[i]
i += 1
else:
if temp >= sum(path2[j:index]):
total += path1[i]
temp = path1[i]
else:
total -= temp
total += sum(path2[j : index + 1])
temp = path2[index]
i, j = index + 1, i
n1, n2 = n2, n1
path1, path2 = path2, path1
if temp < sum(path2[j:n2]):
total -= temp
total += sum(path2[j:n2])
return total % 1000000007
|
FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
accu1 = list(accumulate(nums1))
accu2 = list(accumulate(nums2))
print(accu1)
ans = i = j = 0
pi = pj = -1
while i < len(nums1) and j < len(nums2):
if nums1[i] < nums2[j]:
i += 1
elif nums1[i] > nums2[j]:
j += 1
else:
ans += (
max(accu1[i], accu2[j])
if pi < 0
else max(accu1[i] - accu1[pi], accu2[j] - accu2[pj])
)
pi, pj, i, j = i, j, i + 1, j + 1
ans += (
max(accu1[-1], accu2[-1])
if pi < 0
else max(accu1[-1] - accu1[pi], accu2[-1] - accu2[pj])
)
return ans % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
ret = s1 = s2 = 0
n1, n2 = len(nums1), len(nums2)
i = j = 0
while i < n1 or j < n2:
if i == n1:
s2 += nums2[j]
j += 1
elif j == n2:
s1 += nums1[i]
i += 1
elif nums1[i] < nums2[j]:
s1 += nums1[i]
i += 1
elif nums1[i] > nums2[j]:
s2 += nums2[j]
j += 1
else:
ret += max(s1, s2) + nums1[i]
s1 = s2 = 0
i += 1
j += 1
ret += max(s1, s2)
return ret % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
MOD = 1000000007
Dict1 = defaultdict(int)
Dict2 = defaultdict(int)
for i in range(len(nums1)):
Dict1[nums1[i]] = i + 1
for i in range(len(nums2)):
Dict2[nums2[i]] = i + 1
Dict3 = {}
Dict4 = {}
n1 = len(nums1)
n2 = len(nums2)
for i in nums1:
if Dict1[i] > 0 and Dict2[i] > 0:
Dict3[i] = Dict2[i]
Dict4[i] = Dict1[i]
@lru_cache(None)
def go(i, pos):
if pos == 0:
if i >= n1:
return 0
ans = 0
if Dict1[nums1[i]] >= 1 and Dict2[nums1[i]] >= 1:
ans = max(
ans,
go(i + 1, pos) + nums1[i],
go(Dict3[nums1[i]], (pos + 1) % 2) + nums1[i],
)
else:
ans = max(ans, go(i + 1, pos) + nums1[i])
return ans
else:
if i >= n2:
return 0
ans = 0
if Dict1[nums2[i]] >= 1 and Dict2[nums2[i]] >= 1:
ans = max(
ans,
go(i + 1, pos) + nums2[i],
go(Dict4[nums2[i]], (pos + 1) % 2) + nums2[i],
)
else:
ans = max(ans, go(i + 1, pos) + nums2[i])
return ans
return max(go(0, 0), go(0, 1)) % MOD
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, l1: List[int], l2: List[int]) -> int:
last_p1 = 0
last_p2 = 0
p1 = 0
p2 = 0
sum_so_far = 0
while p1 < len(l1) and p2 < len(l2) and last_p1 < len(l1) and last_p2 < len(l2):
if l1[p1] < l2[p2]:
p1 += 1
elif l1[p1] > l2[p2]:
p2 += 1
else:
sum_so_far += max(sum(l1[last_p1:p1]), sum(l2[last_p2:p2]))
sum_so_far += l1[p1]
last_p1 = p1 + 1
last_p2 = p2 + 1
p1 += 1
p2 += 1
if l1[-1] != l2[-1]:
sum_so_far += max(sum(l1[last_p1:]), sum(l2[last_p2:]))
return sum_so_far % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, a: List[int], b: List[int]) -> int:
a2i = {n: i for i, n in enumerate(a)}
b2i = {n: i for i, n in enumerate(b)}
@lru_cache(None)
def solve(i, is_a, switched):
if is_a:
c = a
c2i = a2i
d = b
d2i = b2i
else:
c = b
c2i = b2i
d = a
d2i = a2i
add = 0 if switched else c[i]
best = add
if not switched and c[i] in d2i:
best = add + solve(d2i[c[i]], not is_a, True)
if i == len(c) - 1:
return best
return max(best, add + solve(i + 1, is_a, False))
return max(solve(0, True, False), solve(0, False, False)) % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, arra: List[int], arrb: List[int]) -> int:
m, n = len(arra), len(arrb)
dica = {a: i for i, a in enumerate(arra)}
dicb = {b: i for i, b in enumerate(arrb)}
dpa, dpb = {}, {}
def go(i, f):
arr1, arr2 = arra, arrb
dic1, dic2 = dica, dicb
dp1, dp2 = dpa, dpb
if not f:
arr1, arr2 = arr2, arr1
dic1, dic2 = dic2, dic1
dp1, dp2 = dp2, dp1
a = arr1[i]
d = a + (dp1[arr1[i - 1]] if i > 0 else 0)
if a in dic2 and dic2[a] > 0:
d = max(d, a + dp2[arr2[dic2[a] - 1]])
dp1[a] = d
i, j = 0, 0
while i < m and j < n:
if arra[i] <= arrb[j]:
go(i, True)
i += 1
else:
go(j, False)
j += 1
while i < m:
go(i, True)
i += 1
while j < n:
go(j, False)
j += 1
return max(dpa[arra[-1]], dpb[arrb[-1]]) % 1000000007
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT DICT FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
l1 = len(nums1)
l2 = len(nums2)
index1 = 0
index2 = 0
score1 = 0
score2 = 0
maxScore = 0
while index1 < l1 and index2 < l2:
n1 = nums1[index1]
n2 = nums2[index2]
if n1 == n2:
score1 += n1
score2 += n2
if score1 < score2:
maxScore += score2
else:
maxScore += score1
score1 = 0
score2 = 0
index1 += 1
index2 += 1
elif n1 < n2:
score1 += n1
index1 += 1
else:
score2 += n2
index2 += 1
for i in range(index1, l1):
score1 += nums1[i]
for i in range(index2, l2):
score2 += nums2[i]
if score1 < score2:
maxScore += score2
else:
maxScore += score1
return maxScore % (10**9 + 7)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
|
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
Score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Example 4:
Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
Output: 61
Constraints:
1 <= nums1.length <= 10^5
1 <= nums2.length <= 10^5
1 <= nums1[i], nums2[i] <= 10^7
nums1 and nums2 are strictly increasing.
|
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
mod = 10**9 + 7
l1 = []
d = {}
d1 = {}
d2 = {}
for i in nums1:
d[i] = 1
for i in nums2:
d[i] = d.get(i, 0) + 1
for i in d:
if d[i] == 2:
l1.append(i)
l1.sort()
n1 = len(l1) + 1
l1.insert(0, 0)
dp = [[(0) for j in range(2)] for i in range(n1 + 1)]
pref1 = [0] * (len(nums1) + 1)
pref2 = [0] * (len(nums2) + 1)
for i in range(len(nums1)):
d1[nums1[i]] = i
pref1[i + 1] = pref1[i] + nums1[i]
for i in range(len(nums2)):
d2[nums2[i]] = i
pref2[i + 1] = pref2[i] + nums2[i]
d1[0] = 0
d2[0] = 0
for i in range(1, n1):
dp[i][0] = (
max(dp[i - 1][0], dp[i - 1][1])
+ pref1[d1[l1[i]] + 1]
- pref1[d1[l1[i - 1]]]
- l1[i - 1]
)
dp[i][1] = (
max(dp[i - 1][0], dp[i - 1][1])
+ pref2[d2[l1[i]] + 1]
- pref2[d2[l1[i - 1]]]
- l1[i - 1]
)
return (
max(dp[n1 - 1][0], dp[n1 - 1][1])
+ max(
pref1[len(nums1)] - pref1[d1[l1[-1]]] - l1[-1],
pref2[len(nums2)] - pref2[d2[l1[-1]]] - l1[-1],
)
) % mod
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = list(input())
t = list(input())
left = []
i = 0
j = 0
while i < n and j < m:
if s[i] == t[j]:
left.append(i)
i += 1
j += 1
else:
i += 1
right = []
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
right.append(i)
i -= 1
j -= 1
else:
i -= 1
right.reverse()
ans = 0
for i in range(m - 1):
ans = max(ans, right[i + 1] - left[i])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
fast_substring = [0] * m
last_substring = [0] * m
j = 0
for i in range(n):
if s[i] == t[j]:
fast_substring[j] = i
j += 1
if j == m:
break
j = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[j]:
last_substring[j] = i
j -= 1
if j < 0:
break
ans = -1
for i in range(m - 1):
ans = max(ans, last_substring[i + 1] - fast_substring[i])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solve(n, m):
l = []
r = []
i1, i2 = 0, 0
while i1 < n and i2 < m:
if s[i1] == t[i2]:
l.append(i1)
i2 += 1
i1 += 1
i1, i2 = n - 1, m - 1
while i1 > -1 and i2 > -1:
if s[i1] == t[i2]:
r.append(i1)
i2 -= 1
i1 -= 1
maxx = 0
r = r[::-1]
for i in range(m - 1):
maxx = max(maxx, r[i + 1] - l[i])
print(maxx)
n, m = map(int, input().split())
s = input()
t = input()
solve(n, m)
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solution():
n, m = map(int, input().split())
s = input()
t = input()
rev_s = "".join(reversed(s))
rev_t = "".join(reversed(t))
last_pos = len(t) * [0]
start = 0
for i in range(len(t)):
idx = rev_s.find(rev_t[i], start)
last_pos[i] = len(s) - idx - 1
start = idx + 1
last_pos.reverse()
result = 1
cur_pos = -1
for i in range(len(t) - 1):
cur_pos = s.find(t[i], cur_pos + 1)
result = max(result, last_pos[i + 1] - cur_pos)
print(result)
solution()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
p = [(0) for i in range(m)]
i, j = 0, 0
while j < m:
while s[i] != t[j]:
i += 1
p[j] = i
i += 1
j += 1
sol = 0
i = n - 1
while j > 1:
j -= 1
while s[i] != t[j]:
i -= 1
sol = max(sol, i - p[j - 1])
i -= 1
print(sol)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = list(input())
t = list(input())
start = []
end = []
j = 0
k = n - 1
for i in range(m):
while s[j] != t[i]:
j += 1
start.append(j)
j += 1
while s[k] != t[m - 1 - i]:
k -= 1
end.append(k)
k -= 1
end = end[::-1]
ans = 0
for i in range(1, m):
ans = max(ans, end[i] - start[i - 1])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s1 = input()
s2 = input()
left = dict()
right = dict()
l = 0
r = n - 1
for i in range(m):
while s1[l] != s2[i]:
l = l + 1
left[i] = l
l = l + 1
for i in range(m - 1, -1, -1):
while s1[r] != s2[i]:
r = r - 1
right[i] = r
r = r - 1
res = 0
for i in range(m - 1):
res = max(res, right[i + 1] - left[i])
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def other(n, m, s, t):
d = [0] * m
i = 0
j = 0
while j < m:
if s[i] == t[j]:
d[j] = i
j += 1
i += 1
e = [0] * m
i = n - 1
j = m - 1
while j >= 0:
if s[i] == t[j]:
e[j] = i
j -= 1
i -= 1
mm = 0
for i in range(m - 1):
if e[i + 1] - d[i] > mm:
mm = e[i + 1] - d[i]
return mm
n, m = map(int, input().split())
s = list(str(input()))
t = list(str(input()))
print(other(n, m, s, t))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
input()
s = input()
t = input()
n = len(s)
m = len(t)
ls = []
i = 0
for c in t:
while s[i] != c:
i += 1
ls.append(i)
i += 1
rs = []
i = n - 1
for c in t[::-1]:
while s[i] != c:
i -= 1
rs.append(i)
i -= 1
rs = rs[::-1]
print(max(rs[i] - ls[i - 1] for i in range(1, m)))
|
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
from sys import *
input = stdin.readline
n, m = map(int, input().split())
s = list(input())
t = list(input())
found = []
idx, idy = m - 1, 0
for i in range(n - 1, -1, -1):
if idx >= 0:
if s[i] == t[idx]:
found.append(i)
idx -= 1
found = found[::-1]
forw = []
for i in range(n):
if idy < m:
if s[i] == t[idy]:
forw.append(i)
idy += 1
ans = 0
for i in range(m - 1):
ans = max(found[i + 1] - forw[i], ans)
print(ans)
|
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
_, s, t = open(0)
r = i = j = -1
a = []
for x in t[:-2]:
i = s.find(x, i + 1)
a += (i,)
for x in t[-2:0:-1]:
j = s.rfind(x, 0, j)
r = max(r, j - a.pop())
print(r)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = [int(k) for k in input().split()]
s = input()
t = input()
c = -1
res = []
for j in range(n):
if s[-j - 1] == t[c]:
res.append(n - j - 1)
c -= 1
if c < -m:
break
mx = 0
c = 0
res = res[::-1]
for j in range(n):
if s[j] == t[c]:
mx = max(res[c + 1] - j, mx)
c += 1
if c == m - 1:
break
print(mx)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
s, t = input().strip(), input().strip()
forward_index, reverse_index, i, j, ans = [], [], 0, 0, 0
while i < n and j < m:
if s[i] == t[j]:
forward_index.append(i)
i, j = i + 1, j + 1
else:
i += 1
i, j = n - 1, m - 1
while i > -1 and j > -1:
if s[i] == t[j]:
reverse_index.append(i)
i, j = i - 1, j - 1
else:
i -= 1
reverse_index = reverse_index[0 : len(reverse_index) - 1][::-1]
for i in range(len(forward_index) - 1):
ans = max(ans, reverse_index[i] - forward_index[i])
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR LIST LIST NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
def main():
n, m = readIntArr()
s = input()
t = input()
j = 0
minP = [None for _ in range(m)]
for i in range(n):
if s[i] == t[j]:
minP[j] = i
j += 1
if j == m:
break
j = m - 1
maxP = [None for _ in range(m)]
for i in range(n - 1, -1, -1):
if s[i] == t[j]:
maxP[j] = i
j -= 1
if j == -1:
break
ans = -inf
for i in range(1, m):
ans = max(ans, maxP[i] - minP[i - 1])
print(ans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
j = 0
left = [-1] * (m + 1)
for i in range(m):
while j < n and s[j] != t[i]:
j += 1
if j == n:
break
left[i + 1] = j
j += 1
right = [-1] * (m + 1)
j = n - 1
for i in range(m - 1, -1, -1):
while j >= 0 and s[j] != t[i]:
j -= 1
if j == -1:
break
right[m - 1 - i + 1] = j
j -= 1
w = 0
for i in range(1, m, +1):
w = max(w, right[m - i] - left[i])
print(w)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
l = []
l1 = []
i = 0
j = 0
while i < n and j < m:
if s[i] == t[j]:
l.append(i + 1)
i += 1
j += 1
else:
i += 1
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
l1.append(i + 1)
i -= 1
j -= 1
else:
i -= 1
l1.reverse()
mi = 0
for i in range(len(l) - 1):
x = l1[i + 1] - l[i]
mi = max(mi, x)
print(mi)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
nxt = [(0) for i in range(m)]
lst = [(0) for i in range(m)]
j = 0
for i in range(n):
if j < m and s[i] == t[j]:
nxt[j] = i
j = j + 1
j = m - 1
for i in range(n - 1, -1, -1):
if j >= 0 and s[i] == t[j]:
lst[j] = i
j = j - 1
mx = 0
for i in range(m - 1):
mx = max(mx, lst[i + 1] - nxt[i])
print(mx)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
s, t = input()[:-1], input()[:-1]
left, right = [0] * m, [0] * m
i = 0
for j in range(m):
while s[i] != t[j]:
i += 1
left[j] = i
i += 1
i = n - 1
for j in range(m - 1, -1, -1):
while s[i] != t[j]:
i -= 1
right[j] = i
i -= 1
ans = 0
for i in range(m - 1):
ans = max(ans, right[i + 1] - left[i])
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
left = [(0) for i in range(m)]
right = [(0) for i in range(m)]
j = 0
i = 0
while j < m:
while s[i] != t[j]:
i += 1
left[j] = i
i += 1
j += 1
j = m - 1
i = n - 1
while j >= 0:
while s[i] != t[j]:
i -= 1
right[j] = i
i -= 1
j -= 1
ans = 0
for i in range(1, m):
ans = max(right[i] - left[i - 1], ans)
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
ans = 0
startPos = s.find(t[0])
endPos = s.rfind(t[m - 1])
counter = 2
maxVals = [(0) for i in range(m)]
minVals = [(0) for i in range(m)]
minVals[0] = startPos
maxVals[m - 1] = endPos
for i in range(endPos - 1, startPos, -1):
if s[i] == t[m - counter] and counter < m:
maxVals[m - counter] = i
counter += 1
counter = 1
for i in range(startPos + 1, endPos, 1):
if s[i] == t[counter] and counter < m - 1:
minVals[counter] = i
counter += 1
for i in range(1, m):
ans = max(ans, maxVals[i] - minVals[i - 1])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
def findFirstOption(n, m, s, t):
firstOccurrence = [(0) for _ in range(m)]
cur = 0
for i in range(n):
if cur == m:
break
chS = ord(s[i]) - ord("a")
chT = ord(t[cur]) - ord("a")
if chT == chS:
firstOccurrence[cur] = i
cur += 1
return firstOccurrence
a = findFirstOption(n, m, s, t)
b = [(n - 1 - x) for x in findFirstOption(n, m, s[::-1], t[::-1])[::-1]]
print(max([(b[i + 1] - a[i]) for i in range(m - 1)]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
if n == m:
print(1)
else:
left = [0] * m
i, j = 0, 0
while j < m:
if s[i] == t[j]:
left[j] = i
j += 1
i += 1
i, j = n - 1, m - 1
res = 0
while j > 0:
if s[i] == t[j]:
res = max(res, i - left[j - 1])
j -= 1
i -= 1
print(res)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
l = []
r = []
j = 0
for i in range(n):
if j == len(t):
break
if s[i] == t[j]:
l.append(i)
j += 1
j = len(t) - 1
for i in range(n - 1, -1, -1):
if j == -1:
break
if s[i] == t[j]:
r.append(i)
j -= 1
r.reverse()
max = 0
for i in range(1, m):
if int(r[i]) - int(l[i - 1]) > max:
max = int(r[i]) - int(l[i - 1])
print(max)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def int_fn():
return int(input())
def str_fn():
return input()
def int_list_fn():
return [int(val) for val in input().split(" ")]
def solve(source: str, n: int, target: str, m: int):
min_list, max_list = [-1] * m, [-1] * m
i = 0
for idx, ch in enumerate(source):
if target[i] == ch:
min_list[i] = idx
i += 1
if i == m:
break
i = m - 1
for last_idx in range(n - 1, -1, -1):
ch = source[last_idx]
if ch == target[i]:
max_list[i] = last_idx
i -= 1
if i < 0:
break
max_diff = 1
for idx in range(1, m):
max_diff = max(max_diff, max_list[idx] - min_list[idx - 1])
print(max_diff)
return
n, m = int_list_fn()
source = str_fn()
target = str_fn()
solve(source, n, target, m)
pass
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
sys.setrecursionlimit(10**6)
def lcm(a, b, curr, mx):
if len(b) == 0:
return max(mx, curr)
if len(a) == 0:
return 0
v = 0
if a[0] == b[0]:
v = lcm(a[1:], b[1:], 0, max(curr, mx))
return max(v, lcm(a[1:], b, curr + 1, mx))
n, m = list(map(int, input().split()))
s = input()
t = input()
a, b = [], []
l = 0
for i in range(m):
while s[l] != t[i]:
l += 1
a.append(l)
l += 1
l = n - 1
for i in range(m)[::-1]:
while s[l] != t[i]:
l -= 1
b.append(l)
l -= 1
b = b[::-1]
x = 0
for i in range(len(a) - 1):
x = max(x, b[i + 1] - a[i])
print(x)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solve():
n, m = list(map(int, input().split()))
s = list(input())
t = list(input())
e, l = [0] * m, [0] * m
j, i = 0, 0
while j < m:
if t[j] == s[i]:
e[j] = i
j += 1
i += 1
j, i = m - 1, n - 1
while j >= 0:
if t[j] == s[i]:
l[j] = i
j -= 1
i -= 1
best = 0
for i in range(m - 1):
best = max(l[i + 1] - e[i], best)
return best
print(solve())
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def main():
inp = input().rstrip().split(" ")
n, m = int(inp[0]), int(inp[1])
s = input().rstrip()
t = input().rstrip()
fwd_arr = [(0) for i in range(m)]
bwd_arr = [(0) for i in range(m)]
fpos = 0
bpos = n - 1
for i in range(m):
while s[fpos] != t[i]:
fpos += 1
fwd_arr[i] = fpos
fpos += 1
while s[bpos] != t[m - 1 - i]:
bpos -= 1
bwd_arr[m - 1 - i] = bpos
bpos -= 1
ans = 0
for i in range(m - 1):
ans = max(ans, bwd_arr[i + 1] - fwd_arr[i])
print(ans)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solve(s, t):
ans = []
for i, c in enumerate(s):
if c == t[len(ans)]:
ans.append(i)
if len(ans) == len(t):
break
return ans
def main():
n, m = map(int, input().split())
s = input()
t = input()
ans = 0
first_s = solve(s, t)
last_s = [(n - i - 1) for i in solve(s[::-1], t[::-1])]
last_s.reverse()
for i in range(m - 1):
ans = max(ans, last_s[i] - first_s[i + 1], last_s[i + 1] - first_s[i])
print(ans)
main()
|
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
forward = []
cur = 0
curr = 0
while curr < len(s) and cur < len(t):
if s[curr] == t[cur]:
forward.append(curr)
cur += 1
curr += 1
backward = []
cur = len(s) - 1
curr = len(t) - 1
while cur >= 0 and curr >= 0:
if s[cur] == t[curr]:
backward.append(cur)
curr -= 1
cur -= 1
backward = backward[::-1]
ans = 0
for i in range(1, len(t)):
ans = max(ans, backward[i] - forward[i - 1], forward[i] - backward[i - 1])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
bl, sl = map(int, input().split())
b, s = input(), input()
c = 0
i, cind = 0, 0
st = []
en = []
while 1:
if b[cind] == s[i]:
st.append(cind)
i += 1
if i == sl:
break
cind += 1
i, cind = 0, 0
while 2:
if b[bl - cind - 1] == s[sl - i - 1]:
en.append(bl - cind - 1)
i += 1
if i == sl:
break
cind += 1
en = en[::-1]
for i in range(len(st) - 1):
c = max(c, en[i + 1] - st[i])
print(max(c, en[-1] - st[-1]))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n, m = list(map(int, input().split()))
s = input()
t = input()
if n == m or m == 1:
print(1)
else:
indexes = [0] * m
revIndexes = [0] * m
front, back = 0, n - 1
for i in range(m):
while s[front] != t[i]:
front += 1
indexes[i] = front
front += 1
while s[back] != t[m - 1 - i]:
back -= 1
revIndexes[m - 1 - i] = back
back -= 1
ans = 1
for i in range(m - 1):
ans = max(
ans,
abs(indexes[i + 1] - revIndexes[i]),
abs(indexes[i] - revIndexes[i + 1]),
)
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
low = []
pointer = 0
for index, char in enumerate(s):
if char == t[pointer]:
low.append(index)
pointer += 1
if pointer == m:
break
else:
raise RuntimeError("Shouldn't reach here")
high = []
pointer = m - 1
for index, char in reversed(tuple(enumerate(s))):
if char == t[pointer]:
high.append(index)
pointer -= 1
if pointer < 0:
break
else:
raise ValueError
high.reverse()
print(max(high[i] - low[i - 1] for i in range(1, m)))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solve(n, m, s, t):
idx = [0] * m
j = 0
for i in range(n):
if j < m and t[j] == s[i]:
idx[j] = i
j += 1
res = 0
j = m - 1
for i in range(n - 1, -1, -1):
if j >= 0 and t[j] == s[i]:
res = max(res, i - idx[j - 1])
j -= 1
return res
n, m = map(int, input().split())
s = input()
t = input()
print(solve(n, m, s, t))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def find():
n, m = map(int, input().split())
s = input()
t = input()
ind = 0
left = []
right = [0] * m
for j in range(m):
while s[ind] != t[j]:
ind += 1
left.append(ind)
ind += 1
ind = n - 1
for j in range(m - 1, -1, -1):
while s[ind] != t[j]:
ind -= 1
right[j] = ind
ind -= 1
ans = 1
for j in range(m - 1):
ans = max(ans, right[j + 1] - left[j])
print(ans)
find()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
last = []
start = []
i = 0
j = 0
while i < n and j < m:
if s[i] == t[j]:
start.append(i)
i += 1
j += 1
else:
i += 1
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
last.append(i)
i -= 1
j -= 1
else:
i -= 1
last = last[::-1]
mx = last[m - 1] - start[m - 2]
for i in range(m - 1, 0, -1):
mx = max(mx, last[i] - start[i - 1])
print(mx)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = [int(x) for x in input().split()]
s = input()
t = input()
n = len(s)
m = len(t)
p = []
j = 0
width = 0
for i, x in enumerate(t):
j0 = j
while s[j] != x:
j += 1
p += [j]
j += 1
width = 0
for i in range(m - 1):
if p[i + 1] - p[i] > width:
width = p[i + 1] - p[i]
newpos = n - 1
for i in range(m - 1, 0, -1):
oldpos = p[i]
element = t[i]
while True:
if newpos == oldpos:
newpos -= 1
break
if s[newpos] == element:
p[i] = newpos
if width < newpos - p[i - 1]:
width = newpos - p[i - 1]
newpos -= 1
break
else:
newpos -= 1
print(width)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
a = []
ans = 0
i = -1
for x in t[:-1]:
i = s.find(x, i + 1)
a += [i]
i = n
for x in t[:0:-1]:
i = s.rfind(x, 0, i)
ans = max(ans, i - a.pop())
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR LIST VAR ASSIGN VAR VAR FOR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
a = []
b = []
k = 0
for i in range(n):
if s[i] == t[k]:
a.append(i)
k += 1
if k == m:
break
k = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[k]:
b.append(i)
k -= 1
if k == -1:
break
b.reverse()
k = 0
for i in range(m):
if i < m - 1:
if b[i + 1] - a[i] > k:
k = b[i + 1] - a[i]
print(k)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
def solve(s, t):
left_indexs = []
right_indexs = []
counter = 0
ans = 0
for i, c in enumerate(s):
if counter + 1 == len(t):
break
if t[counter] == c:
left_indexs.append(i)
counter += 1
counter = len(t) - 1
for i, c in enumerate(s[::-1]):
if counter == 0:
break
if t[counter] == c:
right_indexs.append(len(s) - i - 1)
counter -= 1
for i in range(len(left_indexs)):
ans = max(ans, right_indexs[i] - left_indexs[len(left_indexs) - i - 1])
return ans
n = sys.stdin.readline().strip()
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
print(solve(s, t))
|
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
def a_to_i(x):
return ord(x) - ord("a")
cur = 0
l = []
for i in range(n):
if t[cur] == s[i]:
l.append(i)
cur += 1
if cur == m:
break
cur = m - 1
r = []
for i in range(n - 1, -1, -1):
if t[cur] == s[i]:
r.append(i)
cur -= 1
if cur == -1:
break
r.reverse()
ans = 0
for i in range(m - 1):
ans = max(ans, r[i + 1] - l[i])
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split(" "))
s = [w for w in input()]
t = [w for w in input()]
left = []
j = 0
for i in range(n):
if s[i] == t[j]:
left.append(i)
j = j + 1
if j >= m:
break
right = []
i = n - 1
j = m - 1
while i >= 0:
if s[i] == t[j]:
right.append(i)
j = j - 1
if j < 0:
break
i = i - 1
right.reverse()
if m > 2:
ans = 0
prev = left[0]
for i in range(1, m - 1):
ans = max(ans, right[i] - prev)
prev = left[i]
ans = max(ans, right[-1] - left[-2])
else:
ans = right[-1] - left[0]
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
from sys import stdin, stdout
input = stdin.readline
def main():
n, m = map(int, input().split())
s = input()
t = input()
ar = [0] * m
ar2 = [0] * m
ind = 0
for i in range(m):
while s[ind] != t[i]:
ind += 1
ar[i] = ind
ind += 1
ind = n - 1
for i in range(m - 1, -1, -1):
while s[ind] != t[i]:
ind -= 1
ar2[i] = ind
ind -= 1
ans = 0
for i in range(1, m):
ans = max(ans, ar[i] - ar[i - 1])
ans = max(ans, ar2[i] - ar[i - 1])
ans = max(ans, ar[i] - ar2[i - 1])
ans = max(ans, ar2[i] - ar2[i - 1])
print(ans)
main()
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
ch = input()
L = [int(i) for i in ch.split()]
n, m = L[0], L[1]
c = input()
s = input()
g = [-1] * m
j = 0
for i in range(n):
if c[i] == s[j]:
g[j] = i
j += 1
if j >= m:
break
d = [-1] * m
j = m - 1
for i in range(n - 1, -1, -1):
if c[i] == s[j]:
d[j] = i
j -= 1
if j < 0:
break
maxx = 0
for i in range(1, m):
maxx = max(maxx, d[i] - g[i - 1])
print(maxx)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def findMaxWidth():
n, m = input().split(" ")
big = input()
small = input()
posdict = {}
for letter in small:
posdict[letter] = []
for index in range(len(big)):
if big[index] in posdict.keys():
posdict[big[index]].append(index)
maxi = 0
if len(posdict.keys()) > 1:
print(posdict)
for i in range(len(small) - 1):
while posdict[small[i]][-1] > posdict[small[i + 1]][-1]:
posdict[small[i]].pop()
print(posdict)
for i in range(len(small) - 1):
width = posdict[small[i + 1]][-1] - posdict[small[i]][0]
if width > maxi:
maxi = width
else:
maxi = posdict[small[0]][-(len(small) - 1)] - posdict[small[0]][0]
print(maxi)
def ans():
n, m = map(int, input().split())
s = list(input())
t = list(input())
pre = [0] * m
suf = [0] * m
j = 0
for i in range(m):
while s[j] != t[i]:
j += 1
pre[i] = j
j += 1
j = n - 1
for i in range(m - 1, -1, -1):
while s[j] != t[i]:
j -= 1
suf[i] = j
j -= 1
ans = 0
for i in range(m - 1):
ans = max(ans, suf[i + 1] - pre[i])
print(ans)
ans()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
for u in range(1):
n, m = map(int, input().split())
s = input()
t = input()
left = []
right = []
p = 0
for i in range(n):
if s[i] == t[p]:
left.append(i)
p += 1
if p == m:
break
p = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[p]:
right.append(i)
p -= 1
if p == -1:
break
right = right[::-1]
ans = -1
for i in range(1, m):
ans = max(ans, right[i] - left[i - 1])
print(ans)
|
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solve(n, m, s, t):
l = [-1] * (m + 1)
for i in range(m):
for j in range(l[i - 1] + 1, n):
if s[j] == t[i]:
l[i] = j
break
r = [-1] * (m + 1)
r[-1] = n
for i in range(m - 1, -1, -1):
for j in range(r[i + 1] - 1, -1, -1):
if s[j] == t[i]:
r[i] = j
break
res = -1
for i in range(m - 1):
res = max(res, r[i + 1] - l[i])
print(res)
n, m = map(int, input().split())
s = input()
t = input()
solve(n, m, s, t)
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def answer():
l, r = [0] * (n + 1), [0] * (n + 1)
ind = 0
for i in range(n):
if s1[i] == s2[ind]:
l[ind] = i
ind += 1
if ind == m:
break
ind = m - 1
for i in range(n - 1, -1, -1):
if s1[i] == s2[ind]:
r[ind] = i
ind -= 1
if ind < 0:
break
ans = 0
for i in range(n):
ans = max(ans, r[i + 1] - l[i])
return ans
n, m = map(int, input().split())
s1 = input()
s2 = input()
print(answer())
|
FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def arrIn():
return list(map(int, input().split()))
def mapIn():
return map(int, input().split())
n, m = mapIn()
s = input()
t = input()
minrr = [0] * m
maxrr = [0] * m
i = 0
for j in range(m):
while s[i] != t[j] and i < n:
i += 1
minrr[j] = i
i += 1
i = n - 1
for j in range(m - 1, -1, -1):
while s[i] != t[j] and i > 0:
i -= 1
maxrr[j] = i
i -= 1
x = 0
for i in range(1, m):
y = maxrr[i] - minrr[i - 1]
if y > x:
x = y
print(x)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def solution():
n, m = map(int, input().split())
s, t = input(), input()
ans = 0
left_right = [[(-1) for i in range(2)] for j in range(m)]
index = 0
for i in range(m):
while s[index] != t[i]:
index += 1
left_right[i][0] = index
index += 1
index = n - 1
for j in range(m - 1, -1, -1):
while s[index] != t[j]:
index -= 1
left_right[j][1] = index
index -= 1
for a in range(m - 1):
ans = max(ans, left_right[a + 1][1] - left_right[a][0])
print(ans)
solution()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
n, m = map(int, input().split())
a = input()
b = input()
fc = []
t = 0
for i in b:
while t < n and a[t] != i:
t += 1
fc.append(t)
t += 1
t = n - 1
bc = []
for i in reversed(b):
while t >= 0 and a[t] != i:
t -= 1
bc.append(t)
t -= 1
ans = 0
bc = bc[::-1]
for i in range(m - 1):
ans = max(ans, bc[i + 1] - fc[i])
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
l = [0] * m
r = [0] * m
i = 0
j = 0
while j < m:
if s[i] == t[j]:
l[j] = i
j += 1
i += 1
i = n - 1
j = m - 1
while j > 0:
if s[i] == t[j]:
r[j] = i
j -= 1
i -= 1
print(max(r[i + 1] - l[i] for i in range(m - 1)))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
mn = [0] * m
mx = [0] * m
p = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[p]:
mx[p] = i
p -= 1
if p == -1:
break
p = 0
for i in range(n):
if s[i] == t[p]:
mn[p] = i
p += 1
if p == m:
break
out = 0
for i in range(1, m):
out = max(out, mx[i] - mn[i - 1])
print(out)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
class Solution:
def solution(self, str1, str2):
n, m = len(str1), len(str2)
first_position = [0] * m
last_position = [0] * m
i = 0
for j in range(m):
while i < n and str1[i] != str2[j]:
i += 1
if i >= n:
break
first_position[j] = i
i += 1
i = n - 1
for j in range(m - 1, -1, -1):
while i > -1 and str1[i] != str2[j]:
i -= 1
if i < 0:
break
last_position[j] = i
i -= 1
result = 1
for i in range(m - 1):
result = max(result, last_position[i + 1] - first_position[i])
return result
n, m = map(int, input().split())
str1 = input()
str2 = input()
sol = Solution()
print(sol.solution(str1, str2))
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
def read_ints():
return [int(i) for i in sys.stdin.readline().strip().split()]
def width(s, t):
n = len(s)
m = len(t)
firsts = []
j = 0
for i, c in enumerate(t):
while s[j] != c:
j += 1
firsts.append(j)
j += 1
lasts = []
j = n - 1
for i in range(m - 1, -1, -1):
c = t[i]
while s[j] != c:
j -= 1
lasts.append(j)
j -= 1
lasts.reverse()
return max(a - b for a, b in zip(lasts[1:], firsts[:-1]))
n, m = read_ints()
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
print(width(s, t))
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
__version__ = "3.0"
__date__ = "2021-03-08"
def solve(n, m, s, t):
left_pos = [-1] * m
right_pos = [-1] * m
curr = 0
for pos in range(n):
if curr == m:
break
if s[pos] == t[curr]:
left_pos[curr] = pos
curr += 1
curr = m - 1
for pos in range(n - 1, -1, -1):
if curr == -1:
break
if s[pos] == t[curr]:
right_pos[curr] = pos
curr -= 1
answer = 1
for i in range(m - 1):
answer = max(answer, right_pos[i + 1] - left_pos[i])
return answer
def main(argv=None):
n, m = map(int, input().split())
s = input()
t = input()
print(solve(n, m, s, t))
return 0
STATUS = main()
sys.exit(STATUS)
|
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
earliestMatchingIndices = []
j = 0
for c in t:
while s[j] != c:
j += 1
earliestMatchingIndices.append(j)
j += 1
latestMatchingIndices = [0] * m
j = n - 1
for i in range(m - 1, -1, -1):
while s[j] != t[i]:
j -= 1
latestMatchingIndices[i] = j
j -= 1
maxWidth = 0
for i in range(0, m - 1):
maxWidth = max(latestMatchingIndices[i + 1] - earliestMatchingIndices[i], maxWidth)
print(maxWidth)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def sol(s, t):
n = len(s)
m = len(t)
first = []
j1 = 0
for i in range(n):
if s[i] == t[j1]:
j1 += 1
first.append(i)
if j1 == m:
break
last = []
j1 = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[j1]:
j1 -= 1
last.append(i)
if j1 == -1:
break
answer = 0
for i in range(m - 1):
a1 = first[i]
a2 = last[m - (i + 1) - 1]
answer = max(answer, a2 - a1)
return answer
n, m = map(int, input().split())
s = input()
t = input()
print(sol(s, t))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
a, b, q, w, e = input(), input(), [], [], 0
for i in range(n):
if b[e] == a[i]:
q += [i]
e += 1
if e == m:
break
e -= 1
for i in range(n - 1, -1, -1):
if b[e] == a[i]:
w += [i]
e -= 1
if e == -1:
break
w = w[::-1]
print(max(w[i + 1] - q[i] for i in range(m - 1)))
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR LIST VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR LIST VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def main():
n, m = map(int, input().split())
s = input()
t = input()
a = ["" for i in range(m)]
b = ["" for i in range(m)]
i = 0
j = 0
while i < m:
while t[i] != s[j]:
j += 1
a[i] = j
i += 1
j += 1
t = t[::-1]
s = s[::-1]
j = 0
i = 0
while i < m:
while t[i] != s[j]:
j += 1
b[i] = n - 1 - j
i += 1
j += 1
b = b[::-1]
print(max(b[i + 1] - a[i] for i in range(m - 1)))
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
def width(a, b):
m = len(b)
idx = 0
l = [0] * (2 * 10**5 + 5)
r = [0] * (2 * 10**5 + 5)
for i in range(len(a)):
if a[i] == b[idx]:
l[idx] = i
idx += 1
if idx == m:
break
idx = m - 1
for i in range(len(a) - 1, -1, -1):
if a[i] == b[idx]:
r[idx] = i
idx -= 1
if idx < 0:
break
ans = 0
for i in range(len(b) - 1):
ans = max(ans, r[i + 1] - l[i])
return ans
n = input()
a = input()
b = input()
print(width(a, b))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split())
s = input()
t = input()
start = 1000000 * [0]
end = 1000000 * [0]
j = 0
for i in range(n):
if j == m:
break
if t[j] == s[i]:
start[j] = i
j += 1
j = m - 1
for i in range(n - 1, -1, -1):
if j == -1:
break
if t[j] == s[i]:
end[j] = i
j -= 1
ans = -1
for i in range(1, m):
ans = max(ans, abs(end[i] - start[i - 1]))
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER LIST NUMBER ASSIGN VAR BIN_OP NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
n, m = map(int, input().split(" "))
s = input()
t = input()
minm = [0] * m
i = 0
for j in range(n):
if i == m:
break
if s[j] == t[i]:
minm[i] = j
i += 1
maxm = [0] * m
i = m - 1
for j in range(n - 1, -1, -1):
if i == -1:
break
if s[j] == t[i]:
maxm[i] = j
i -= 1
mx = -1
for i in range(1, m):
if maxm[i] - minm[i - 1] > mx:
mx = maxm[i] - minm[i - 1]
print(mx)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
answers = []
def solve(n, m, s, t):
if n == m:
print(1)
return
first = []
second = []
current = 0
for i in range(n):
if s[i] == t[current]:
first.append(i)
current += 1
if current == m:
break
current = m - 1
for i in range(n - 1, -1, -1):
if s[i] == t[current]:
second.append(i)
current -= 1
if current == -1:
break
second.reverse()
ans = 0
for i in range(1, m):
current = max(
first[i] - first[i - 1],
first[i] - second[i - 1],
second[i] - second[i - 1],
second[i] - first[i - 1],
)
ans = max(ans, current)
print(ans)
n, m = [int(x) for x in input().split()]
s = input()
t = input()
solve(n, m, s, t)
|
ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$.
A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$.
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence.
-----Input-----
The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) — the lengths of the strings $s$ and $t$.
The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
-----Output-----
Output one integer — the maximum width of a beautiful sequence.
-----Examples-----
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
-----Note-----
In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$.
In the second example the beautiful sequence with the maximum width is $\{1, 5\}$.
In the third example there is exactly one beautiful sequence — it is $\{1, 2, 3, 4, 5\}$.
In the fourth example there is exactly one beautiful sequence — it is $\{1, 2\}$.
|
import sys
def solve(s, t, n, m):
P_min = []
P_max = []
i = 0
j = 0
while i < n and j < m:
if s[i] == t[j]:
P_min.append(i)
j += 1
i += 1
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
P_max.append(i)
j -= 1
i -= 1
P_max = P_max[::-1]
max_val = 1
for i in range(len(P_max) - 1):
max_val = max(max_val, P_max[i + 1] - P_min[i])
return max_val
if 1 == 2:
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
else:
input = sys.stdin.readline
n, m = list(map(int, input().split()))
s = input()
t = input()
print(solve(s, t, n, m))
|
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR IF NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.