message
stringlengths 2
23.8k
| message_type
stringclasses 2
values | message_id
int64 0
1
| conversation_id
int64 97
109k
| cluster
float64 0
0
| __index_level_0__
int64 194
217k
|
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
n=int(input())
s=input()
c=0
d=0
for i in range(0,n):
if(s[i]=='1'):
c=c+1
else:
d=d+1
if(c>d):
print(c-d)
else:
print(d-c)
```
|
instruction
| 0
| 3,768
| 0
| 7,536
|
Yes
|
output
| 1
| 3,768
| 0
| 7,537
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
n=int(input())
a=input()
answer = 0
answer2=0
for i in a:
if i == '1':
answer +=1
else:
answer2+=1
print(n-(min(answer,answer2)*2))
```
|
instruction
| 0
| 3,769
| 0
| 7,538
|
Yes
|
output
| 1
| 3,769
| 0
| 7,539
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
n=int(input())
a=input()
s=0
for i in range(n):
if a[i]=='0':
s+=1
print(abs(n-2*s))
```
|
instruction
| 0
| 3,770
| 0
| 7,540
|
Yes
|
output
| 1
| 3,770
| 0
| 7,541
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
str=input()
b=True
while b:
b=False
if '10' in str:
str=str.replace('10','')
b=True
else:
if '01' in str:
str=str.replace('01','')
b=True
print(len(str))
```
|
instruction
| 0
| 3,771
| 0
| 7,542
|
No
|
output
| 1
| 3,771
| 0
| 7,543
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
# n = (a1+1)(a2+1)...(ak+1)
# number = p1^a1 * p2^a2 *p3^a3 *...*pk^ak
n = int(input())
s = input()
s_inxex = [1] * n
if n == 1:
print('1')
exit()
i = 0
j = n - 1
while i < j:
if s[i] != s[j]:
s_inxex[i] = 0
s_inxex[j] = 0
i += 1
j -= 1
else:
j -= 1
s_out = [x for idx,x in enumerate(s) if s_inxex[idx] == 1]
print (len(s_out))
```
|
instruction
| 0
| 3,772
| 0
| 7,544
|
No
|
output
| 1
| 3,772
| 0
| 7,545
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
# n = (a1+1)(a2+1)...(ak+1)
# number = p1^a1 * p2^a2 *p3^a3 *...*pk^ak
n = int(input())
s = input()
s_inxex = [1] * n
if n == 1:
print(s)
exit()
prev_i = -1
i = 0
j = 1
while i != j and j < n:
if s[i] != s[j]:
s_inxex[i] = 0
s_inxex[j] = 0
if prev_i < 0:
i = j + 1
j = i + 1
else:
i = prev_i
j += 1
else:
prev_i = i
i = j
j += 1
#print('end loop: ', i, j)
s_out = [x for idx,x in enumerate(s) if s_inxex[idx] == 1]
#print ("".join(s_out))
print (len(s_out))
```
|
instruction
| 0
| 3,773
| 0
| 7,546
|
No
|
output
| 1
| 3,773
| 0
| 7,547
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: <image>.
In the second sample test it is possible to change the string like the following: <image>.
In the third sample test it is possible to change the string like the following: <image>.
Submitted Solution:
```
size = int(input())
num = input()
while '10' in num:
num = num.replace('10', '')
print(len(num))
```
|
instruction
| 0
| 3,774
| 0
| 7,548
|
No
|
output
| 1
| 3,774
| 0
| 7,549
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,239
| 0
| 8,478
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
from sys import stdin
from sys import setrecursionlimit as SRL; SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
s = str(rd().strip())
t = str(rd().strip())
s = '0' + s
canl = [0] * (len(s) + 10)
canr = [0] * (len(s) + 10)
j = 0
for i,v in enumerate(s):
if j<len(t) and v == t[j]:
j += 1
canl[i] = j
j = len(t) - 1
for i in range(len(s)-1,-1,-1):
if j>=0 and s[i] == t[j]:
j -= 1
canr[i] = len(t)-j-1
def check(x):
if x > len(s):
return False
for i in range(1,len(s) - x+1):
l = i - 1
r = i + x
if canl[l] + canr[r] >= len(t):
return True
return False
l = 0
r = len(s)
while l<r:
mid = (l+r)//2
if check(mid):
l = mid + 1
else:
r = mid
print(r-1)
```
|
output
| 1
| 4,239
| 0
| 8,479
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,240
| 0
| 8,480
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
s = input()
t = input()
n = len(s)
m = len(t)
dp1 = [0] * (n + 1)
for i in range(n):
dp1[i + 1] = dp1[i]
if (dp1[i] < m and s[i] == t[dp1[i]]):
dp1[i + 1] += 1
dp2 = [0] * (n + 1)
for i in range(n - 1, -1, -1):
dp2[i] = dp2[i + 1]
if (dp2[i + 1] < m and s[i] == t[-1 - dp2[i + 1]]):
dp2[i] += 1
def check(ln):
for i in range(n - ln + 1):
if ((dp1[i] + dp2[i + ln]) >= m):
return True
return False
l, r = 0, n
while (r - l > 1):
mid = (l + r) // 2
if (check(mid)):
l = mid
else:
r = mid
print(l)
```
|
output
| 1
| 4,240
| 0
| 8,481
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,241
| 0
| 8,482
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
def naiveSolve(n):
return
def main():
# s --> t
s = '$' + input() + '$'
t = '$' + input() + '$'
n = len(t)
left = [-1] * n
right = [-1] * n
m = len(s)
j = 0
for i in range(m):
if s[i] == t[j]:
left[j] = i
j += 1
if j == n:
break
j = n - 1
for i in range(m-1, -1, -1):
if s[i] == t[j]:
right[j] = i
j -= 1
if j == -1:
break
ans = 0
for i in range(n - 1):
ans = max(ans, right[i+1] - left[i] - 1)
print(ans)
return
import sys
# input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(l,r):
print('? {} {}'.format(l,r))
sys.stdout.flush()
return int(input())
def answerInteractive(x):
print('! {}'.format(x))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
from math import gcd,floor,ceil
for _abc in range(1):
main()
```
|
output
| 1
| 4,241
| 0
| 8,483
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,242
| 0
| 8,484
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
s = input()
t = input()
pos = [[-1, -1] for i in range(len(t))]
ptr = 0
for i,c in enumerate(t):
while s[ptr] != c:
ptr += 1
pos[i][0] = ptr
ptr += 1
ptr = len(s) - 1
for i in range(len(t)-1, -1, -1):
c = t[i]
while s[ptr] != c:
ptr -= 1
pos[i][1] = ptr
ptr -= 1
best = max(pos[0][1], len(s)-pos[-1][0]-1)
for i in range(1, len(pos)):
best = max(best, pos[i][1] - pos[i-1][0] - 1)
print(best)
```
|
output
| 1
| 4,242
| 0
| 8,485
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,243
| 0
| 8,486
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
def f(n):
for i in range(-1,lt):
if right[i+1]-left[i]>n:
return True
return False
s=input()
t=input()
ls=len(s)
lt=len(t)
right=dict()
left=dict()
i=0
j=0
ls=len(s)
left[-1]=-1
while i<lt:
if s[j]==t[i]:
left[i]=j
i+=1
j+=1
j=ls-1
i=lt-1
right[lt]=ls
while i>=0:
if s[j]==t[i]:
right[i]=j
i-=1
j-=1
low=0
high=ls-1
#print(right,"\n",left)
while low<=high:
mid=(high+low)//2
# print(mid,f(mid))
if f(mid):
low=mid+1
else:
high=mid-1
print(high)
```
|
output
| 1
| 4,243
| 0
| 8,487
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,244
| 0
| 8,488
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
s=input()
t=input()
s_pos=[len(s) for i in range(len(t))]
i,j=len(s)-1,len(t)-1
while i>-1 and j>-1:
if s[i]==t[j]:
s_pos[j]=i
j-=1
i-=1
p,q=0,0
ans=0
while p<len(s):
if q==len(t):
ans=max(ans,len(s)-p)
break
remove=s_pos[q]-p
ans=max(ans,remove)
if s[p]==t[q]:
q+=1
p+=1
print(ans)
```
|
output
| 1
| 4,244
| 0
| 8,489
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,245
| 0
| 8,490
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import heapq
def main():
s = input()
t = input()
n, m = len(s), len(t)
i, j = 0, 0
first = []
last = []
while j < m:
if s[i] == t[j]:
first.append(i)
i += 1
j += 1
else:
i += 1
i, j = n-1, m-1
while j >= 0:
if s[i] == t[j]:
last.append(i)
j -= 1
i -= 1
last = last[::-1]
ans = max(last[0], n-first[-1]-1)
for i in range(m-1):
ans = max(ans, last[i+1]-first[i]-1)
print (ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
```
|
output
| 1
| 4,245
| 0
| 8,491
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
|
instruction
| 0
| 4,246
| 0
| 8,492
|
Tags: binary search, greedy, implementation, two pointers
Correct Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
def main():
s=list(input())
t=list(input())
n=len(s)
m=len(t)
pre=[]
post=[]
r=0
for i in range(n):
if s[i]==t[r]:
r+=1
pre.append(i)
if r==m:
break
r=m-1
for i in range(n-1,-1,-1):
if s[i]==t[r]:
r-=1
post.append(i)
if r==-1:
break
post.reverse()
# print(pre)
# print(post)
ans=0
for i in range(m-1):
ans=max(ans,post[i+1]-pre[i]-1)
print(max(ans,n-pre[-1]-1,post[0]))
#----------------------------------------------------------------------------------------
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# endregion
if __name__ == '__main__':
main()
```
|
output
| 1
| 4,246
| 0
| 8,493
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
# s = "tessts"
# t = "tet"
s = input()
t = input()
ns = len(s)
nt = len(t)
ans = 0
found = 0
DP_left = [0]*ns
DP_left[0] = int(s[0] == t[0])
for i in range(1, ns):
if DP_left[i-1] < nt:
DP_left[i] = DP_left[i-1] + (s[i] == t[DP_left[i-1]])
DP_right = [0]*ns
DP_right[-1] = int(s[-1] == t[-1])
for i in reversed(range(ns-1)):
if 0 <= nt - DP_right[i+1] - 1 < nt :
DP_right[i] = DP_right[i+1] + (s[i] == t[nt - 1 - DP_right[i+1]])
from collections import defaultdict
d_r = defaultdict(int)
d_l = defaultdict(int)
old = 1
for index, i in enumerate(DP_left):
if i == old:
d_l[i] = index
old += 1
for index, i in enumerate(DP_right):
d_r[i] = max(d_r[i], index)
for i in range(nt + 1):
if i == 0:
ans = max(ans, d_r[nt])
elif i == nt:
ans = max(ans, ns - d_l[nt] - 1)
else:
ans = max(ans, d_r[nt-i] - d_l[i] - 1)
print(ans)
```
|
instruction
| 0
| 4,247
| 0
| 8,494
|
Yes
|
output
| 1
| 4,247
| 0
| 8,495
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
'''input
asdfasdf
fasd
'''
import sys
from collections import defaultdict as dd
mod=10**9+7
def ri(flag=0):
if flag==0:
return [int(i) for i in sys.stdin.readline().split()]
else:
return int(sys.stdin.readline())
st = input()
pat = input()
n= len(st)
forward = [0 for i in range(n)]
idx =0
for i in range(n):
if idx < len(pat) and st[i] == pat[idx]:
idx+=1
forward[i] = idx
backward = [0 for x in range(n)]
idx =len(pat)-1
for i in range(n-1,-1,-1):
if idx>=0 and st[i] == pat[idx]:
idx-=1
backward[i]= idx+2
# print(forward)
# print(backward)
c1 =dd(int)
c2 = dd(int)
for i in range(n):
c1[forward[i]] = i+1
c2[backward[i]] = i+1
ans = max(c2[1]-1 , n-c1[len(pat)])
#print(c1,c2)
for i in range(1,len(pat)):
ans = max(ans , abs(c1[i] - c2[i+1])-1)
print(ans)
```
|
instruction
| 0
| 4,248
| 0
| 8,496
|
Yes
|
output
| 1
| 4,248
| 0
| 8,497
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
from collections import deque
def substr_idx(s, t, reverse=False):
idx = deque()
i = 0
rng = range(len(s))
if reverse:
rng = reversed(rng)
for k in rng:
if i >= len(t):
break
if reverse and s[k] == t[-i-1]:
idx.appendleft(k)
i += 1
elif not reverse and s[k] == t[i]:
idx.append(k)
i += 1
return idx
s = input()
t = input()
idleft = substr_idx(s, t)
idright = substr_idx(s, t, reverse=True)
m = max(idright[0], len(s) - idleft[-1] - 1)
for i in range(1, len(idleft)):
m = max(m, idright[i]-idleft[i-1]-1)
print(m)
```
|
instruction
| 0
| 4,249
| 0
| 8,498
|
Yes
|
output
| 1
| 4,249
| 0
| 8,499
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
from collections import defaultdict as DD
from bisect import bisect_left as BL
from bisect import bisect_right as BR
from itertools import combinations as IC
from itertools import permutations as IP
from random import randint as RI
import sys
import math
MOD=pow(10,9)+7
from math import gcd
def IN(f=0):
if f==0:
return ( [int(i) for i in sys.stdin.readline().split()] )
else:
return ( int(sys.stdin.readline()) )
a=input()
b=input()
n=len(a)
m=len(b)
q=[]
w=[]
j=0
d1=DD(int)
d2=DD(int)
for i in range(len(a)):
if j>=len(b):
q.append(0)
elif a[i]==b[j]:
q.append(j+1)
j+=1
else:
q.append(0)
j=len(b)-1
for i in range(len(a)-1,-1,-1):
if j<0:
w.append(0)
elif a[i]==b[j]:
w.append(j+1)
j-=1
else:
w.append(0)
w.reverse()
for i in range(len(a)):
d1[q[i]]=i+1
for i in range(len(a)):
d2[w[i]]=i+1
ans = max(d2[1]-1,len(a)-d1[len(b)])
for i in range(1,m):
tr = abs(d1[i]- d2[i+1])-1
if ans<tr:
ans=tr
#print(tr)
print(ans)
```
|
instruction
| 0
| 4,250
| 0
| 8,500
|
Yes
|
output
| 1
| 4,250
| 0
| 8,501
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
def process(s, t):
l=[]
r=[0]*len(t)
j=0
for i in range(len(t)):
while s[j]!=t[i]:
j+=1
l.append(j)
j+=1
j=len(s)-1
for i in reversed(range(len(t))):
while s[j]!=t[i]:
j-=1
r[i]=j
j-=1
res=max(r[0], len(s)-1-l[-1])
for i in range(len(t)):
res=max(res, r[i]-l[i])
return res
s=input()
t=input()
print(process(s,t))
```
|
instruction
| 0
| 4,251
| 0
| 8,502
|
No
|
output
| 1
| 4,251
| 0
| 8,503
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
z,zz=input,lambda:list(map(int,z().split()))
zzz=lambda:[int(i) for i in stdin.readline().split()]
szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz())
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if (x%i==0 and x!=2) or x==0:return 0
return 1
def dfs(u,visit,graph):
visit[u]=True
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
###########################---Test-Case---#################################
"""
"""
###########################---START-CODING---##############################
s=z()
t=z()
ans=0
lst=[0]*len(t)
for i in range(len(t)-1,-1,-1):
pos=len(s)-1
if (i+1<len(t)):
pos=lst[i+1]-1
while s[pos]!=t[i]:
pos-=1
lst[i]=pos
ans=0
pos=0
for i in range(len(s)):
rpos=len(s)-1
if pos<len(t):
rpos-=1
ans=max(ans,rpos-i+1)
if pos<len(t) and t[pos]==s[i]:
pos+=1
print(ans)
exit()
for i in range(len(s)):
for j in range(i,len(s)):
pos=0
for p in range(len(s)):
if i<=p and p<=j:continue
if pos<len(t) and t[pos]==s[p]:
pos+=1
if pos==len(t) :
ans=max(ans,j-i+1)
print(ans)
```
|
instruction
| 0
| 4,252
| 0
| 8,504
|
No
|
output
| 1
| 4,252
| 0
| 8,505
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
s=input()
t=input()
sl,tl=len(s),len(t)
left=[]
right=[]
x=0
for i in range(sl):
if x<tl and s[i]==t[x]:
left.append(i)
x+=1
x=tl-1
for i in range(sl-1,-1,-1):
if x>=0 and s[i]==t[x]:
right.append(i)
x-=1
right.reverse()
if(tl==1):
print(max(max(left[0],sl-left[0]-1),max(right[0],sl-right[0]-1)))
else:
ans=max(left[-1],right[0])
for i in range(1,tl):
ans=max(ans,right[i]-left[i-1]-1)
print(ans)
```
|
instruction
| 0
| 4,253
| 0
| 8,506
|
No
|
output
| 1
| 4,253
| 0
| 8,507
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2 ⋅ 10^5 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3
Submitted Solution:
```
from sys import stdin,stdout
s=stdin.readline().strip()
t=stdin.readline().strip()
leftmost=[-1]
ptr1=ptr2=0
while ptr2<len(t):
if s[ptr1]==t[ptr2]:
leftmost.append(ptr1)
ptr1+=1;ptr2+=1
else:
ptr1+=1
rightmost=[]
ptr1=len(s)-1
ptr2=len(t)-1
while ptr2>=0:
if s[ptr1]==t[ptr2]:
rightmost.append(ptr1)
ptr1-=1;ptr2-=1
else:
ptr1-=1
rightmost.reverse();rightmost.append(len(s))
ans=1<<64
for i in range(len(leftmost)-1):
ans=max(ans,leftmost[i]+rightmost[i+1]-1)
stdout.write(str(ans)+"\n")
#print(leftmost)
#print(rightmost)
```
|
instruction
| 0
| 4,254
| 0
| 8,508
|
No
|
output
| 1
| 4,254
| 0
| 8,509
|
Provide a correct Python 3 solution for this coding contest problem.
C: Only one subsequence --Unique Subsequence-
problem
One day Ebi-chan noticed that a text string T of length n and a pattern string P (m \ leq n) of length m were placed on the desk. Ebi-chan loves the "only one subsequence" that appears in strings, so she immediately began investigating whether P was the only subsequence of T.
The property that P is the only subsequence of T is expressed as follows: Now write the i-th character of the string X as X_i.
* A sequence of length m S = (s_1, ..., s_m) (where s_1 <s_2 <... <s_m) and for each i (i = 1, ..., m) T_ {s_i} The sequence S with = P_i is uniquely determined.
For several hours after starting the investigation, Ebi-chan was staring at the string, but it seems that she was tired because the string was too long. If you couldn't see it, you decided to help Ebi-chan. For Ebi-chan, write a program that outputs “yes” if P is only one subsequence of T, and “no” otherwise.
Input format
T
P
The first line is given the text string T. The second line is given the pattern string P.
Constraint
* 1 \ leq | P | \ leq | T | \ leq 5 \ times 10 ^ 5
* T and P are composed of lowercase letters ‘a’-’z’.
Output format
Print “yes” if P is a single subsequence of T, otherwise “no” on one line.
Input example 1
aizucamp
azu
Output example 1
yes
Input example 2
abracadabra
rada
Output example 2
no
Input example 3
hokkaido
dekai
Output example 3
no
Example
Input
aizucamp
azu
Output
yes
|
instruction
| 0
| 4,990
| 0
| 9,980
|
"Correct Solution:
```
t=input()
p=input()
mae=[0]*len(p)
cnt=0
for i in range(len(t)):
if t[i]==p[cnt]:
mae[cnt]=i
cnt+=1
if cnt==len(p):
break
if cnt!=len(p):print("no");exit()
cnt-=1
for j in range(len(t)-1,-1,-1):
if t[j]==p[cnt]:
if mae[cnt]!=j:print("no");exit()
cnt-=1
if cnt==-1:break
if cnt!=-1:print("no")
else:print("yes")
```
|
output
| 1
| 4,990
| 0
| 9,981
|
Provide a correct Python 3 solution for this coding contest problem.
C: Only one subsequence --Unique Subsequence-
problem
One day Ebi-chan noticed that a text string T of length n and a pattern string P (m \ leq n) of length m were placed on the desk. Ebi-chan loves the "only one subsequence" that appears in strings, so she immediately began investigating whether P was the only subsequence of T.
The property that P is the only subsequence of T is expressed as follows: Now write the i-th character of the string X as X_i.
* A sequence of length m S = (s_1, ..., s_m) (where s_1 <s_2 <... <s_m) and for each i (i = 1, ..., m) T_ {s_i} The sequence S with = P_i is uniquely determined.
For several hours after starting the investigation, Ebi-chan was staring at the string, but it seems that she was tired because the string was too long. If you couldn't see it, you decided to help Ebi-chan. For Ebi-chan, write a program that outputs “yes” if P is only one subsequence of T, and “no” otherwise.
Input format
T
P
The first line is given the text string T. The second line is given the pattern string P.
Constraint
* 1 \ leq | P | \ leq | T | \ leq 5 \ times 10 ^ 5
* T and P are composed of lowercase letters ‘a’-’z’.
Output format
Print “yes” if P is a single subsequence of T, otherwise “no” on one line.
Input example 1
aizucamp
azu
Output example 1
yes
Input example 2
abracadabra
rada
Output example 2
no
Input example 3
hokkaido
dekai
Output example 3
no
Example
Input
aizucamp
azu
Output
yes
|
instruction
| 0
| 4,991
| 0
| 9,982
|
"Correct Solution:
```
t = input()
p = input()
ok = 1
result1 = [0] * len(p); idx = 0
for i, c in enumerate(t):
if idx < len(p) and c == p[idx]:
result1[idx] = i
idx += 1
if idx < len(p):
ok = 0
result2 = [0] * len(p); idx = len(p)-1
for i in range(len(t)-1, -1, -1):
c = t[i]
if idx >= 0 and c == p[idx]:
result2[idx] = i
idx -= 1
if idx >= 0:
ok = 0
if ok and result1 == result2:
print("yes")
else:
print("no")
```
|
output
| 1
| 4,991
| 0
| 9,983
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,013
| 0
| 10,026
|
Tags: brute force, implementation
Correct Solution:
```
n, m, q = map(int, input().split())
s = input()
t = input()
flag = [0]*(1007)
prefix = [0]*(1007)
for i in range(n-m+1):
f = 1
for j in range(m):
if s[i+j] != t[j]:
f = 0
flag[i]= f
prefix[i+1]= prefix[i]+flag[i]
for i in range(max(0,n-m+1), n):
prefix[i+1] = prefix[i]
for _ in range(q):
l, r = map(int, input().split())
l -= 1
r -= (m - 1)
print(prefix[r] - prefix[l]) if r >= l else print(0)
```
|
output
| 1
| 5,013
| 0
| 10,027
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,014
| 0
| 10,028
|
Tags: brute force, implementation
Correct Solution:
```
string = ""
array = []
n, m, q = map(int, input().split())
s = input()
t = input()
for i in range(0, n - m + 1) :
if s[ i : i + m] == t :
string += '1'
else :
string += '0'
for i in range(0, q) :
a, b = map(int, input().split())
if b - a + 1 >= m :
array.append((string[ a - 1: b - m + 1]).count('1'))
else :
array.append(0)
for i in range(0, q) :
print(array[i], end = '\n')
```
|
output
| 1
| 5,014
| 0
| 10,029
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,015
| 0
| 10,030
|
Tags: brute force, implementation
Correct Solution:
```
n, m, q = [int(i) for i in input().split()]
s = input()
t = input()
positions = ''
for i in range(n-m+1):
if s[i:i + m] == t:
positions += '1'
else:
positions += '0'
for i in range(q):
l,r = map(int, input().split())
if r - l + 1 >= m:
print (positions[l-1:r-m+1].count('1'))
else:
print(0)
```
|
output
| 1
| 5,015
| 0
| 10,031
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,016
| 0
| 10,032
|
Tags: brute force, implementation
Correct Solution:
```
n,m,q = map(int,input().split())
s = input()
t = input()
l = []
r = []
for i in range(n-m+1):
if s[i:i+m] == t:
l.append(i)
r.append(i+m-1)
for i in range(q):
x,y = map(int,input().split())
x-=1
y-=1
ans = 0
for j in range(len(l)):
if x <= l[j] and y >= r[j]:
ans+=1
print(ans)
```
|
output
| 1
| 5,016
| 0
| 10,033
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,017
| 0
| 10,034
|
Tags: brute force, implementation
Correct Solution:
```
def read():
return int(input())
def readlist():
return list(map(int, input().split()))
def readmap():
return map(int, input().split())
n, m, q = readmap()
S = input()
T = input()
L = []
R = []
for _ in range(q):
l, r = readmap()
L.append(l)
R.append(r)
left = [0] * n
right = [0] * n
for i in range(n-m+1):
if S[i:i+m] == T:
for j in range(i+1, n):
left[j] += 1
for j in range(i+m-1, n):
right[j] += 1
for i in range(q):
l, r = L[i], R[i]
print(max(0, right[r-1] - left[l-1]))
```
|
output
| 1
| 5,017
| 0
| 10,035
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,018
| 0
| 10,036
|
Tags: brute force, implementation
Correct Solution:
```
n,m,q = map(int, input().split())
s = input()
t =input()
res=""
if n>=m:
for i in range(n):
if (s[i:i+m]==t):
res+="1"
else:
res+="0"
for i in range(q):
c0,c1 = map(int, input().split())
if (m>n) or (c1-c0+1<m):
print (0)
else :
print (res[c0-1:c1-m+1].count("1"))
```
|
output
| 1
| 5,018
| 0
| 10,037
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,019
| 0
| 10,038
|
Tags: brute force, implementation
Correct Solution:
```
n,m,q=map(int,input().split())
s,t=input(),input()
a=[0,0]
b=0
for i in range(n):
b+=s[i:i+m]==t
a+=[b]
for _ in[0]*q:
l,r=map(int,input().split())
print(a[max(l,r-m+2)]-a[l])
```
|
output
| 1
| 5,019
| 0
| 10,039
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
|
instruction
| 0
| 5,020
| 0
| 10,040
|
Tags: brute force, implementation
Correct Solution:
```
n, m, q = map(int, input().split())
s = input()
t = input()
a = [0] * n
for i in range(n - m + 1):
if s[i:i + m] == t:
a[i] = 1
for i in range(q):
k = 0
l, r = map(int, input().split())
for j in range(l - 1, r):
if a[j] == 1 and j + m - 1 < r:
k += 1
print(k)
```
|
output
| 1
| 5,020
| 0
| 10,041
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
import bisect
f=lambda: map(int, input().split())
n,m,q=f()
s,t=input(),input()
x,st=[],[]
c1=0
for i in range(n):
if "".join(c for c in s[i:i+m])==t:
x.append(i+1)
c1+=1
for i in range(q):
cnt=0
l,r=f()
if x!=[] and r-l+1>=m:
r=r-m+1
left=bisect.bisect_left(x,l)
right=bisect.bisect_left(x,r)
if right>=c1: right-=1
if x[right]>r: right-=1
cnt=right-left+1
st.append(str(cnt))
print('\n'.join(st))
```
|
instruction
| 0
| 5,021
| 0
| 10,042
|
Yes
|
output
| 1
| 5,021
| 0
| 10,043
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
# n=int(input())
# ns=[int(x) for x in input().split()]
# dp=[None]*n
# def greater(i,num):
# return ns[i]+ns[i+1]>=num
# def biSearch(t,l,r):
# if r-l<=1:
# return l
# m=(l+r)//2
# if greater(m,t)
#
#
# def update(t):
# l=ns[t]
n,m,q=[int(x)for x in input().split()]
sn=input()
sm=input()
def eq(i):
for j in range(m):
if sn[i+j]!=sm[j]:
return False
return True
re=[0]*n
for i in range(n-m+1):
if eq(i):
re[i]=1
for i in range(1,n):
re[i]+=re[i-1]
for i in range(q):
l,r=[int(x)-1 for x in input().split()]
if r-l+1<m:
print(0)
continue
if l==0:
print(re[r-m+1])
else:
print(re[r-m+1]-re[l-1])
```
|
instruction
| 0
| 5,022
| 0
| 10,044
|
Yes
|
output
| 1
| 5,022
| 0
| 10,045
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
n, m, q = list(map(int, input().strip().split()))
s = input()
t = input()
locs = []
for i in range(n):
if i + m <= n and s[i:i+m] == t:
locs.append(i)
#print(locs)
def find_max(nums, target):
l, r = 0, len(nums) - 1
while l < r:
mid = (l + r) // 2
if nums[mid] >= target:
r = mid
else:
l = mid + 1
if nums[l] >= target:
return l
else:
return len(nums)
def helper(l, r, nums):
if len(nums) == 0:
return 0
x = find_max(nums, l)
y = find_max(nums, r)
if y >= len(nums) or nums[y] > r:
return y - x
else:
return y - x + 1
for i in range(q):
l, r = list(map(int, input().strip().split()))
l -= 1
r -= 1
if r - m + 1 < l:
print(0)
else:
res = helper(l, r - m + 1, locs)
print(res)
```
|
instruction
| 0
| 5,023
| 0
| 10,046
|
Yes
|
output
| 1
| 5,023
| 0
| 10,047
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
n,m,q=map(int,input().split())
s=input()
t=input()
k=[0]*(n+1)
for i in range(1,n+1):
k[i]=k[i-1]
if s[i-1:i+m-1]==t:k[i]+=1
for i in range(q):
l,r=map(int,input().split())
print(k[max(l-1,r-m+1)]-k[l-1])
```
|
instruction
| 0
| 5,024
| 0
| 10,048
|
Yes
|
output
| 1
| 5,024
| 0
| 10,049
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
n, m, q = map(int, input().split())
s = input()
t = input()
d = []
axis = [0]*n
for i in range(0, n-m+1):
if s[i:i+m] == t:
for x in range(i, i+m):
axis[x] = 1
for i in range(q):
ans = 0
l, r = map(int, input().split())
print(sum(axis[l-1:r])//m)
```
|
instruction
| 0
| 5,025
| 0
| 10,050
|
No
|
output
| 1
| 5,025
| 0
| 10,051
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
from sys import stdin
def main():
n, m , q = [ int(s) for s in stdin.readline().split(" ")]
s = stdin.readline().strip()
t = stdin.readline().strip()
queries = []
for line in range(0, q) :
queries.append( [ int(s) for s in stdin.readline().split(" ")])
for query in queries:
if( m <= query[1] - query[0]):
print(0)
else:
cont = 0
for index in range(query[0], (query[1] - m + 2)):
if(s[index-1:index+m-1] == t):
cont+=1
print(cont)
main()
```
|
instruction
| 0
| 5,026
| 0
| 10,052
|
No
|
output
| 1
| 5,026
| 0
| 10,053
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
string = ""
array = []
n, m, q = map(int, input().split())
s = input()
t = input()
for i in range(0, n - m + 1) :
if s[ i : i + m] == t :
string += '1'
else :
string += '0'
for i in range(0, q) :
a, b = map(int, input().split())
if a - b + 1 >= m :
array.append(string[ a - 1: b - m + 1].count("1"))
else :
array.append(0)
for i in range(0, q) :
print(array[i])
```
|
instruction
| 0
| 5,027
| 0
| 10,054
|
No
|
output
| 1
| 5,027
| 0
| 10,055
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Submitted Solution:
```
import sys
N, M, Q = tuple(map(int, input().split()))
S = input()
T = input()
def FindT1(S, T, M, i, j):
A = S[i-1:j]
ans = 0
for k in range(j-i+2-M):
if A[k: k+M] == T:
ans = ans + 1
return ans
def FindT2(S, T, N, M):
ans = []
for k in range(N-M +1):
if S[k:k+M] == T:
ans.append(1)
else:
ans.append(0)
return ans
if Q <= 20:
for t in range(Q):
i,j = tuple(map(int, sys.stdin.readline().split()))
print(FindT1(S, T, M, i, j))
else:
ans = FindT2(S, T, N, M)
print(ans)
for t in range(Q):
i,j = tuple(map(int, sys.stdin.readline().split()))
if j-i+1 < M:
print("hey")
else:
print(sum(ans[i-1:j+1-M]))
```
|
instruction
| 0
| 5,028
| 0
| 10,056
|
No
|
output
| 1
| 5,028
| 0
| 10,057
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,539
| 0
| 11,078
|
Tags: binary search, greedy, implementation
Correct Solution:
```
def check(mid,ha,has,c):
need=0
for i in range(26):
need+=max(0,ha[i]*mid-has[i])
# print(mid,need)
return c>=need
ss='abcdefghijklmnopqrstuvwxyz'
s = list(input())
t = list(input())
ha=[0]*26
has=[0]*26
m=10000000000
c=0
for i in t:
ha[ord(i)-ord('a')]+=1
for i in s:
if i=='?':
c+=1
else:
has[ord(i)-ord('a')]+=1
l=0
r=10000000000
#print(has,ha)
while l<=r:
mid = l + (r-l)//2
if(check(mid,ha,has,c)):
l=mid+1
else:
r=mid-1
j=0
ll=len(s)
for i in range(26):
req=max(0,ha[i]*r-has[i])
while j<ll and req!=0:
if s[j]=='?':
s[j]=ss[i]
req-=1
j+=1
for i in range(ll):
if s[i]=='?':
s[i]='a'
print(''.join(s))
```
|
output
| 1
| 5,539
| 0
| 11,079
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,540
| 0
| 11,080
|
Tags: binary search, greedy, implementation
Correct Solution:
```
from collections import Counter
A = input()
AC = Counter(A)
B = input()
BC = Counter(B)
letters = [chr(ord('a') + i) for i in range(ord('z') - ord('a') + 1)]
result = 0
a, b, c = 0, len(A) + 1, 0
#for k in range(len(A) - len(B) + 1):
# needed = 0
# for x in letters:
# needed += max([0, k * BC[x] - AC[x]])
#
# if needed <= AC['?']:
# result = k
# else:
# break
while a < b:
c = (a + b) // 2
needed = 0
for x in letters:
needed += max([0, c * BC[x] - AC[x]])
if needed <= AC['?']:
a = c + 1
else:
b = c
result = a - 1
C = list(A)
i = 0
for x in letters:
cneed = max([0, result * BC[x] - AC[x]])
while cneed > 0:
if C[i] == '?':
C[i] = x
cneed -= 1
i += 1
while i < len(A):
if C[i] == '?':
C[i] = 'z'
i += 1
print(''.join(C))
```
|
output
| 1
| 5,540
| 0
| 11,081
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,541
| 0
| 11,082
|
Tags: binary search, greedy, implementation
Correct Solution:
```
def main():
s = input()
t = input()
nd = {}
hv = {}
for x in t:
if x not in nd:
nd[x]=1
else:
nd[x]+=1
for x in s:
if x not in hv:
hv[x]=1
else:
hv[x]+=1
def f(n):
hp = 0
if("?" in hv):
hp += hv["?"]
for x in nd:
if x not in hv:
hp -=nd[x]*n
else:
if( hv[x] - nd[x]*n>=0):
hp +=0
else:
hp +=hv[x] - nd[x]*n
if(hp<0):
return False
else:
return True
l =0
r = len(s)+100
while(r-1!=l):
mid = (l+r)//2
if(f(mid)):
l = mid
else:
r = mid
tmp = ''
for x in nd:
now = hv[x] if x in hv else 0
if now < nd[x]*l:
q = nd[x]*l - now
tmp += x * q
s = list(s)
p = 0
for x in range(len(s)):
if s[x] == "?":
if p == len(tmp):
s[x] = "a"
else:
s[x] = tmp[p]
p += 1
print("".join(s))
main()
```
|
output
| 1
| 5,541
| 0
| 11,083
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,542
| 0
| 11,084
|
Tags: binary search, greedy, implementation
Correct Solution:
```
def solve(mid):
tmp = 0
for i in cnt_t:
if cnt_t[i] * mid <= cnt_s[i]:
pass
else:
tmp += cnt_t[i] * mid - cnt_s[i]
return tmp <= cnt_s["?"]
def solve2(mid):
tmp = deque([])
for i in cnt_t:
if cnt_t[i] * mid <= cnt_s[i]:
pass
else:
for _ in range(cnt_t[i] * mid - cnt_s[i]):
tmp.append(i)
return tmp
from collections import deque
import sys
input = sys.stdin.readline
s = input()[:-1]
t = input()[:-1]
cnt_s = {"?": 0}
cnt_t = {}
for i, char in enumerate(t):
if char not in cnt_t:
cnt_t[char] = 1
cnt_s[char] = 0
else:
cnt_t[char] += 1
for i, char in enumerate(s):
if char not in cnt_s:
cnt_s[char] = 1
else:
cnt_s[char] += 1
ok = -1
ng = 10 ** 9
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
s = list(s)
q = solve2(ok)
for i in range(len(s)):
if s[i] == "?":
if q:
s[i] = q.pop()
else:
s[i] = "a"
print("".join(s))
```
|
output
| 1
| 5,542
| 0
| 11,085
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,543
| 0
| 11,086
|
Tags: binary search, greedy, implementation
Correct Solution:
```
s = input()
t = input()
abc = 'abcdefghijklmnopqrstuvwxyz'
dabc ={}
for i in range(26):
dabc[abc[i]] = i
lt = {}
ls = {}
dd = {}
ls['?'] = 0
for i in abc:
lt[i] = 0
ls[i] = 0
dd[i] = 0
for letter in t:
lt[letter] += 1
for letter in s:
ls[letter] +=1
X = ls['?']
def check(ans):
global ls, lt, abc, X
return -sum(min(0, ls[l] - lt[l] * ans) for l in abc) <= X
start, end = [0, 2000000]
i = 0
while start < end:
st = start + end
ans = (st + st%2)//2
if check(ans):
start = ans
else:
end = ans - 1
ans = start
for letter in abc:
dd[letter] = max(0, lt[letter] * ans - ls[letter])
X -= max(0, lt[letter] * ans - ls[letter])
s1 = ['']
j = 0
for i in s:
if i != '?':
s1.append(i)
else:
try:
while dd[abc[j]] == 0:
j +=1
s1.append(abc[j])
dd[abc[j]] -= 1
except:
s1.append('z')
print(''.join(s1))
```
|
output
| 1
| 5,543
| 0
| 11,087
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,544
| 0
| 11,088
|
Tags: binary search, greedy, implementation
Correct Solution:
```
s = list(input())
t = input()
abc = 'abcdefghijklmnopqrstuvwxyz'
dabc ={}
for i in range(26):
dabc[abc[i]] = i
lt = {}
ls = {}
dd = {}
ls['?'] = 0
for i in abc:
lt[i] = 0
ls[i] = 0
dd[i] = 0
for letter in t:
lt[letter] += 1
for letter in s:
ls[letter] +=1
X = ls['?']
def check(ans):
global ls, lt, abc, X
return -sum(min(0, ls[l] - lt[l] * ans) for l in abc) <= X
start, end = [0, 2000000]
i = 0
while start < end:
st = start + end
ans = (st + st%2)//2
if check(ans):
start = ans
else:
end = ans - 1
ans = start
for letter in abc:
dd[letter] = max(0, lt[letter] * ans - ls[letter])
X -= max(0, lt[letter] * ans - ls[letter])
j = 0
for i in range(len(s)):
if s[i] == '?':
try:
while dd[abc[j]] == 0:
j +=1
s[i] = abc[j]
dd[abc[j]] -= 1
except:
s[i] = 'z'
print(''.join(s))
```
|
output
| 1
| 5,544
| 0
| 11,089
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,545
| 0
| 11,090
|
Tags: binary search, greedy, implementation
Correct Solution:
```
from collections import Counter
import sys
input = sys.stdin.readline
s = list(input().rstrip())
t = input().rstrip()
cnt1 = Counter(s)
cnt2 = Counter(t)
if cnt1['?'] == 0:
print(*s, sep='')
exit()
ok, ng = 0, 10**9
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
hatena = cnt1['?']
for k, v in cnt2.items():
hatena -= max(0, v*mid - cnt1[k])
if hatena >= 0:
ok = mid
else:
ng = mid
keys = list(cnt2.keys())
for i in range(len(s)):
if s[i] == '?':
while keys and cnt2[keys[-1]] * ok <= cnt1[keys[-1]]:
keys.pop()
if keys:
s[i] = keys[-1]
cnt1[keys[-1]] += 1
else:
s[i] = 'z'
print(*s, sep='')
```
|
output
| 1
| 5,545
| 0
| 11,091
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
|
instruction
| 0
| 5,546
| 0
| 11,092
|
Tags: binary search, greedy, implementation
Correct Solution:
```
# @oj: codeforces
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-11-18 19:10
# @url:https://codeforc.es/contest/825/problem/D
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio
BUFSIZE = 8192
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
## 注意嵌套括号!!!!!!
## 先有思路,再写代码,别着急!!!
## 先有朴素解法,不要有思维定式,试着换思路解决
## 精度 print("%.10f" % ans)
## sqrt:int(math.sqrt(n))+1
## 字符串拼接不要用+操作,会超时
## 二进制转换:bin(1)[2:].rjust(32,'0')
## array copy:cur=array[::]
## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200
def main():
s=str(input())
t=str(input())
cnt=[0]*26
for i in range(len(t)):
cnt[ord(t[i])-ord('a')]+=1
q=s.count('?')
if q==0:
print (s)
return
cnt1=[0]*26
for i in range(len(s)):
if s[i]!='?':
cnt1[ord(s[i])-ord('a')]+=1
res=[0]*26
while q>0:
for i in range(26):
if cnt1[i]>=cnt[i]:
cnt1[i]-=cnt[i]
else:
if q>=cnt[i]-cnt1[i]:
q-=(cnt[i]-cnt1[i])
res[i]+=(cnt[i]-cnt1[i])
cnt1[i]=0
else:
res[i]+=q
q=0
break
# print (res)
ans=list(s)
for i in range(len(ans)):
if ans[i]=='?':
j=0
while j<26:
if res[j]>0:
ans[i]=chr(ord('a')+j)
res[j]-=1
break
j+=1
print ("".join(ans))
if __name__ == "__main__":
main()
```
|
output
| 1
| 5,546
| 0
| 11,093
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
Input
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Output
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from queue import Queue
import collections
import itertools
import bisect
import heapq
# sys.setrecursionlimit(100000)
# ^^^TAKE CARE FOR MEMORY LIMIT^^^
import random
def main():
pass
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def primeFactorsCount(n):
cnt=0
while n % 2 == 0:
cnt+=1
n = n // 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
cnt+=1
n = n // i
if n > 2:
cnt+=1
return (cnt)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c = 0
while (n % 2 == 0):
n //= 2
c += 1
return c
def seive(n):
primes = [True] * (n + 1)
primes[1] = primes[0] = False
i = 2
while (i * i <= n):
if (primes[i] == True):
for j in range(i * i, n + 1, i):
primes[j] = False
i += 1
pr = []
for i in range(0, n + 1):
if (primes[i]):
pr.append(i)
return pr
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def denofactinverse(n, m):
fac = 1
for i in range(1, n + 1):
fac = (fac * i) % m
return (pow(fac, m - 2, m))
def numofact(n, m):
fac = 1
for i in range(1, n + 1):
fac = (fac * i) % m
return (fac)
def sod(n):
s = 0
while (n > 0):
s += n % 10
n //= 10
return s
def getChar():
for i in range(0,26):
if(rt[i]>0):
rt[i]-=1
return chr(i+97)
return ""
def getVal(mid):
req=0
for i in range(0,26):
req+=max(0,mid*ct[i]-cs[i])
return(req)
s=list(input())
t=list(input())
cs,ct=[0]*26,[0]*26
cq=0
for i in s:
if(i!="?"):
cs[ord(i)-97]+=1
else:
cq+=1
for i in t:
ct[ord(i)-97]+=1
l,r=0,(len(s)+len(t)-1)//len(t)
while(l<=r):
mid=(l+r)//2
if(getVal(mid)<=cq):
l=mid+1
else:
r=mid-1
rt=[0]*26
for i in range(0,26):
if(r*ct[i]-cs[i]>0):
rt[i]=r*ct[i]-cs[i]
n=len(s)
#print(rt)
for i in range(0,n):
if(s[i]=="?"):
temp=getChar()
if(temp):
s[i]=temp
else:
break
anss="".join(s)
anss=anss.replace("?","a")
print(anss)
```
|
instruction
| 0
| 5,547
| 0
| 11,094
|
Yes
|
output
| 1
| 5,547
| 0
| 11,095
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.