description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | i_speed, f_speed = input().split(" ")
time, transition = input().split(" ")
i_speed = int(i_speed)
f_speed = int(f_speed)
time = int(time)
transition = int(transition)
path = [i_speed] * time
for i in range(1, len(path)):
path[i] += transition * i
while path[i] > f_speed + transition * (len(path) - i - 1):
path[i] -= 1
print(sum(path)) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | def solve(v1, v2, t, d):
cache = {}
def calc_max(cur_v, remain_t):
if remain_t == 0:
return 0
cur_dist = float("-inf")
if remain_t == 1 and cur_v != v2:
return cur_dist
cache_key = cur_v, remain_t
if cache_key in cache:
return cache[cache_key]
max_v = min(cur_v + d, v2 + d * remain_t)
min_v = max(0, max(cur_v - d, v2 - d * remain_t))
for v in range(max_v, min_v - 1, -1):
cur_dist = max(cur_dist, calc_max(v, remain_t - 1))
cache[cache_key] = cur_v + cur_dist
return cur_v + cur_dist
return calc_max(v1, t)
def main():
v1, v2 = map(int, input().strip().split())
t, d = map(int, input().strip().split())
print(solve(v1, v2, t, d))
main() | FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = list(map(int, input().split()))
t, d = list(map(int, input().split()))
if v1 > v2:
v1, v2 = v2, v1
if d == 0:
print(t * v1)
else:
a = [i for i in range(v1, v1 + t * d, d)]
b = [i for i in range(v2, v2 + t * d, d)]
b.reverse()
i = 0
answer = 0
while i < t - 1:
if abs(a[i] - b[i + 1]) <= d:
if sum(a[: i + 1]) + sum(b[i + 1 :]) > answer:
answer = sum(a[: i + 1]) + sum(b[i + 1 :])
i += 1
print(answer) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = list(map(int, input().split()))
t, d = list(map(int, input().split()))
ans = [0] * t
ans[0] = v1
ans[-1] = v2
n = t
for i in range(1, n - 1):
if ans[-1] < ans[i - 1] + d:
if ans[i - 1] + d - ans[-1] <= d * (n - 1 - i):
ans[i] = ans[i - 1] + d
else:
ans[i] = ans[-1] + d * (n - 1 - i)
else:
ans[i] = ans[i - 1] + d
print(sum(ans)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | def main():
v1, v2 = (int(i) for i in input().split())
t, d = (int(i) for i in input().split())
res = 0
for i in range(t):
res += min(v1 + d * i, v2 + d * (t - i - 1))
print(res)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = map(int, input().split())
t, d = map(int, input().split())
dst = v1
for i in range(2, t + 1):
for x in range(d):
if abs(v1 + 1 - v2) <= d * (t - i):
v1 += 1
elif abs(v1 - v2) > d * (t - i):
if v1 > v2:
v1 -= 1
else:
v1 += 1
dst += v1
print(dst) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
for _ in range(1):
v1, v2 = lst()
n, d = lst()
N = 1001
dp = [[(0) for _ in range(N)] for _ in range(n)]
dp[0][v1] = v1
for i in range(1, n - 1):
for j in range(N):
for diff in range(-d, d + 1):
if 0 <= j + diff < N and dp[i - 1][j + diff] != 0:
dp[i][j] = max(dp[i][j], dp[i - 1][j + diff] + j)
for diff in range(-d, d + 1):
if v2 + diff >= 0:
dp[n - 1][v2] = max(dp[n - 1][v2], dp[n - 2][v2 + diff] + v2)
print(dp[n - 1][v2]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | a, b = map(int, input().split())
t, d = map(int, input().split())
first = [0] * t
first[0] = a
for i in range(1, t):
first[i] = first[i - 1] + d
second = [0] * t
second[0] = b
for i in range(1, t):
second[i] = second[i - 1] + d
second = second[::-1]
final = [0] * t
for i in range(t):
final[i] = min(first[i], second[i])
print(sum(final)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = map(int, input().split(" "))
a, b = map(int, input().split(" "))
ans = 0
z = [v1]
x = [v2]
for i in range(a - 2):
z.append(z[-1] + b)
x.append(x[-1] + b)
x.append(v1)
z.append(v2)
x.reverse()
for i in range(a):
ans += min(x[i], z[i])
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = [int(x) for x in input().split()]
t, d = [int(x) for x in input().split()]
pre1 = [v1]
pre2 = [v2]
for i in range(1, t):
pre1.append(pre1[i - 1] + d)
for i in range(1, t):
pre2.append(pre2[i - 1] + d)
pre2 = pre2[::-1]
ans = 0
for i in range(t):
ans += min(pre1[i], pre2[i])
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | vs = input().split()
v1 = int(vs[0])
v2 = int(vs[1])
vals = input().split()
t = int(vals[0])
d = int(vals[1])
a = [(0) for i in range(t + 1)]
index = t
maxspeed = v2
while index > 0:
a[index] = maxspeed
index = index - 1
maxspeed += d
answer = v1
current = v1
index = 2
done = False
while index <= t and not done:
if current + d <= a[index]:
current = current + d
answer = answer + current
index = index + 1
else:
current = a[index]
answer = answer + current
index = index + 1
done = True
while index <= t:
current = current - d
answer = answer + current
index = index + 1
print(answer) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = map(int, input().split())
t, d = map(int, input().split())
res = 0
for i in range(1, t + 1):
r1 = v1 + (i - 1) * d
r2 = v2 + (t - i) * d
res += min(r1, r2)
print(res) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | v1, v2 = map(int, input().split())
t, d = map(int, input().split())
v = v1
ans = v1
for i in range(1, t):
delta = v2 - v + (t - i - 1) * d
v += min(d, delta)
ans += v
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | def main():
v1, v2 = map(int, input().split())
t, d = map(int, input().split())
print(
sum(
min(vv)
for vv in zip(range(v1, v1 + t * d, d), range(v2 + (t - 1) * d, v2 - 1, -d))
)
if d
else v1 * t
)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR |
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
-----Input-----
The first line contains two integers v_1 and v_2 (1 β€ v_1, v_2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
-----Output-----
Print the maximum possible length of the path segment in meters.
-----Examples-----
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
-----Note-----
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | a, b = map(int, input().split())
v1, v2 = min(a, b), max(a, b)
t, d = map(int, input().split())
dist = 0
vn = v1
t -= 1
j = 0
a = 0
r1 = 0
while j < t and vn - (t - j) * d <= v2:
vn += d
j += 1
a = 1
r1 += 1
vd = vn
r1 -= a
x = 0
b = t > r1 + 1
while x < d and vd - (t - j) * d > v2:
vd -= 1
x += 1
dist = (
(r1 + 1) * v1
+ (r1 + 1) * r1 * d // 2
+ (t - j + 1) * v2
+ (t - j) * (t - j + 1) * d // 2
)
if t == 1:
dist = v1 + v2
if d == 0:
dist = v1 * (t + 1)
print(dist) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, β¦, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 β€ n β€ 10^6) β the number of stages, r_1, r_2, r_3 (1 β€ r_1 β€ r_2 β€ r_3 β€ 10^9) β the reload time of the three guns respectively, d (1 β€ d β€ 10^9) β the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6, 1 β€ i β€ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1β
3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1β
2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1β
1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | import sys
def solve(lst, r1, r2, r3, d):
console("----- solving ------")
const_r2_r1_d = r2 + r1 + d
const_2r1_d = 2 * r1 + d
m1_cost = [(r1 * x + r3) for x in lst]
m4_cost = [min(r1 * x + const_2r1_d, const_r2_r1_d) for x in lst]
baseline = sum(m1_cost) + (len(lst) - 1) * d
cost_diff = [(a - b) for a, b in zip(m1_cost, m4_cost)]
del m1_cost
del m4_cost
savings = [[0, 0] for _ in lst]
savings[0][1] = max(cost_diff[0], -d)
for i in range(1, len(lst)):
savings[i][0] = max(
savings[i - 1][0], savings[i - 1][1] + cost_diff[i], savings[i - 1][1] - d
)
savings[i][1] = max(savings[i - 1][0] + cost_diff[i], savings[i - 1][0] - d)
total_savings = max(0, savings[-2][1], savings[-1][0], savings[-1][1] - d)
return baseline - total_savings
def console(*args):
return
inp = sys.stdin.readlines()
for _ in [1]:
_, r1, r2, r3, d = list(map(int, inp[0].split()))
lst = list(map(int, inp[1].split()))
res = solve(lst, r1, r2, r3, d)
print(res) | IMPORT FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP VAR VAR FUNC_DEF RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR LIST NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, β¦, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 β€ n β€ 10^6) β the number of stages, r_1, r_2, r_3 (1 β€ r_1 β€ r_2 β€ r_3 β€ 10^9) β the reload time of the three guns respectively, d (1 β€ d β€ 10^9) β the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6, 1 β€ i β€ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1β
3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1β
2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1β
1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n, r1, r2, r3, d = map(int, input().split())
a = list(map(int, input().split()))
dp = [0, float("inf")]
for i in range(1, n + 1):
na = a[i - 1]
ndp = [float("inf"), float("inf")]
c1 = r1 * na + r3
c2 = min(r1 * (na + 2), r2 + r1)
if i != n:
ndp[0] = min(dp[0] + c1, dp[1] + min(c1, c2))
ndp[1] = dp[0] + c2 + 2 * d
if i == n:
print(
min(dp[0] + c1, dp[0] + c2 + 2 * d, dp[1] + c2, dp[1] + c1 - d)
+ d * (n - 1)
)
dp = ndp | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR |
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, β¦, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 β€ n β€ 10^6) β the number of stages, r_1, r_2, r_3 (1 β€ r_1 β€ r_2 β€ r_3 β€ 10^9) β the reload time of the three guns respectively, d (1 β€ d β€ 10^9) β the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6, 1 β€ i β€ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1β
3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1β
2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1β
1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | N, R1, R2, R3, D = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0, 0] for _ in A]
dp[0][0] = R1 * A[0] + R3
dp[0][1] = min(R2, R1 * A[0] + R1)
for i, a in enumerate(A[1:-1], 1):
dp[i][0] = min(
dp[i - 1][0] + D + R1 * A[i] + R3,
dp[i - 1][1]
+ D
+ min(
R1 * A[i] + R3 + 2 * D + R1,
R2 + 2 * D + 2 * R1,
R1 * A[i] + 2 * R1 + 2 * D + R1,
),
)
dp[i][1] = min(
dp[i - 1][0] + D + min(R1 * A[i] + R1, R2),
dp[i - 1][1] + D + min(R1 * A[i] + 2 * D + 2 * R1, R2 + 2 * D + R1),
)
dp[-1][0] = min(
dp[-2][0] + D + min(R1 * A[-1] + R3, R2 + 2 * D + R1, R1 * A[-1] + 2 * R1 + 2 * D),
dp[-2][1]
+ D
+ min(
R1 * A[-1] + R3 + D + R1, R2 + 2 * D + 2 * R1, R1 * A[-1] + R1 + 2 * D + 2 * R1
),
)
print(dp[-1][0]) | ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER |
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, β¦, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 β€ n β€ 10^6) β the number of stages, r_1, r_2, r_3 (1 β€ r_1 β€ r_2 β€ r_3 β€ 10^9) β the reload time of the three guns respectively, d (1 β€ d β€ 10^9) β the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6, 1 β€ i β€ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1β
3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1β
2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1β
1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | import sys
input = sys.stdin.readline
def snipe(i):
global r1, r2, r3, arr
return r1 * arr[i] + r3
def laser(i):
global r1, r2, r3, arr
return min(r1 * (arr[i] + 1), r2)
n, r1, r2, r3, d = list(map(int, input().split()))
arr = list(map(int, input().split()))
dp = [[sys.maxsize for j in range(2)] for i in range(n)]
dp[0][0] = snipe(0) + d
dp[0][1] = laser(0) + d
for i in range(1, n - 1):
dp[i][0] = min(
dp[i - 1][0] + snipe(i) + d, dp[i - 1][1] + laser(i) + 2 * r1 + 3 * d
)
dp[i][1] = dp[i - 1][0] + laser(i) + d
i = n - 1
ans = min(
dp[i - 1][0] + snipe(i),
dp[i - 1][1] + laser(i) + 2 * r1 + 2 * d,
dp[i - 1][1] + snipe(i) + r1 + d,
dp[i - 1][0] + laser(i) + 2 * d + r1,
)
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
if (a + b) % 2 != 0:
print(-1)
else:
print((a - b) // 2, (a + b) // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A = int(input())
B = int(input())
if B > A or (A + B) % 2 != 0:
print(-1)
else:
C = A - B
C //= 2
if B & C == 0:
print(C, A - C)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | x = int(input())
y = int(input())
if (x - y) % 2 == 0:
print((x - y) // 2, (x + y) // 2, sep=" ", end="\n")
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
if a >= b:
if (a - b) % 2 == 1:
print(-1)
exit(0)
f = b
s = (a - b) // 2
t = s
if f ^ s == f + s:
print(t, end=" ")
print(f + s)
exit(0)
if f ^ t == f + t:
print(s, end=" ")
print(f + t)
exit(0)
if t ^ s == t + s:
print(f, end=" ")
print(t + s)
exit(0)
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A = int(input())
B = int(input())
x = (A - B) // 2
y = x ^ B
if x + y == A and x ^ y == B:
print(x, y)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a, b = int(input()), int(input())
x, y = a - b >> 1, (a - b) // 2 + b
if a < b or a + b & 1 or x & a - x != x:
print("-1")
else:
print(x, y) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
if a < b or (a - b) % 2 != 0:
print(-1)
else:
v = (a - b) // 2
x = 0
y = 0
for i in range(64):
if v & 1 << i != 0:
x += 1 << i
y += 1 << i
ok = 1
for i in range(64):
if b & 1 << i:
if y & 1 << i:
ok = 0
break
else:
y += 1 << i
if ok == 0:
print(-1)
else:
print(x, end=" ")
print(y) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | S = int(input())
X = int(input())
A = (S - X) // 2
a = 0
b = 0
ok = 1
for i in range(64):
xnow = X & 1 << i
anow = A & 1 << i
if xnow == 0 and anow == 0:
pass
elif xnow > 0 and anow == 0:
a = 1 << i | a
elif xnow == 0 and anow > 0:
a = 1 << i | a
b = 1 << i | b
else:
print(-1)
ok = 0
if ok == 1:
print(b)
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A = int(input())
B = int(input())
if A < B:
T = A
A = B
B = T
X = A - B
if X % 2 == 1:
print(-1)
else:
X //= 2
print("{0} {1}".format(int(X), int(A - X))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
A = a
x = 0
y = 0
for i in range(64):
if b & 1 << i != 0:
y |= 1 << i
elif a & 1 << i + 1 != b & 1 << i + 1:
a ^= 1 << i + 1
x |= 1 << i
y |= 1 << i
if x ^ y == b and x + y == A:
print(x, y)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | _a = int(input())
_b = int(input())
A = bin(_a)[2:]
B = bin(_b)[2:]
if len(A) < len(B):
A, B = B, A
B = "0" * (len(A) - len(B)) + B
X = ""
Y = ""
need_carry = False
for a, b in zip(A, B):
if a + b == "11":
X += "0"
Y += "1"
elif a + b == "10":
if need_carry:
X += "1"
Y += "1"
need_carry = True
else:
X += "0"
Y += "0"
need_carry = True
elif a + b == "01":
X += "0"
Y += "1"
elif a + b == "00":
if need_carry:
X += "1"
Y += "1"
need_carry = False
else:
X += "0"
Y += "0"
need_carry = False
x = int(X, base=2)
y = int(Y, base=2)
if x + y == _a and x ^ y == _b:
print(x, y)
else:
print("-1") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR STRING VAR STRING VAR STRING IF BIN_OP VAR VAR STRING IF VAR VAR STRING VAR STRING ASSIGN VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR STRING VAR STRING VAR STRING IF BIN_OP VAR VAR STRING IF VAR VAR STRING VAR STRING ASSIGN VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A, B = int(input()), int(input())
if A < B:
print(-1)
X = (A - B) // 2
Y = X + B
if X + Y != A or X ^ Y != B:
print(-1)
else:
print(X, Y) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A = int(input())
B = int(input())
if (A - B) % 2 != 0:
print(-1)
exit()
XAY = A - B >> 1
res = 0
r1 = XAY
r2 = A - r1
if r1 < 0 or r2 < 0:
print(-1)
exit()
if A != r1 + r2 or B != r1 ^ r2:
print(-1)
exit()
print(r1, r2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | import sys
def solve():
(a,) = rv()
(b,) = rv()
bbin = bin(b)[::-1]
bl, x, y = [0] * 64, [0] * 64, [0] * 64
for i in range(len(bbin) - 2):
bl[i] = 1 if bbin[i] == "1" else 0
for i in range(64):
count = 0
if bl[i] == 1:
count += 1
if count == 1:
y[i] = 1
else:
pass
x[i] = 2
y[i] = 2
xres, yres = 0, 0
for i in range(64):
xres += pow(2, i) if x[i] == 1 else 0
yres += pow(2, i) if y[i] == 1 else 0
for i in range(63, -1, -1):
if x[i] == y[i] == 2:
toadd = 2 * pow(2, i)
if xres + yres + toadd <= a:
x[i] = y[i] = 1
xres += toadd // 2
yres += toadd // 2
else:
x[i] = y[i] = 0
if xres + yres == a:
print(xres, yres)
else:
print(-1)
def prt(l):
return print(" ".join(l))
def rv():
return map(int, input().split())
def rl(n):
return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544:
sys.stdin = open("test.txt")
solve() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
q = a - b
if q >= 0 and q % 2 == 0:
q = (a - b) // 2
x = bin(b)[2:][::-1]
y = bin(q)[2:][::-1]
if len(x) > len(y):
s = ["0"] * (len(x) - len(y))
y += "".join(s)
else:
s = ["0"] * (len(y) - len(x))
x += "".join(s)
j = 0
f = 0
s = ""
l = len(x)
while j < l:
if x[j] == "0" and y[j] == "0":
s += "0"
elif x[j] == "0" and y[j] == "1":
s += "1"
elif x[j] == "1" and y[j] == "0":
s += "0"
else:
f = 1
break
j += 1
if f == 1:
print(-1)
else:
m = int(s[::-1], 2)
print(m, a - m)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
if a < b:
print(-1)
else:
nda = a - b >> 1
xrr = b
num1 = 0
num2 = 0
flag = True
for i in range(0, 64):
bit1 = nda >> i & 1
bit2 = xrr >> i & 1
if bit1 == 0 and bit2 == 0:
num1 += 0
num2 += 0
elif bit1 == 1 and bit2 == 0:
num1 += 1 << i
num2 += 1 << i
elif bit1 == 0 and bit2 == 1:
num2 += 1 << i
elif bit1 == 1 and bit2 == 1:
flag = False
break
if flag:
print(num1, num2, sep=" ")
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
if (a - b) % 2 == 1:
print("-1")
exit(0)
x = (a - b) // 2
y = a - x
print(x, y) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
c = (a - b) // 2
if a < b:
print(-1)
elif c & a - c != c:
print(-1)
else:
print(str(c) + " " + str(a - c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | S = int(input())
X = int(input())
if S < X or (S - X) % 2 == 1:
print("-1")
else:
print((S - X) // 2, end=" ")
print(X ^ (S - X) // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | A = int(input())
B = int(input())
if A < B:
print(-1)
else:
v = A - B
if v % 2:
print(-1)
else:
v //= 2
if B & v:
print(-1)
else:
print(f"{v} {v + B}") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR VAR |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
x = int((a - b) / 2)
y = a - x
if 2 * x + b == a:
print("{} {}".format(x, y))
else:
print("-1") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR STRING |
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:
X xor Y = 6810 = 10001002.
Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:
* A = X + Y
* B = X xor Y, where xor is bitwise exclusive or.
* X is the smallest number among all numbers for which the first two conditions are true.
Input
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
Output
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
Examples
Input
142
76
Output
33 109 | a = int(input())
b = int(input())
k = a - b
if k % 2:
print(-1)
exit(0)
k >>= 1
if k < 0:
print(-1)
exit(0)
print(k, a - k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def solve_simple(source, target):
if not set(source) >= set(target):
return -1
operations = 0
target_index = 0
while target_index < len(target):
operations += 1
source_index = 0
while source_index < len(source) and target_index < len(target):
if source[source_index] == target[target_index]:
target_index += 1
source_index += 1
return operations
def solve_optimal(source, target):
if not set(source) >= set(target):
return -1
positions = {}
next_positions = {}
i = len(source) - 1
while i >= 0:
positions[source[i]] = i
for char, position in positions.items():
next_positions[i, char] = position
i -= 1
operations = 0
target_index = 0
while target_index < len(target):
operations += 1
position = 0
while target_index < len(target):
position = next_positions.get((position, target[target_index]))
if position is None:
break
position += 1
target_index += 1
return operations
solve = solve_optimal
for T in range(int(input())):
print(solve(input(), input())) | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | from sys import stdin, stdout
def bs(nums, target):
low = 0
high = len(nums) - 1
while low < high:
m = (high + low) // 2
if nums[m] <= target:
low = m + 1
else:
high = m
if low == len(nums):
return -1
if nums[low] <= target:
return -1
return nums[low]
def main():
casos = int(input())
for c in range(casos):
s = input()
t = input()
A = set(s)
B = set(t)
sw = 1
Graf = []
for i in range(30):
Graf.append([])
for x in B:
if x == "\n":
continue
if x not in A:
sw = 0
break
if sw:
for j in range(len(s)):
if s[j] == "\n":
continue
pos = ord(s[j]) - ord("a")
Graf[pos].append(j + 1)
ans = 0
i = 0
n = len(t)
while i < n:
ans += 1
pos = 0
while i < n:
tmp = ord(t[i]) - ord("a")
tmp = bs(Graf[tmp], pos)
if i == n or tmp == -1:
break
pos = tmp
i += 1
print(ans)
else:
print(-1)
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
def main():
d = {}
for i in range(26):
d[chr(i + ord("a"))] = i
for _ in range(int(input())):
s = input().strip()
t = input().strip()
s1 = set(s)
s2 = set(t)
if not s2.issubset(s1):
print(-1)
continue
n = len(s)
l = [[(10**9 + 7) for i in range(26)] for j in range(n)]
for i in range(n - 1, 0, -1):
for j in range(26):
l[i - 1][j] = l[i][j]
l[i - 1][d[s[i]]] = i
ans = 0
curr = 0
i = 0
while i < len(t):
if curr == 10**9 + 7:
ans += 1
curr = 0
if t[i] == s[curr]:
i += 1
if i == len(t):
ans += 1
break
curr = l[curr][d[t[i]]]
else:
curr = l[curr][d[t[i]]]
print(ans)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
s = input().rstrip()
t = input().rstrip()
L = len(s)
dp = []
cur_min = [-1] * 26
for i in range(L):
x = s[L - 1 - i]
xx = ord(x) - 97
cur_min[xx] = L - 1 - i
dp.append(cur_min[:])
dp.reverse()
ans = 1
ALL = set(s)
for x in t:
if x not in ALL:
ans = -1
if ans < 0:
print(ans)
continue
cur = 0
for x in t:
xx = ord(x) - 97
if cur == L or dp[cur][xx] < 0:
cur = 0
ans += 1
cur = dp[cur][xx] + 1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
s = input().strip()
t = input().strip()
LEN = len(s)
NEXT = [([-1] * 26) for i in range(LEN + 1)]
for i in range(LEN - 1, -1, -1):
for j in range(26):
NEXT[i][j] = NEXT[i + 1][j]
NEXT[i][ord(s[i]) - 97] = i
ANS = 0
ind = 0
indt = 0
LENT = len(t)
while indt < LENT:
k = t[indt]
if ind == 0 and NEXT[ind][ord(k) - 97] == -1:
print(-1)
break
elif NEXT[ind][ord(k) - 97] == -1:
ANS += 1
ind = 0
else:
ind = NEXT[ind][ord(k) - 97] + 1
indt += 1
else:
print(ANS + 1) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
def main():
import sys
input = sys.stdin.readline
for _ in range(int(input())):
S = input().rstrip("\n")
T = input().rstrip("\n")
N = len(S)
nxt = [([N + 1] * 26) for _ in range(N + 1)]
for i in range(N, 0, -1):
s = S[i - 1]
for j in range(26):
c = chr(j + 97)
if c == s:
nxt[i - 1][j] = i
else:
nxt[i - 1][j] = nxt[i][j]
seen = {}
for s in S:
seen[s] = 1
ans = 1
i = 0
for t in T:
t_ = ord(t) - 97
if t not in seen:
ans = -1
break
i = nxt[i][t_]
if i == N + 1:
ans += 1
i = nxt[0][t_]
print(ans)
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | ALPHA = 26
def offset(c):
return ord(c) - ord("a")
T = int(input())
for cs in range(T):
s = list(map(offset, list(input())))
t = list(map(offset, list(input())))
ls = len(s)
nxt = [[(-1) for j in range(ALPHA)] for i in range(ls)]
nxt[ls - 1][s[ls - 1]] = ls - 1
for i in range(ls - 2, -1, -1):
for j in range(ALPHA):
nxt[i][j] = nxt[i + 1][j]
nxt[i][s[i]] = i
cnt = 1
cur = 0
for c in t:
if cur >= ls:
cur = 0
cnt += 1
if nxt[cur][c] == -1:
cur = 0
cnt += 1
if nxt[cur][c] == -1:
cnt = -1
break
cur = nxt[cur][c] + 1
print(cnt) | ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = list(input())
t = list(input())
n = len(s)
pos = [[(-1) for i in range(26)] for i in range(n)]
for i in range(n - 1, -1, -1):
if i + 1 < n:
pos[i] = pos[i + 1].copy()
pos[i][ord(s[i]) - ord("a")] = i
ans = 1
prev = 0
n1 = len(t)
for i in range(n1):
ch = ord(t[i]) - ord("a")
if pos[0][ch] == -1:
ans = -1
break
if prev >= n:
ans += 1
prev = pos[0][ch] + 1
elif pos[prev][ch] == -1:
ans += 1
prev = pos[0][ch] + 1
else:
prev = pos[prev][ch] + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input())
for i in range(T):
s = input()
t = input()
n = len(s)
m = len(t)
d = []
for _ in range(26):
d.append([])
if set(s).intersection(set(t)) != set(t):
print(-1)
else:
for j in range(n):
while len(d[ord(s[j]) - 97]) < j + 1:
d[ord(s[j]) - 97].append(j)
for j in range(n):
d[ord(s[j]) - 97] += [-1] * (n - len(d[ord(s[j]) - 97]))
q = 0
k = 1
for j in range(m):
if q == n:
k += 1
q = d[ord(t[j]) - 97][0] + 1
elif d[ord(t[j]) - 97][q] == -1:
k += 1
q = d[ord(t[j]) - 97][0] + 1
elif q > d[ord(t[j]) - 97][q]:
k += 1
q = d[ord(t[j]) - 97][0] + 1
else:
q = d[ord(t[j]) - 97][q] + 1
print(k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = input()
t = input()
nxt = [([-1] * 26) for _ in range(len(s))]
exist = [False] * 26
get_code = lambda c: ord(c) - ord("a")
last = [-1] * 26
first = [-1] * 26
for i in range(len(s)):
c = s[i]
code = get_code(c)
exist[code] = True
if first[code] == -1:
first[code] = i
for j in range(i - 1, max(last[code] - 1, -1), -1):
nxt[j][code] = i
last[code] = i
if exist[get_code(t[0])]:
cur = len(s) - 1
loop = 0
for c in t:
code = get_code(c)
if not exist[code]:
loop = -1
break
if nxt[cur][code] == -1:
loop += 1
cur = -1
else:
cur = nxt[cur][code]
if cur == -1:
cur = first[code]
print(loop)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | from sys import stdin
input = stdin.readline
def printTable(u):
for _ in u:
print(*_)
def getAns(s, t):
l = len(s)
l2 = len(t)
rows, cols = l, 26
u = [[(-1) for i in range(cols)] for j in range(rows)]
currLetterIndices = [-1] * 26
for j in range(26):
if j in s:
ind = s.index(j)
currLetterIndices[j] = 0
for i in range(l):
u[i][j] = ind
for i in range(l):
currLetter = s[i]
for k in range(currLetterIndices[currLetter], i):
u[k][currLetter] = i
currLetterIndices[currLetter] = i
currIndex = -1
cycles = 1
for letter in t:
newIndex = u[currIndex][letter]
if newIndex <= currIndex:
cycles += 1
if newIndex == -1:
return -1
currIndex = newIndex
return cycles
for _ in range(int(input())):
s = [(ord(x) - 97) for x in input().rstrip()]
t = [(ord(x) - 97) for x in input().rstrip()]
print(getAns(s, t)) | ASSIGN VAR VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for i in range(int(input())):
s = input()
inf = 10**12
x = input()
dp = [[inf for i in range(26)] for j in range(len(s))]
d = {}
n = len(s)
for i in range(n - 1, -1, -1):
for j in range(26):
if j == ord(s[i]) - ord("a"):
dp[i][j] = i
elif i + 1 < n:
dp[i][j] = dp[i + 1][j]
ind = 0
c = 0
i = 0
while i < len(x):
if dp[0][ord(x[i]) - ord("a")] == inf:
print(-1)
break
else:
ind = dp[ind][ord(x[i]) - ord("a")] + 1
if ind > n:
c += 1
ind = 0
i -= 1
elif ind == n:
c += 1
ind = 0
i += 1
else:
if ind != 0:
c += 1
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
readline = sys.stdin.readline
Qu = int(readline())
Ans = [None] * Qu
for qu in range(Qu):
T = list(map(lambda x: ord(x) - 97, readline().strip()))
S = list(map(lambda x: ord(x) - 97, readline().strip()))
N = len(S)
M = len(T)
inf = 10**9 + 7
table = [([inf] * 26) for _ in range(M + 1)]
for i in range(M - 1, -1, -1):
t = T[i]
for j in range(26):
table[i][j] = table[i + 1][j]
table[i][t] = i
cnt = 0
j = -1
ans = 1
while cnt < N:
s = S[cnt]
if j == M or table[j + 1][s] == inf:
ans += 1
j = -1
j = table[j + 1][s]
cnt += 1
if j == inf:
ans = -1
break
Ans[qu] = ans
print("\n".join(map(str, Ans))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def solve(s, t):
dp = [[float("inf") for i in range(26)] for j in range(len(s))]
for i in range(len(s) - 1, -1, -1):
dp[i][ord(s[i]) - ord("a")] = i
if i + 1 < len(s):
for j in range(26):
dp[i][j] = min(dp[i][j], dp[i + 1][j])
moves = 0
i = 0
j = 0
while True:
index = dp[i][ord(t[j]) - ord("a")]
if index == float("inf") and i == 0:
print(-1)
return
found = False
if index == float("inf"):
i = 0
moves += 1
found = True
else:
j += 1
i = index + 1
if i == len(s):
i = 0
moves += 1
found = True
if j == len(t):
if not found:
moves += 1
break
print(moves)
def main():
T = int(input())
for i in range(T):
s = input()
t = input()
solve(s, t)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for nt in range(int(input())):
s = input()
t = input()
n = len(s)
d = set(s)
flag = 0
for i in t:
if i not in d:
print(-1)
flag = 1
break
if flag:
continue
dp = [[(-1) for i in range(n)] for j in range(26)]
for i in range(n - 1, -1, -1):
if i == n - 1:
dp[ord(s[i]) - 97][-1] = i
continue
for j in range(26):
if ord(s[i]) - 97 == j:
dp[j][i] = i
else:
dp[j][i] = dp[j][i + 1]
ans = 1
k = 0
for i in range(len(t)):
if k >= n or dp[ord(t[i]) - 97][k] == -1:
k = dp[ord(t[i]) - 97][0] + 1
ans += 1
else:
k = dp[ord(t[i]) - 97][k] + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | n = int(input())
for _ in range(n):
str1 = input()
str2 = input()
n = len(str1)
m = len(str2)
next_char = [([-1] * n) for _ in range(26)]
for i in range(n - 1, -1, -1):
c = ord(str1[i]) - ord("a")
if i < n - 1:
for j in range(26):
next_char[j][i] = next_char[j][i + 1]
next_char[c][i] = i
count = 1
at = 0
f = True
for i in range(m):
if at == n:
at = 0
count += 1
c = ord(str2[i]) - ord("a")
if next_char[c][at] == -1:
if at == 0 or next_char[c][0] == -1:
f = False
break
else:
at = next_char[c][0] + 1
count += 1
else:
at = next_char[c][at] + 1
if f:
print(count)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def find(x, a):
b = 0
e = len(a) - 1
while b <= e:
m = (b + e) // 2
if 0 < m < n - 1 and a[m] < x < a[m + 1]:
return a[m + 1]
elif a[m] > x:
e = m - 1
else:
b = m + 1
if 0 <= b <= len(a) - 1:
return a[b]
elif 0 <= e <= len(a) - 1:
return a[e]
test = int(input())
for q in range(test):
s = input()
t = input()
n = len(s)
d = {}
for i in range(n):
if s[i] in d:
d[s[i]] += [i]
else:
d[s[i]] = [i]
x = len(t)
l = []
ans = 0
for i in range(x):
if t[i] in d:
if i == 0:
ans += 1
l.append(d[t[i]][0])
elif l[-1] >= d[t[i]][-1]:
ans += 1
l.append(d[t[i]][0])
else:
l.append(find(l[-1], d[t[i]]))
else:
ans = -1
break
print(ans) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input())
for _ in range(T):
s = input()
t = input()
order = {}
for i, char in enumerate(s):
if char in order:
order[char].append(i)
else:
order[char] = [i]
answer = 1
prev = -1
for char in t:
if char not in order:
answer = -1
break
a = 0
b = len(order[char]) - 1
if order[char][b] <= prev:
answer += 1
prev = -1
if order[char][a] > prev:
prev = order[char][a]
else:
while b - a > 1:
m = (a + b) // 2
if order[char][m] <= prev:
a = m
else:
b = m
prev = order[char][b]
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | alphabet = "abcdefghijklmnopqrstuvwxyz"
T = int(input())
for t in range(T):
s = input()
t = input()
ls = len(s)
lt = len(t)
bs = [(0) for _ in range(26)]
bt = [(0) for _ in range(26)]
check = 0
for e in t:
bt[ord(e) - 97] = 1
for e in s:
bs[ord(e) - 97] = 1
for i in range(26):
if bt[i] and not bs[i]:
check = 1
break
if check:
print(-1)
else:
a = [[float("inf") for i in range(26)] for j in range(ls + 1)]
for i in range(ls - 1, -1, -1):
for j in alphabet:
if s[i] == j:
a[i][ord(j) - 97] = i
else:
a[i][ord(j) - 97] = a[i + 1][ord(j) - 97]
sum = 1
last = a[0][ord(t[0]) - 97] + 1
for i in range(1, lt):
if a[last][ord(t[i]) - 97] == float("inf"):
sum += 1
last = a[0][ord(t[i]) - 97] + 1
else:
last = a[last][ord(t[i]) - 97] + 1
print(sum) | ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def search_just_greater(arr, target):
if target >= arr[-1]:
return -1
low = 0
high = len(arr) - 1
while low < high:
mid = low + (high - low) // 2
if arr[mid] <= target:
low = mid + 1
else:
high = mid
return low
for _ in range(int(input())):
s = list(input())
t = list(input())
positions = []
for i in range(26):
positions.append([])
for i in range(len(s)):
positions[ord(s[i]) - 97].append(i)
pointer = -1
bufferEmpty = True
ans = 0
for i in range(len(t)):
cur = ord(t[i]) - 97
if len(positions[cur]) == 0:
ans = -1
break
newptr = search_just_greater(positions[cur], pointer)
if newptr == -1:
ans += 1
newptr = search_just_greater(positions[cur], -1)
if i == len(t) - 1:
ans += 1
pointer = positions[cur][newptr]
print(ans) | FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def upperBound(array, k):
low = 0
high = len(array)
while low < high:
mid = (low + high) // 2
if array[mid] < k:
low = mid + 1
else:
high = mid
return low
for _ in range(int(input())):
s = input().strip()
t = input().strip()
array = [[] for _ in range(26)]
for i in range(len(s)):
array[ord(s[i]) - ord("a")].append(i)
temp = -1
ops = 1
flag = True
for i in t:
k = ord(i) - ord("a")
if len(array[k]) == 0:
flag = False
break
if temp == -1:
temp = array[k][0]
continue
klag = False
tt = upperBound(array[k], temp)
if tt == len(array[k]):
ops += 1
temp = array[k][0]
continue
if array[k][tt] == temp:
tt += 1
if tt < len(array[k]):
klag = True
temp = array[k][tt]
if not klag:
ops += 1
temp = array[k][0]
continue
if flag:
print(ops)
else:
print(-1) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s, t = input(), input()
ss = set(list(s))
flag = False
for i in t:
if i not in ss:
flag = True
break
if flag:
print(-1)
continue
n = len(s)
dp = [list([float("inf")] * 26) for _ in range(n)]
for i in range(n - 1, -1, -1):
vl = ord(s[i]) - ord("a")
dp[i][vl] = i
for j in range(26):
if j != vl and i < n - 1:
dp[i][j] = dp[i + 1][j]
ans = 1
j = 0
for i in range(len(t)):
val = ord(t[i]) - ord("a")
if j == n:
j, ans = 0, ans + 1
if dp[j][val] == float("inf"):
ans, j = ans + 1, 0
j = dp[j][val] + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
def search(arr, x):
if arr[0] >= x:
return arr[0]
if arr[-1] < x:
return -1
lo = 0
hi = len(arr) - 1
ans = hi
while lo <= hi:
m = (lo + hi) // 2
if arr[m] >= x:
hi = m - 1
ans = m
else:
lo = m + 1
return arr[ans]
t = int(input())
for i in range(t):
s = input().strip()
t = input().strip()
ans = 1
a = [[] for i in range(26)]
for j in range(len(s)):
a[ord(s[j]) - ord("a")].append(j)
s_idx = 0
for char in t:
c_idx = ord(char) - ord("a")
if len(a[c_idx]) == 0:
print(-1)
break
tmp = search(a[c_idx], s_idx)
if tmp == -1:
ans += 1
s_idx = search(a[c_idx], 0) + 1
else:
s_idx = tmp + 1
else:
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR RETURN VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def binary(alpha, k):
global dic, dicle
n = dicle[alpha]
ar = dic[alpha]
if k >= ar[-1]:
ans = n
elif k < ar[0]:
ans = 0
else:
l = 0
r = n - 1
while not (k >= ar[l] and k < ar[l + 1]):
mid = (l + r) // 2
if ar[mid] <= k:
l = mid
elif ar[mid] > k:
r = mid
ans = l + 1
if ans == n:
return ar[0]
else:
return ar[ans]
for _ in range(int(input())):
st = input()
le = len(st)
dic = {}
dicle = {}
for i in range(le):
if st[i] in dic:
dic[st[i]].append(i)
dicle[st[i]] += 1
else:
dic[st[i]] = [i]
dicle[st[i]] = 1
flag = True
ts = input()
count = 1
cur = -1
lets = len(ts)
for j in range(lets):
if ts[j] in dic:
pos = binary(ts[j], cur)
if pos <= cur:
count += 1
cur = pos
else:
cur = pos
else:
flag = False
if flag:
print(count)
else:
print(-1) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = input()
t = input()
if set(t).difference(set(s)):
print(-1)
else:
nxt_s = [{"a": 0} for _ in range(-1, len(s))]
cur = {}
for i in range(len(s) - 1, -2, -1):
nxt_s[i] = cur.copy()
cur[s[i]] = i
ans = 1
i = -1
for c in t:
if c in nxt_s[i]:
i = nxt_s[i][c]
else:
ans += 1
i = nxt_s[-1][c]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT STRING NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
s, t = input()[:-1], input()[:-1]
n, m = len(s), len(t)
nxt = [[(0) for j in range(26)] for i in range(n + 1)]
nxt[-1] = [-1] * 26
for i in range(n - 1, -1, -1):
for j in range(26):
if s[i] == chr(ord("a") + j):
nxt[i][j] = i
else:
nxt[i][j] = nxt[i + 1][j]
a = ans = 0
for i in t:
if nxt[a][ord(i) - ord("a")] == -1:
a = 0
ans += 1
a = nxt[a][ord(i) - ord("a")] + 1
if not a:
print(-1)
break
else:
print(ans + 1) | ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for t in range(int(input())):
s = input()
z = input()
c = []
a = []
plus = 0
for i in z:
c.append(i)
plus += 1
count = 0
for i in s:
a.append(i)
count += 1
b = [[(0) for i in range(26)] for j in range(count + 1)]
alpha = [(9999999) for i in range(26)]
for i in range(count):
alpha[ord(a[i]) - 97] = min(alpha[ord(a[i]) - 97], i)
i = count - 1
while i >= 1:
b[i][ord(a[i]) - 97] = i
b[i - 1] = b[i][:]
i -= 1
add = 1
one = alpha[ord(c[0]) - 97]
if one == 9999999:
print(-1)
continue
i = 0
while True:
i += 1
if i == plus:
flag = 1
break
two = ord(c[i]) - 97
if b[one + 1][two] == 0:
add += 1
one = alpha[ord(c[i]) - 97]
if one == 9999999:
flag = -1
break
else:
one = b[one + 1][two]
if flag == -1:
print(-1)
else:
print(add) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
t = int(input())
def binary(ar, l, r, pos, n):
m = (l + r) // 2
if m == 0 and ar[m] > pos:
return ar[m]
elif m > 0 and ar[m] > pos and ar[m - 1] <= pos:
return ar[m]
if l < r:
if m > 0 and ar[m - 1] > pos:
return binary(ar, l, m, pos, n)
elif ar[m] <= pos:
return binary(ar, m + 1, r, pos, n)
return -2
for i in range(t):
b = False
ct = 1
t = input()
s = input()
dic = {}
fr = {}
for j in range(len(t)):
if t[j] not in dic:
dic[t[j]] = [j]
fr[t[j]] = 1
else:
dic[t[j]].append(j)
fr[t[j]] += 1
pos = -1
for j in s:
if j not in dic:
print("-1")
b = True
break
else:
l = fr[j]
val = binary(dic[j], 0, l - 1, pos, l)
if val > pos:
pos = val
elif val == -2 and pos != -1:
ct += 1
pos = -1
val = binary(dic[j], 0, l - 1, pos, l)
if val > pos:
pos = val
elif val == -2 and pos == -1:
print("-1")
b = True
break
if b == False:
print(ct) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR RETURN VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | tt = int(input())
for i in range(tt):
s = input()
t = input()
sm = [[] for i in range(len(s))]
sa = [(-1) for i in range(26)]
cind = 0
start = 0
cnt = 1
s0 = s[0]
for i in range(len(s) - 1, -1, -1):
sm[i] = sa.copy()
sa[int(s[i], base=36) - 10] = i
sa = sm[0].copy()
for i in range(26):
if sa[i] == -1:
sa[i] = -2
if sa[int(s[0], base=36) - 10] == -2:
sa[int(s[0], base=36) - 10] = -3
sm[0] = sa.copy()
if s0 == t[0]:
start = 1
for i in range(start, len(t)):
l = int(t[i], base=36) - 10
nind = sm[cind][l]
if nind == -1:
cnt += 1
if int(s0, base=36) - 10 == l:
cind = 0
continue
else:
if sm[0][l] == -2:
print(-1)
break
cind = sm[0][l]
continue
if nind == -2:
print(-1)
break
if nind == -3:
cind = 0
cnt += 1
else:
cind = nind
else:
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | t = int(input())
def find(letter, after):
global pos
if letter not in pos.keys():
return -2
if pos[letter][0] > after:
return pos[letter][0]
elif pos[letter][-1] <= after:
return -1
lower = 0
upper = len(pos[letter]) - 1
while upper - lower > 1:
mid = (lower + upper) // 2
if pos[letter][mid] > after:
upper = mid
else:
lower = mid
return pos[letter][upper]
for number in range(t):
s = input()
t = input()
pos = {}
for i, let in enumerate(s):
if let not in pos.keys():
pos[let] = []
pos[let].append(i)
counter = 0
i = -1
found = False
for l in t:
c = find(l, i)
if c == -2:
print(-1)
found = True
break
elif c == -1:
counter += 1
i = -1
i = find(l, i)
else:
i = c
if found:
continue
if i == -1:
print(counter)
else:
print(counter + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR RETURN NUMBER IF VAR VAR NUMBER VAR RETURN VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | LETTERS = set(list("abcdefghijklmnopqrstuvwxyz"))
def PrepareLettersIndices(s):
s_letters = dict()
for letters in LETTERS:
s_letters[letters] = []
for i in range(len(s)):
s_letters[s[i]].append(i)
return s_letters
def BinSearch(num, numbers):
if num >= numbers[-1]:
return -1
if num < numbers[0]:
return numbers[0]
left = 0
right = len(numbers) - 1
mid = (left + right) // 2
while not numbers[mid] <= num < numbers[mid + 1]:
if numbers[mid] <= num:
left = mid
else:
right = mid
mid = (left + right) // 2
return numbers[mid + 1]
def main():
T = int(input())
for i in range(T):
s = list(input())
t = list(input())
s_set = set(s)
t_set = set(t)
if not t_set.issubset(s_set):
print(-1)
continue
letters_dict = PrepareLettersIndices(s)
result = 1
current = -1
j = 0
while j < len(t):
new = BinSearch(current, letters_dict[t[j]])
if new == -1:
result += 1
current = -1
else:
current = new
j += 1
print(result)
return 0
main() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | tt = int(input())
def find(c, i):
l = 0
r = len(mp[c]) - 1
ret = -1
while l <= r:
m = (l + r) // 2
if mp[c][m] > i:
ret = mp[c][m]
r = m - 1
else:
l = m + 1
return ret
for _ in range(tt):
s = input()
t = input()
mp = [[] for i in range(26)]
for i in range(len(s)):
mp[ord(s[i]) - 97].append(i)
prev = -1
ans = 1
for i in range(len(t)):
g = find(ord(t[i]) - 97, prev)
if len(mp[ord(t[i]) - 97]) == 0:
ans = float("-inf")
elif g != -1:
prev = g
else:
ans += 1
prev = mp[ord(t[i]) - 97][0]
print(-1 if ans <= float("-inf") else ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | t = int(input())
for i in range(t):
s = input()
k = input()
b = []
a = []
for j in s:
b.append(j)
for j in k:
a.append(j)
b1 = [[] for j in range(26)]
for j in range(len(b)):
b1[ord(b[j]) - 97].append(j)
dp = [([-1] * 26) for j in range(len(b) + 1)]
for j in range(26):
k = b1[j]
p = 0
curr = 0
while p < len(b):
if curr < len(k):
if k[curr] >= p:
dp[p][j] = k[curr]
else:
curr += 1
if curr < len(k):
dp[p][j] = k[curr]
else:
break
p += 1
j = 0
curr = 0
ans = 1
f = 0
while j < len(a):
p = ord(a[j]) - 97
k = dp[curr][p]
if k == -1:
curr = 0
k = dp[curr][p]
if k == -1:
f = -1
break
ans += 1
curr = k + 1
else:
curr = k + 1
j += 1
if f == -1:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = input()
t = input()
d = {}
for x in s:
d[x] = 1
for x in t:
if d.get(x, 0) == 0:
print(-1)
break
else:
dp = [([-1] * 26) for i in range(len(s))]
p = [-1] * 26
for i in range(len(s) - 1, -1, -1):
p[ord(s[i]) - 97] = i
for i in range(len(s) - 1, -1, -1):
dp[i] = p[:]
p[ord(s[i]) - 97] = i
ans = 1
pos = -1
for x in t:
if pos >= dp[pos][ord(x) - 97]:
ans += 1
pos = dp[pos][ord(x) - 97]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def next(arr, target):
start = 0
end = len(arr) - 1
ans = -1
while start <= end:
mid = (start + end) // 2
if arr[mid] <= target:
start = mid + 1
else:
ans = mid
end = mid - 1
return ans
def sol():
s = list(input())
t = list(input())
f = []
for i in range(26):
f.append([])
for i in range(len(s)):
f[ord(s[i]) - 97].append(i)
for i in range(len(t)):
t[i] = ord(t[i]) - 97
ind = 0
c = 1
inds = [0] * 26
l = -1
while ind < len(t):
if inds[t[ind]] >= len(f[t[ind]]):
if len(f[t[ind]]) == 0:
c = -1
break
inds = [0] * 26
l = -1
c += 1
continue
fl = False
ne = next(f[t[ind]], l)
if ne == -1:
inds = [0] * 26
l = -1
c += 1
continue
inds[t[ind]] = ne
if f[t[ind]][inds[t[ind]]] <= l:
inds = [0] * 26
l = -1
c += 1
continue
l = f[t[ind]][inds[t[ind]]]
inds[t[ind]] += 1
ind += 1
print(c)
for _ in range(int(input())):
sol() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def countGreater(arr, k):
l = 0
r = len(arr) - 1
n = len(arr)
leftGreater = n
while l <= r:
m = int(l + (r - l) // 2)
if arr[m] > k:
leftGreater = m
r = m - 1
else:
l = m + 1
return n - leftGreater
T = int(input())
while T:
s = input()
t = input()
ls = [[] for i in range(26)]
for i in range(len(s)):
ls[ord(s[i]) - 97].append(i)
flag = True
for i in range(len(t)):
if len(ls[ord(t[i]) - 97]) == 0:
flag = False
break
if flag:
ans = 0
x = -1
i = 0
while i < len(t):
arr = ls[ord(t[i]) - 97]
if countGreater(arr, x) == 0:
ans += 1
x = -1
i -= 1
else:
x = arr[len(arr) - countGreater(arr, x)]
i += 1
print(ans + 1)
else:
print(-1)
T -= 1 | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = input()
t = input()
d = {}
for i in range(len(s)):
d[s[i]] = 1
flag = True
for i in range(len(t)):
if d.get(t[i]) == None:
flag = False
break
if not flag:
print(-1)
continue
dp = []
ll = [float("inf")] * 26
for _ in range(len(s)):
dp.append(list(ll))
for i in range(len(s) - 1, -1, -1):
dp[i][ord(s[i]) - ord("a")] = i
for j in range(26):
if j != ord(s[i]) - ord("a") and i + 1 < len(s):
dp[i][j] = dp[i + 1][j]
ct = 1
i = 0
j = 0
while i < len(t):
if j == len(s):
j = 0
ct += 1
if dp[j][ord(t[i]) - ord("a")] >= float("inf"):
ct += 1
j = 0
j = dp[j][ord(t[i]) - ord("a")] + 1
i += 1
print(ct) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input().strip())
for _ in range(T):
s = input().strip()
t = input().strip()
lt = len(t)
if lt == 0:
print(0)
continue
s_letters = set(el for el in s)
impossible = False
for el in t:
if el not in s_letters:
impossible = True
break
if impossible:
print(-1)
continue
ls = len(s)
nxt = [[(-1) for _ in range(26)] for _ in range(ls)]
for i in range(ls - 1, -1, -1):
letter = s[i]
k = ord(letter) - 97
for j in range(26):
if j == k:
nxt[i][j] = i
elif i != ls - 1:
nxt[i][j] = nxt[i + 1][j]
ct = 0
cnext = 0
total = 0
while ct < len(t):
letter = ord(t[ct]) - 97
if nxt[cnext][letter] != -1:
if cnext == 0:
total += 1
cnext = (nxt[cnext][letter] + 1) % ls
else:
total += 1
cnext = (nxt[0][letter] + 1) % ls
ct += 1
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input())
def solve():
S = input()
T = input()
char_to_index = {}
for i, t in enumerate(S):
if t not in char_to_index:
char_to_index[t] = []
char_to_index[t].append(i)
ans = 0, len(S) + 1
for t in T:
if t not in char_to_index:
return -1
indices = char_to_index[t]
nextIndex = len(indices)
jump = len(indices)
while jump != 0:
while 0 <= nextIndex - jump and indices[nextIndex - jump] > ans[1]:
nextIndex -= jump
jump //= 2
if nextIndex == len(indices):
ans = ans[0] + 1, indices[0]
else:
ans = ans[0], indices[nextIndex]
return ans[0]
for _ in range(T):
print(solve()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER WHILE NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | INF = 1000000.0
def main():
def solve():
ss = input()
tt = input()
ls = len(ss)
nxt = [[INF] * 26]
for i, s in enumerate(ss[::-1]):
nxt.append(nxt[-1].copy())
nxt[-1][ord(s) - ord("a")] = ls - i - 1
nxt.reverse()
ans = 1
pos = 0
for t in tt:
ordt = ord(t) - ord("a")
nxtt = nxt[pos][ordt]
if nxtt == INF:
if nxt[0][ordt] == INF:
print(-1)
return
ans += 1
pos = nxt[0][ordt] + 1
else:
pos = nxtt + 1
print(ans)
q = int(input())
for _ in range(q):
solve()
main() | ASSIGN VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = [(ord(c) - 97) for c in list(input())]
t = [(ord(c) - 97) for c in list(input())]
pl = [[] for i in range(26)]
for i in range(len(s)):
pl[s[i]].append(i)
pos = -1
j = 0
ans = 1
while j < len(t):
i = t[j]
if pl[i] == []:
ans = -1
break
else:
l = 0
r = len(pl[i]) - 1
while l <= r:
mid = l + (r - l) // 2
if pl[i][mid] > pos:
r = mid - 1
else:
l = mid + 1
if l >= len(pl[i]):
ans += 1
pos = -1
else:
pos = pl[i][l]
j += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def binarySearch(seq, a):
if not seq:
return -2
n = len(seq)
l = 0
r = len(seq)
while l < r:
m = (l + r) // 2
if seq[m] > a:
r = m
if seq[m] < a:
l = m + 1
if seq[m] == a:
if m == n - 1:
return -1
return seq[m + 1]
if l == n:
return -1
return seq[l]
def minOp(S, T):
letters = [[] for _ in range(26)]
for i, c in enumerate(S):
letters[ord(c) - ord("a")].append(i)
op = 1
pre = -1
for i, t in enumerate(T):
t_pos = letters[ord(t) - ord("a")]
t_index = binarySearch(t_pos, pre)
if t_index == -1:
op += 1
pre = t_pos[0]
elif t_index == -2:
return -1
else:
pre = t_index
return op
t = int(input())
for _ in range(t):
S = input()
T = input()
print(minOp(S, T)) | FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def main():
T = int(input().strip())
for _ in range(T):
s = input().strip()
t = input().strip()
charset = set(s)
if any(c not in charset for c in t):
print(-1)
continue
d = {c: len(s) for c in charset}
jump = [0] * len(s)
for i in range(len(s) - 1, -1, -1):
jump[i] = dict(d)
d[s[i]] = i
i = -1
result = 1
for c in t:
i += 1
if i == len(s):
i = 0
result += 1
while s[i] != c:
i = jump[i][c]
if i == len(s):
i = 0
result += 1
print(result)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
def solve():
s = [(ord(i) - 97) for i in input().strip()]
t = [(ord(i) - 97) for i in input().strip()]
ss = set(s)
tt = set(t)
for i in tt:
if i not in ss:
print(-1)
return
nxt = [[(-1) for _ in range(26)] for _ in range(len(s) + 1)]
for i in range(len(s) - 1, -1, -1):
for j in range(26):
nxt[i][j] = nxt[i + 1][j]
nxt[i][s[i]] = i
count = 1
pos = 0
for i in range(len(t)):
if pos == len(s) or nxt[pos][t[i]] == -1:
pos = 0
count += 1
pos = nxt[pos][t[i]] + 1
print(count)
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = list(map(lambda x: ord(x) - 97, input()))
t = list(map(lambda x: ord(x) - 97, input()))
if len(set(t) - set(s)) > 0:
print(-1)
continue
dp = [([-1] * 26) for i in range(len(s))]
nextt = [-1] * 26
for i in range(len(s) - 1, -1, -1):
nextt[s[i]] = i
for i in range(len(s) - 1, -1, -1):
dp[i] = nextt[:]
nextt[s[i]] = i
pos, ans = -1, 1
for i in t:
if pos >= dp[pos][i]:
ans += 1
pos = dp[pos][i]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input())
for _ in range(T):
s = input()
t = input()
n = len(s)
pos = [[] for _ in range(26)]
for i in range(n):
pos[ord(s[i]) - 97].append(i)
val = -1
ans = 1
for x in t:
p = pos[ord(x) - 97]
if not p:
ans = -1
break
l, r = 0, len(p) - 1
while l <= r:
mid = (l + r) // 2
if p[mid] > val:
r = mid - 1
else:
l = mid + 1
if l < len(p):
val = p[l]
else:
ans += 1
val = p[0]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | tests = int(input())
while tests > 0:
s = input()
t = input()
dpList = []
i = len(s) - 1
cd = {}
while i >= 0:
cd[s[i]] = i
dpList.append(cd.copy())
i -= 1
dpList = dpList[::-1]
dpList.append({})
ans = 1
i = 0
ci = 0
while i < len(t):
val = t[i]
nextIndex = len(s) + 1
if val in dpList[ci]:
ci = dpList[ci][val] + 1
i += 1
else:
if ci == 0:
ans = -1
break
ans += 1
ci = 0
print(ans)
tests -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def f(s, t):
dp = [([float("inf")] * 26) for i in range(len(s) + 10)]
for i in reversed(range(len(s))):
dp[i] = dp[i + 1].copy()
dp[i][ord(s[i]) - 97] = i
res = 1
pos = 0
for i in range(len(t)):
if pos == len(s):
pos = 0
res += 1
if dp[pos][ord(t[i]) - 97] == float("inf"):
pos = 0
res += 1
if dp[pos][ord(t[i]) - 97] == float("inf"):
res = float("inf")
break
pos = dp[pos][ord(t[i]) - 97] + 1
return [res, -1][res == float("inf")]
for i in range(int(input())):
s = input()
t = input()
print(f(s, t)) | FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN LIST VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | T = int(input())
while T:
T -= 1
s = input()
n = len(s)
t = input()
f = [[n] * 26] * n
for i in range(n - 1, -1, -1):
if i < n - 1:
f[i] = f[i + 1].copy()
f[i][ord(s[i]) - ord("a")] = i
i = 0
res = 1
ok = True
for c in t:
j = ord(c) - ord("a")
if i == n or f[i][j] == n:
res += 1
i = 0
if f[i][j] == n:
print(-1)
ok = False
break
i = f[i][j] + 1
if ok:
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s1 = input()
s2 = input()
if not set(s2).issubset(set(s1)):
print(-1)
continue
n = len(s1)
l = [([10**9 + 7] * 26) for _ in range(n)]
for i in range(n - 1, 0, -1):
for j in range(26):
l[i - 1][j] = l[i][j]
l[i - 1][ord(s1[i]) - 97] = i
ans = 0
curr = 0
i = 0
while i < len(s2):
if curr == 10**9 + 7:
ans += 1
curr = 0
if s2[i] == s1[curr]:
i += 1
if i == len(s2):
ans += 1
break
curr = l[curr][ord(s2[i]) - 97]
else:
curr = l[curr][ord(s2[i]) - 97]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = sys.stdin.readline
def bnsearch(L, x):
start = 0
end = len(L) - 1
while start <= end:
mid = (start + end) // 2
if L[mid] == x:
return mid
if L[mid] > x:
end = mid - 1
else:
start = mid + 1
return start
for i in " " * int(input()):
s = input()
t = input()
s = s[: len(s) - 1]
t = t[: len(t) - 1]
cond = True
d = [[] for i in range(26)]
checks = ""
for i in s:
if i not in checks:
checks += i
checkt = ""
for i in t:
if i not in checkt:
checkt += i
for i in checkt:
if i not in checks:
cond = False
print(-1)
break
if not cond:
continue
for i in range(len(s)):
d[ord(s[i]) - 97].append(i)
counter = 1
pt = 0
for i in t:
iL = d[ord(i) - 97]
k = bnsearch(iL, pt)
if k == len(iL):
pt = iL[0] + 1
counter += 1
continue
else:
pt = iL[k] + 1
print(counter) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for h in range(int(input())):
s = input()
t = input()
dp = [[float("inf") for i in range(26)] for i in range(len(s) + 1)]
dp[-2][ord(s[-1]) - ord("a")] = len(s) - 1
for i in range(len(s) - 2, -1, -1):
for j in range(26):
dp[i][j] = dp[i + 1][j]
dp[i][ord(s[i]) - ord("a")] = i
ans = 1
l1 = -1
l2 = 0
while l2 < len(t):
if l1 == -1 and dp[l1 + 1][ord(t[l2]) - ord("a")] == float("inf"):
print(-1)
break
elif dp[l1 + 1][ord(t[l2]) - ord("a")] != float("inf"):
l1 = dp[l1 + 1][ord(t[l2]) - ord("a")]
l2 += 1
else:
l1 = -1
ans += 1
else:
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)]
def slv():
s = input()
n = len(s)
s *= 2
t = input()
m = len(t)
dp = []
for char in range(26):
a = [2 * n] * (2 * n)
for i in range(2 * n):
if s[i] == chr(char + ord("a")):
a[i] = i
for i in reversed(range(2 * n - 1)):
a[i] = min(a[i], a[i + 1])
dp.append(a)
ans, pos = 1, 0
for i in range(m):
char = ord(t[i]) - ord("a")
if dp[char][pos] == 2 * n:
print(-1)
return 0
elif dp[char][pos] < n:
pos = dp[char][pos] + 1
else:
pos = dp[char][pos] - n + 1
ans += 1
print(ans)
return 0
def main():
t = int(input())
for i in range(t):
slv()
return 0
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def alpha(x):
return ord(x) - 97
for _ in range(int(input())):
s = input()
t = input()
curi = 0
iters = 1
nexts = [[(-1) for _ in range(len(s) + 1)] for _ in range(26)]
for i in range(26):
for j in range(len(s) - 1, -1, -1):
if alpha(s[j]) == i:
nexts[i][j] = j
else:
nexts[i][j] = nexts[i][j + 1]
for i in range(26):
for j in range(len(s) - 1, -1, -1):
if nexts[i][j] == -1:
nexts[i][j] = nexts[i][0]
for ch in t:
if curi == len(s):
curi = 0
iters += 1
nexti = nexts[alpha(ch)][curi]
if nexti == -1:
print(-1)
break
if nexti < curi:
iters += 1
curi = nexti + 1
else:
print(iters) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def bi_search(a, x):
l, r = 0, len(a) - 1
if a[r] <= x:
return -1
while l < r:
m = (l + r) // 2
if a[m] <= x:
l = m + 1
else:
r = m
return a[r]
def solve(s, t):
pos = [[] for _ in range(26)]
for i, c in enumerate(s):
x = ord(c) - ord("a")
pos[x].append(i)
cur = -1
ans = 1
for i, c in enumerate(t):
x = ord(c) - ord("a")
if pos[x] == []:
print(-1)
return
it = bi_search(pos[x], cur)
if it == -1:
it = pos[x][0]
ans += 1
cur = it
print(ans)
def main():
tc = int(input())
for _ in range(tc):
s = input()
t = input()
solve(s, t)
main() | FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR LIST EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
input = lambda: sys.stdin.readline().rstrip()
Q = int(input())
for _ in range(Q):
S = [(ord(a) - 97) for a in input()]
T = [(ord(a) - 97) for a in input()]
N = len(S)
S += S
X = [([-1] * 26) for _ in range(N)]
Y = [-1] * 26
for _ in range(2):
for i in range(N)[::-1]:
a = S[i]
X[i][a] = Y[a]
for j in range(26):
if X[i][j] == -1:
X[i][j] = X[i + 1 - N][j]
Y[a] = i
X[i - 1][a] = i
i = N - 1
ans = 0
for t in T:
ni = X[i][t]
if ni < 0:
print(-1)
break
if ni <= i:
ans += 1
i = ni
else:
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def s_map(s):
m = [[None] * 26] + [([None] * 26) for _ in s]
for prev_i, (prev_x, prev_col, cur_col) in reversed(
list(enumerate(zip(s, m[1:], m), 1))
):
cur_col[:] = prev_col
cur_col[prev_x] = prev_i
return m
def solve(s, t):
if not set(t).issubset(set(s)):
return -1
s = [(ord(c) - ord("a")) for c in s]
t = [(ord(c) - ord("a")) for c in reversed(t)]
m = s_map(s)
n = 0
while t:
i = 0
while t and m[i][t[-1]]:
i = m[i][t[-1]]
t.pop()
n += 1
return n
T = int(input())
for _ in range(T):
s = input().strip()
t = input().strip()
print(solve(s, t)) | FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP LIST NONE NUMBER BIN_OP LIST NONE NUMBER VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | n = int(input())
for i in range(0, n):
s = input().rstrip()
t = input().rstrip()
L = []
x = list(s)
y = list(t)
for j in range(0, 26):
D = []
L.append(D)
for j in range(0, len(x)):
L[ord(x[j]) - 97].append(j)
M = list(L)
j = 0
tot = 0
G = 0
while j < len(y):
A = ord(y[j]) - 97
if len(L[A]) == 0:
G = 1
break
else:
M = list(L)
O = 0
while j < len(y):
A = ord(y[j]) - 97
if O == 0:
if len(M[A]) != 0:
O += 1
j += 1
V = M[A][0]
else:
break
else:
B = M[A]
if len(B) != 0:
I = 0
J = len(B) - 1
N = -1
while I <= J:
m = (I + J) // 2
if B[m] <= V:
I = m + 1
else:
N = B[m]
J = m - 1
if N != -1:
V = N
j += 1
else:
break
else:
break
M = list(L)
tot += 1
if G == 1:
print(-1)
else:
print(tot) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) β the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer β the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | t = int(input())
for _ in range(t):
s = str(input())
t = str(input())
INF = 10**18
n = len(s)
m = len(t)
X = [([INF] * 26) for i in range(n + 5)]
for i in reversed(range(n)):
for j in range(26):
X[i][j] = X[i + 1][j]
X[i][ord(s[i]) - ord("a")] = i
ans = 1
pos = 0
for i in range(m):
if pos == n:
pos = 0
ans += 1
if X[pos][ord(t[i]) - ord("a")] == INF:
pos = 0
ans += 1
if X[pos][ord(t[i]) - ord("a")] == INF and pos == 0:
ans = INF
break
pos = X[pos][ord(t[i]) - ord("a")] + 1
if ans == INF:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.