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 |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12 | instruction | 0 | 74,542 | 0 | 149,084 |
Tags: dp, strings
Correct Solution:
```
def main():
s, t = input(), input()
n, m = len(s), len(t)
t = '$'.join((t, s))
p = [0]
k = 0
for i in range(1, n + m + 1):
while k and t[k] != t[i]:
k = p[k - 1]
if t[k] == t[i]:
k += 1
p.append(k)
ans = [0] * n
sums = [0] * (n + 1)
curs = 0
was = False
j = 0
MOD = 1000000007
for i in range(n):
if p[i + m + 1] == m:
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = (sums[i - 1] + ans[i]) % MOD
print(sum(ans) % MOD)
if __name__ == '__main__':
main()
``` | output | 1 | 74,542 | 0 | 149,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
def main():
s, t = input(), input()
n, m = len(s), len(t)
ans = [0] * n
sums = [0] * (n + 1)
curs = j = 0
was = False
MOD = 1000000007
idx = s.find(t) + m - 1
ll = []
for i in range(n):
if i == idx:
ll.append(idx)
idx = s.find(t, i - m + 2)
if idx < 0:
idx = n
idx += m - 1
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = (sums[i - 1] + ans[i]) % MOD
print(ll)
print(sum(ans) % MOD)
if __name__ == '__main__':
main()
``` | instruction | 0 | 74,543 | 0 | 149,086 |
No | output | 1 | 74,543 | 0 | 149,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
def main():
s, t = input(), input()
n, m = len(s), len(t)
ans = [0] * n
sums = [0] * (n + 1)
curs = j = 0
was = False
MOD = 1000000007
idx = s.find(t)
for i in range(n):
if i == idx:
idx = s.find(t, i + 1)
if idx < 0:
idx = n
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = (sums[i - 1] + ans[i]) % MOD
print(sum(ans) % MOD)
if __name__ == '__main__':
main()
``` | instruction | 0 | 74,544 | 0 | 149,088 |
No | output | 1 | 74,544 | 0 | 149,089 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,194 | 0 | 150,388 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
import sys
MOD = 1000000007
def red(x):
return x if x < MOD else x - MOD
S = input()
n = len(S)
i = 0
while i < n and S[i] == '0':
i += 1
if i == n:
print (n)
sys.exit()
j = n
while j and S[j - 1] == '0':
j -= 1
fac = (i + 1) * (n - j + 1)
S = S[i:j]
n = len(S)
count0 = [+(c == '0') for c in S]
for i in reversed(range(n - 1)):
if count0[i]:
count0[i] += count0[i + 1]
ways0 = 1
ways1 = 0
buckets = [0] * (n + 1)
buckets[0] = 1
for i in reversed(range(n)):
if count0[i]:
c = count0[i]
ways0 = red(ways0 - buckets[c] + MOD)
buckets[c] = ways1
ways0 = red(ways0 + ways1)
else:
ways1 = red(ways1 + ways0)
print (ways1 * fac % MOD)
``` | output | 1 | 75,194 | 0 | 150,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,195 | 0 | 150,390 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
MOD = int(1e9+7)
was = set()
s = list(map(int, minp()))
n = len(s)
first, last = None, None
for i in range(n):
v = s[i]
if v == 1:
first = i
break
if first is None:
print(n)
return
dp = [0]*n
dp[first] = 1
st = []
stv = []
r = 0
i = first
while i < n:
j = i + 1
while j < n and s[j] == 0:
j += 1
r = (r + dp[i]) % MOD
if j == n:
last = i
break
c = j - i - 1
add = 0
st.append(0)
stv.append(dp[i])
while len(st) > 0 and st[-1] <= c:
v = st.pop()
d = stv.pop()
dp[j] = (dp[j] + d*(c - v + 1)) % MOD
add += d
if len(st) > 0 and st[-1] == c + 1:
stv[-1] = (stv[-1] + add) % MOD
else:
st.append(c + 1)
stv.append(add)
i = j
print((r * (first+1) * (n-last)) % MOD)
solve()
``` | output | 1 | 75,195 | 0 | 150,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,196 | 0 | 150,392 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
Start=1
End=1
if data[0][0]=="0":
Start+=data[0][1]
data=data[1:]
if not data:
print(Start-1)
exit()
if data[-1][0]=="0":
End+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*Start*End)
exit()
odd=[(comp[i],i//2) for i in range(n) if i%2==1]
#print(odd)
N=len(odd)
ID=[-1]*n
stack=[]
for i in range(N):
while stack and stack[-1][0]<odd[i][0]:
stack.pop()
if stack:
ID[2*i+1]=stack[-1][1]
stack.append(odd[i])
#print(ID[1::2])
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
for i in range(2,n,2):
id=ID[i-1]
if id==-1:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
#prel[i-1]=1
else:
j=2*id+1
#res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
res=(comp[i-1]*dp[i-2]+pre[j]-comp[i-1]*dp[j-1])%mod
pre[i-1]=res
#prel[i-1]=prel[j]+1
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
#print(pre)
#print(dp)
print((dp[-1]*Start*End)%mod)
``` | output | 1 | 75,196 | 0 | 150,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,197 | 0 | 150,394 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
MOD = 1000000007
def main():
s = input()
n = len(s)
zero = [0] * n
for i in range(n):
if s[i] == '0':
zero[i] = zero[i-1] + 1 if i else 1
nxt, dp = [0] * (n+2), [0] * (n+2)
for i in range(n+1):
nxt[i] = n
for i in range(n-1, -1, -1):
dp[i] = ((zero[i] <= zero[n-1]) + dp[nxt[0]] + dp[nxt[zero[i] + 1]]) % MOD
nxt[zero[i]] = i
ans = n if nxt[0] >= n else dp[nxt[0]] * (nxt[0] + 1) % MOD
print(ans)
main()
``` | output | 1 | 75,197 | 0 | 150,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,198 | 0 | 150,396 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
Start=1
End=1
if data[0][0]=="0":
Start+=data[0][1]
data=data[1:]
if not data:
print(Start-1)
exit()
if data[-1][0]=="0":
End+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*Start*End)
exit()
odd=[comp[i] for i in range(n) if i%2==1]
N=len(odd)
N0 = 2**(N-1).bit_length()
data = [None]*(2*N0)
INF = (-1, 0)
# 区間[l, r+1)の値をvに書き換える
# vは(t, value)という値にする (新しい値ほどtは大きくなる)
def update(l, r, v):
L = l + N0; R = r + N0
while L < R:
if R & 1:
R -= 1
data[R-1] = v
if L & 1:
data[L-1] = v
L += 1
L >>= 1; R >>= 1
# a_iの現在の値を取得
def _query(k):
k += N0-1
s = INF
while k >= 0:
if data[k]:
s = max(s, data[k])
k = (k - 1) // 2
return s
# これを呼び出す
def query(k):
return _query(k)[1]
ID=[-1]*n
for i in range(N):
start=0
end=N
while end-start>1:
test=(end+start)//2
if query(test)>=odd[i]:
start=test
else:
end=test
#print(i,start,odd[i])
if start!=0:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
if query(0)>=odd[i]:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
ID[2*i+1]=-1
update(0,i+1,(i,odd[i]))
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
for i in range(2,n,2):
id=ID[i-1]
if id==-1:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
#prel[i-1]=1
else:
j=2*id+1
#res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
res=(comp[i-1]*dp[i-2]+pre[j]-comp[i-1]*dp[j-1])%mod
pre[i-1]=res
#prel[i-1]=prel[j]+1
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
#print(pre)
#print(dp)
print((dp[-1]*Start*End)%mod)
``` | output | 1 | 75,198 | 0 | 150,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,199 | 0 | 150,398 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
import sys
readline = sys.stdin.readline
MOD = 10**9+7;S = readline().strip().split('1')
if len(S) == 1:print(len(S[0]))
else:
S = [len(s)+1 for s in S];ans = S[0]*S[-1];S = S[1:-1];dp = [0]*(max(S)+2);dp[0] = 1
for ai in S:
res = 0;rz = 0
for i in range(ai+1):res = (res + dp[i])%MOD;rz = (rz + (ai-i)*dp[i])%MOD;dp[i] = 0
dp[0] = rz;dp[ai] = res
aaa = 0
for d in dp:aaa = (aaa+d)%MOD
print(aaa*ans%MOD)
``` | output | 1 | 75,199 | 0 | 150,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation. | instruction | 0 | 75,200 | 0 | 150,400 |
Tags: combinatorics, data structures, dp
Correct Solution:
```
import sys
readline = sys.stdin.readline
MOD = 10**9+7
S = readline().strip().split('1')
if len(S) == 1:
print(len(S[0]))
else:
S = [len(s)+1 for s in S]
ans = S[0]*S[-1]
S = S[1:-1]
dp = [0]*(max(S)+2)
dp[0] = 1
for ai in S:
res = 0
rz = 0
for i in range(ai+1):
res = (res + dp[i])%MOD
rz = (rz + (ai-i)*dp[i])%MOD
dp[i] = 0
dp[0] = rz
dp[ai] = res
aaa = 0
for d in dp:
aaa = (aaa+d)%MOD
print(aaa*ans%MOD)
``` | output | 1 | 75,200 | 0 | 150,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation.
Submitted Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
start=1
end=1
if data[0][0]=="0":
start+=data[0][1]
data=data[1:]
if not data:
print(start-1)
exit()
if data[-1][0]=="0":
end+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*start*end)
exit()
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
prel=[0]*n
premono=[0]*n
pre[1]=comp[0]*comp[1]
prel[1]=1
premono[1]=comp[1]
odd=[(comp[i],i) for i in range(n) if i%2==1]
odd.sort(reverse=True)
ID=[0]*n
for i in range(len(odd)):
val,id=odd[i]
ID[id]=i
for i in range(2,n,2):
#for j in range(i-1,0,-2):
#if comp[j]>now:
#res+=dp[j-1]*(comp[j]-now)
#now=comp[j]
id=ID[i-1]
if id==0:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
prel[i-1]=1
premono[i-1]=dp[i-2]
else:
j=odd[id-1][1]
res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
pre[i-1]=res
prel[i-1]=prel[j]+1
premono[i-1]=(premono[j]+dp[i-2])%mod
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
print((dp[-1]*start*end)%mod)
``` | instruction | 0 | 75,201 | 0 | 150,402 |
No | output | 1 | 75,201 | 0 | 150,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation.
Submitted Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
Start=1
End=1
if data[0][0]=="0":
Start+=data[0][1]
data=data[1:]
if not data:
print(Start-1)
exit()
if data[-1][0]=="0":
End+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*Start*End)
exit()
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
prel=[0]*n
premono=[0]*n
pre[1]=comp[0]*comp[1]
prel[1]=1
premono[1]=comp[1]
odd=[comp[i] for i in range(n) if i%2==1]
N=len(odd)
N0 = 2**(N-1).bit_length()
data = [None]*(2*N0)
INF = (-1, 0)
# 区間[l, r+1)の値をvに書き換える
# vは(t, value)という値にする (新しい値ほどtは大きくなる)
def update(l, r, v):
L = l + N0; R = r + N0
while L < R:
if R & 1:
R -= 1
data[R-1] = v
if L & 1:
data[L-1] = v
L += 1
L >>= 1; R >>= 1
# a_iの現在の値を取得
def _query(k):
k += N0-1
s = INF
while k >= 0:
if data[k]:
s = max(s, data[k])
k = (k - 1) // 2
return s
# これを呼び出す
def query(k):
return _query(k)[1]
ID=[-1]*n
for i in range(N):
start=0
end=N
while end-start>1:
test=(end+start)//2
if query(test)>=odd[i]:
start=test
else:
end=test
#print(i,start,odd[i])
if start!=0:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
if query(0)>=odd[i]:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
ID[2*i+1]=-1
update(0,i+1,(i,odd[i]))
#print(comp)
#print(odd)
#print(ID)
for i in range(2,n,2):
#for j in range(i-1,0,-2):
#if comp[j]>now:
#res+=dp[j-1]*(comp[j]-now)
#now=comp[j]
id=ID[i-1]
if id==-1:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
prel[i-1]=1
premono[i-1]=dp[i-2]
else:
j=2*id+1
res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
pre[i-1]=res
prel[i-1]=prel[j]+1
premono[i-1]=(premono[j]+dp[i-2])%mod
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
#print(dp)
print((dp[-1]*Start*End)%mod)
``` | instruction | 0 | 75,202 | 0 | 150,404 |
No | output | 1 | 75,202 | 0 | 150,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation.
Submitted Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
Start=1
End=1
if data[0][0]=="0":
Start+=data[0][1]
data=data[1:]
if not data:
print(Start-1)
exit()
if data[-1][0]=="0":
End+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*Start*End)
exit()
odd=[comp[i] for i in range(n) if i%2==1]
N=len(odd)
N0 = 2**(N-1).bit_length()
data = [None]*(2*N0)
INF = (-1, 0)
# 区間[l, r+1)の値をvに書き換える
# vは(t, value)という値にする (新しい値ほどtは大きくなる)
def update(l, r, v):
L = l + N0; R = r + N0
while L < R:
if R & 1:
R -= 1
data[R-1] = v
if L & 1:
data[L-1] = v
L += 1
L >>= 1; R >>= 1
# a_iの現在の値を取得
def _query(k):
k += N0-1
s = INF
while k >= 0:
if data[k]:
s = max(s, data[k])
k = (k - 1) // 2
return s
# これを呼び出す
def query(k):
return _query(k)[1]
ID=[-1]*n
for i in range(N):
start=0
end=N
while end-start>1:
test=(end+start)//2
if query(test)>=odd[i]:
start=test
else:
end=test
#print(i,start,odd[i])
if start!=0:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
if query(0)>=odd[i]:
ID[2*i+1]=start
update(start+1,i+1,(i,odd[i]))
else:
ID[2*i+1]=-1
update(0,i+1,(i,odd[i]))
#print(comp)
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
prel=[0]*n
premono=[0]*n
pre[1]=comp[0]*comp[1]
prel[1]=1
premono[1]=comp[0]
for i in range(2,n,2):
id=ID[i-1]
if id==-1:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
prel[i-1]=1
premono[i-1]=dp[i-2]
else:
j=2*id+1
#res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
res=(comp[i-1]*dp[i-2]+pre[j]-comp[i-1]*premono[j])%mod
pre[i-1]=res
prel[i-1]=prel[j]+1
premono[i-1]=(premono[j]+dp[i-2])%mod
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
#print(dp)
print((dp[-1]*Start*End)%mod)
``` | instruction | 0 | 75,203 | 0 | 150,406 |
No | output | 1 | 75,203 | 0 | 150,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala has a binary string s of length n. Koa can perform no more than n-1 (possibly zero) operations of the following form:
In one operation Koa selects positions i and i+1 for some i with 1 ≤ i < |s| and sets s_i to max(s_i, s_{i+1}). Then Koa deletes position i+1 from s (after the removal, the remaining parts are concatenated).
Note that after every operation the length of s decreases by 1.
How many different binary strings can Koa obtain by doing no more than n-1 (possibly zero) operations modulo 10^9+7 (1000000007)?
Input
The only line of input contains binary string s (1 ≤ |s| ≤ 10^6). For all i (1 ≤ i ≤ |s|) s_i = 0 or s_i = 1.
Output
On a single line print the answer to the problem modulo 10^9+7 (1000000007).
Examples
Input
000
Output
3
Input
0101
Output
6
Input
0001111
Output
16
Input
00101100011100
Output
477
Note
In the first sample Koa can obtain binary strings: 0, 00 and 000.
In the second sample Koa can obtain binary strings: 1, 01, 11, 011, 101 and 0101. For example:
* to obtain 01 from 0101 Koa can operate as follows: 0101 → 0(10)1 → 011 → 0(11) → 01.
* to obtain 11 from 0101 Koa can operate as follows: 0101 → (01)01 → 101 → 1(01) → 11.
Parentheses denote the two positions Koa selected in each operation.
Submitted Solution:
```
from itertools import groupby
s=input()
n=len(s)
s=[s[i] for i in range(n)]
mod=10**9+7
data=groupby(s)
data=[(key,len(list(group))) for key,group in data]
start=1
end=1
if data[0][0]=="0":
start+=data[0][1]
data=data[1:]
if not data:
print(start-1)
exit()
if data[-1][0]=="0":
end+=data[-1][1]
data.pop()
comp=[0]*len(data)
for i in range(len(data)):
comp[i]=data[i][1]
n=len(comp)
if n==1:
print(comp[0]*start*end)
exit()
dp=[0]*n
dp[0]=comp[0]
pre=[0]*n
prel=[0]*n
premono=[0]*n
pre[1]=comp[0]*comp[1]
prel[1]=1
premono[1]=comp[1]
odd=[(comp[i],i) for i in range(n) if i%2==1]
odd.sort(reverse=True)
ID=[0]*n
for i in range(len(odd)):
val,id=odd[i]
ID[id]=i
for i in range(2,n,2):
res=0
now=0
#for j in range(i-1,0,-2):
#if comp[j]>now:
#res+=dp[j-1]*(comp[j]-now)
#now=comp[j]
id=ID[i-1]
if id==0:
res=comp[i-1]*dp[i-2]
pre[i-1]=res
prel[i-1]=1
premono[i-1]=dp[i-2]
else:
j=odd[id-1][0]
res=(comp[i-1]*dp[i-2]+(comp[j]-comp[i-1])*premono[j])%mod
pre[i-1]=res
prel[i-1]=prel[j]+1
premono[i-1]=(premono[j]+dp[i-2])%mod
dp[i]=(comp[i]*res+dp[i-2]+comp[i])%mod
print((dp[-1]*start*end)%mod)
``` | instruction | 0 | 75,204 | 0 | 150,408 |
No | output | 1 | 75,204 | 0 | 150,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,563 | 0 | 151,126 |
Tags: brute force, dp
Correct Solution:
```
strs=input()
array=[]
char=[]
count=1
char.append(strs[0])
i=1
while(i<len(strs)):
if(strs[i]==strs[i-1]):
count+=1
else:
array.append(count)
char.append(strs[i])
count=1
i+=1
array.append(count)
bback=[]
aback=[]
bfront=[]
afront=[]
i=0
while(i<len(array)):
if(i==0):
if(char[i]=='b'):
aback.append(0)
bback.append(array[0])
else:
bback.append(0)
aback.append(array[0])
else:
if(char[i]=='b'):
aback.append(aback[i-1])
bback.append(bback[i-1]+array[i])
else:
aback.append(aback[i-1]+array[i])
bback.append(bback[i-1])
i+=1
i=0
while(i<len(array)):
afront.append(aback[-1]-aback[i])
bfront.append(bback[-1]-bback[i])
i+=1
if(len(array)==1):
print(array[0])
else:
maxm=-1
i=0
while(i<len(array)):
if(char[i]=='b'):
j=i
while(j<len(array)):
if(char[j]=='b'):
if(j==i):
ans=aback[i]+array[i]+afront[i]
if(ans>maxm):
maxm=ans
else:
ans=aback[i]+array[i]+(bback[j]-bback[i])+afront[j]
if(ans>maxm):
maxm=ans
j+=1
i+=1
print(maxm)
``` | output | 1 | 75,563 | 0 | 151,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,564 | 0 | 151,128 |
Tags: brute force, dp
Correct Solution:
```
s = input()
a, b = [0], [0]
a_count, b_count = 0, 0
for x in s:
if x == 'a':
a_count += 1
else:
b_count += 1
a.append(a_count)
b.append(b_count)
best = b_count
for i in range(len(a)):
for j in range(i, len(a)):
best = max(best, a[i] + (a_count-a[j]) + (b[j]-b[i]))
print(best)
``` | output | 1 | 75,564 | 0 | 151,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,565 | 0 | 151,130 |
Tags: brute force, dp
Correct Solution:
```
s = list(input())
n = len(s)
lis=[[0,0] for i in range(n+2)]
for i in range(n):
if s[i]=='a':
lis[i+1][0]=lis[i][0]+1
lis[i+1][1]=lis[i][1]
else:
lis[i+1][0]=lis[i][0]
lis[i+1][1]=lis[i][1]+1
#print(lis)
ans=0
for i in range(1,n+1):
for j in range(i,n+2):
tmp=0
tmp+=(lis[j-1][1]-lis[i-1][1])
tmp+=(lis[i-1][0])
tmp+=(lis[n][0]-lis[j-1][0])
# print(tmp,i,j)
ans=max(ans,tmp)
print(ans)
``` | output | 1 | 75,565 | 0 | 151,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,566 | 0 | 151,132 |
Tags: brute force, dp
Correct Solution:
```
s = input().strip()
n = len(s)
dp = [[0, 0, 0] for _ in range(n)]
for i in range(n):
if s[i] == 'a':
dp[i][0] = 1
for j in range(i):
dp[i][0] = max(dp[i][0], dp[j][0] + 1)
dp[i][1] = dp[i][0]
dp[i][2] = 1
for j in range(i):
dp[i][2] = max(dp[i][2], dp[j][1] + 1, dp[j][2] + 1)
else:
dp[i][0] = 0
dp[i][1] = 1
for j in range(i):
dp[i][1] = max(dp[i][1], dp[j][1] + 1)
dp[i][2] = 1
for j in range(i):
dp[i][2] = max(dp[i][2], dp[j][1] + 1)
ans = 0
for i in range(n):
ans = max(ans, dp[i][2])
print(ans)
``` | output | 1 | 75,566 | 0 | 151,133 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,567 | 0 | 151,134 |
Tags: brute force, dp
Correct Solution:
```
s = input()
n = len(s)
dp = []
dp.append((0, 0, 0))
for i in range(n):
if s[i] == 'a':
dp.append((dp[-1][0] + 1, dp[-1][1], max(dp[-1][1:]) + 1))
elif s[i] == 'b':
dp.append((dp[-1][0], max(dp[-1][1], dp[-1][0]) + 1, dp[-1][-1]))
print(max(dp[-1]))
``` | output | 1 | 75,567 | 0 | 151,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,568 | 0 | 151,136 |
Tags: brute force, dp
Correct Solution:
```
s=input()
n=len(s)
prefa=[0]*(n)
prefb=[0]*(n)
prefa[0]=(s[0]=='a')
prefb[0]=(s[0]=='b')
c=0
for i in range(1,n):
prefa[i]=prefa[i-1]+(s[i]=='a')
prefb[i]=prefb[i-1]+(s[i]=='b')
ans1,ans=0,0
prefa=[0]+prefa
prefb=[0]+prefb
for i in range(n+1):
for j in range(i,n+1):
ans1=(prefa[n]-prefa[j])+(prefb[j]-prefb[i])+(prefa[i])
if ans1>ans:ans=ans1
print(ans)
``` | output | 1 | 75,568 | 0 | 151,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,569 | 0 | 151,138 |
Tags: brute force, dp
Correct Solution:
```
max1=0
string=input()
first=0
second=0
third=0
i=0
length=len(string)
while(i<length):
if string[i]=='a':
if second>third:
third=second+1
else:
third+=1
first+=1
if string[i]=='b':
if first>second:
second=first+1
else:
second+=1
i+=1
print(max(third,first,second))
``` | output | 1 | 75,569 | 0 | 151,139 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful. | instruction | 0 | 75,570 | 0 | 151,140 |
Tags: brute force, dp
Correct Solution:
```
s = input()
a = [0, 0, 0]
for i in range(len(s)):
if s[i] == "a":
a[2] = max(a) + 1
a[0] += 1
else:
a[1] = max(a[0], a[1]) + 1
print(max(a))
``` | output | 1 | 75,570 | 0 | 151,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
s = input()
a_count = s.count('a')
b_count = len(s) - a_count
a_counts = [0] * len(s)
b_counts = [0] * len(s)
for i in range(len(s)):
if s[i] == 'a':
a_counts[i] = 1
else:
b_counts[i] = 1
if i > 0:
a_counts[i] += a_counts[i - 1]
b_counts[i] += b_counts[i - 1]
best = 0
count = 0
for i in range(-1, len(s)):
for j in range(i + 1, len(s) + 1):
count = 0
if i >= 0:
count += a_counts[i]
count += (b_count if j == len(s) else b_counts[j]) - (0 if i < 0 else b_counts[i])
count += 0 if j == len(s) else (a_count - a_counts[j])
if best < count:
best = count
print(best)
``` | instruction | 0 | 75,571 | 0 | 151,142 |
Yes | output | 1 | 75,571 | 0 | 151,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
s = ' '+input()
dp = [[0,0,0]for i in range(len(s)+5)]
i = 0
while i<len(s)-1:
i += 1
if s[i]=='a':
dp[i][0] = dp[i-1][0] + 1
dp[i][1] = dp[i-1][1]
dp[i][2] = max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+1
if s[i]=='b':
dp[i][0] = dp[i - 1][0]
dp[i][1] = max(dp[i - 1][0],dp[i-1][1]) + 1
dp[i][2] = max(dp[i - 1][0], dp[i - 1][2])
print(max(dp[i][0],dp[i][1],dp[i][2]))
``` | instruction | 0 | 75,572 | 0 | 151,144 |
Yes | output | 1 | 75,572 | 0 | 151,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
a = input()
dp1 = 0
dp2 = 0
cnt = 0
for i in range(len(a)):
if a[i] == 'a':
cnt += 1
dp2 = max(dp1 + 1, dp2 + 1)
else:
dp1 = max(dp1 + 1, cnt + 1)
print(max(dp1,dp2))
``` | instruction | 0 | 75,573 | 0 | 151,146 |
Yes | output | 1 | 75,573 | 0 | 151,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
s = input()
n = len(s)
dp = [[10 ** 9 for j in range(4)] for i in range(n + 1)]
dp[0][0] = 0
for i in range(n + 1):
for j in range(3):
cnt = 0
for k in range(i, n + 1):
dp[k][j + 1] = min(dp[k][j + 1], dp[i][j] + cnt)
if k < n and ((j == 1 and s[k] == 'a') or (j != 1 and s[k] == 'b')):
cnt += 1
print(n - dp[n][3])
``` | instruction | 0 | 75,574 | 0 | 151,148 |
Yes | output | 1 | 75,574 | 0 | 151,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
import random, math
from copy import deepcopy as dc
# To Genrate Random Number for Test-Cases
def randomNumber(s, e):
return random.randint(s, e)
# To Generate Random Array for Test-Cases
def randomArray(s, e, s_size, e_size):
size = random.randint(s_size, e_size)
arr = [randomNumber(s, e) for i in range(size)]
return arr
# To Generate Question Specific Test-Cases
def testcase():
pass
# Brute Force Approach to check Solution
def brute():
pass
# Efficient Approach for problem
def effe():
pass
# Function to call the actual solution
def solution(s):
sa = 0
mb = 0
ea = 0
cur_b = 0
cur_a = 0
n = len(s)
for i in range(n):
if s[i] == 'a' and not mb:
sa += 1
elif s[i] == 'a' and mb:
ea += 1
elif s[i] == 'b' and not ea:
mb += 1
elif s[i] == 'b' and ea:
cur_b += 1
if cur_b > ea:
ea = 0
mb += cur_b
cur_b = 0
# print(sa+mb+ea)
return sa + mb +ea
# Function to take input
def input_test():
s = input()
out = solution(s)
print(out)
# Function to check test my code
def test():
pass
input_test()
# test()
``` | instruction | 0 | 75,575 | 0 | 151,150 |
No | output | 1 | 75,575 | 0 | 151,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
#nikita and string
s = input()
#bflag, baflag, su
dp = [0,0,0]
for i in s:
if i == 'b':
if dp[1]:
dp[2] += dp[1]
dp[1] = 0
dp[0] = 1
elif i == 'a':
if dp[0]:
dp[1] += 1
print(len(s)-dp[2])
``` | instruction | 0 | 75,576 | 0 | 151,152 |
No | output | 1 | 75,576 | 0 | 151,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
from collections import defaultdict
from itertools import groupby
def reverse_enumerate(seq):
n = len(seq)
for i in range(n):
yield n - 1 - i, seq[n - 1 - i]
class Symbol(object):
def __init__(self, symbol, iter=None):
self.symbol = symbol
self.size = sum(1 for _ in iter) if iter else 0
def __hash__(self):
return hash(self.symbol)
def solve(inp):
symbols = [Symbol(s, i) for s, i in groupby(inp)]
longest_end_a = defaultdict(int)
longest_start_a = defaultdict(int)
longest_end_b = defaultdict(int)
n = len(symbols)
for idx, symbol in reverse_enumerate(symbols):
if symbol.symbol == "a":
longest_end_a[idx] = longest_end_a[idx + 1] + symbol.size
longest_end_b[idx] = longest_end_b[idx + 1]
else:
longest_end_a[idx] = longest_end_a[idx + 1]
longest_end_b[idx] = longest_end_b[idx + 1] + symbol.size
for idx, symbol in enumerate(symbols):
if symbol.symbol == "a":
longest_start_a[idx] = longest_start_a[idx - 1] + symbol.size
else:
longest_start_a[idx] = longest_start_a[idx - 1]
return max(
longest_start_a[idx] + (longest_end_b[idx] - longest_end_b[second_idx]) + longest_end_a[second_idx]
for idx in range(n)
for second_idx in range(idx + 1, n)
)
if __name__ == "__main__":
print(solve(input()))
``` | instruction | 0 | 75,577 | 0 | 151,154 |
No | output | 1 | 75,577 | 0 | 151,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Nikita found the string containing letters "a" and "b" only.
Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".
Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.
Examples
Input
abba
Output
4
Input
bab
Output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
Submitted Solution:
```
s = input()
if len(s) > 508 and s[:508] == "baabbbabbbbbbbbbbbbbbbabbbbbbaabbbabbbbabbaabbbbbbabbbbbaabbbaaabbbbbbbbabbabbbabbaababbbabbbbabbabababbbbbbbbbbabbbbbbbbabbbbabbbbbbabbbbabbabbbbbabbbbbbbabbbbaabbbbbabbbbabbbabbbbbabbbbbbbaaaaabbbbbbbbbbbbbbbbbbbbbabbbbbbbababbbbbaabbabbabbbbbbbbaabbbbbabbbbbbbbbbbbbbabbbababbbabbbabbabaabbbbbbbbbbbbbbbbbbbbaabbbbbabbbbbbbbbbabbbbbbbbbbbbbabbbbbbabbbabbbbbbbabbbbbbbbbbbbaabbbbbbbbbbbbbababbbaabbbbbbabbbbbbabbbbaabbbaabbabbbbaababbbbbbabbbabbbbbbbbbbbbbbbbbbbbabbbbbbabbbbbabbbbbbbbbbabbbbbaabbbbbbbabbb":
print(4001)
dp = [(1, s[0], False)]
for i in range(1, len(s)):
# print(dp)
if s[i] == "a":
mx = (-1, "", False)
for j in range(i-1, -1, -1):
if dp[j][0] > mx[0]:
mx = dp[j]
dp.append((mx[0]+1, mx[1]+"a", mx[2] or mx[1][-1] == "b"))
else:
mx = (-1, "", False)
for j in range(i-1, -1, -1):
# print("dp:", dp[j])
if dp[j][0] > mx[0]:
if dp[j][1][-1] == "a" and dp[j][2]:
continue
mx = dp[j]
dp.append((mx[0]+1, mx[1]+"b", mx[2]))
# print(dp)
print(dp[-1][0])
``` | instruction | 0 | 75,578 | 0 | 151,156 |
No | output | 1 | 75,578 | 0 | 151,157 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,723 | 0 | 151,446 |
"Correct Solution:
```
s=input()
K=int(input())
mojiretsu=set()
for i in range(0,len(s)):
for k in range(1,6):
mojiretsu.add(s[i:i+k])
ans=sorted(mojiretsu)
print(ans[K-1])
``` | output | 1 | 75,723 | 0 | 151,447 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,724 | 0 | 151,448 |
"Correct Solution:
```
s=input()
k=int(input())
subs=set()
for l in range(1,k+1):
for i in range(len(s)-l+1):
subs.add(s[i:i+l])
subs=sorted(subs)
print(subs[k-1])
``` | output | 1 | 75,724 | 0 | 151,449 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,725 | 0 | 151,450 |
"Correct Solution:
```
s = input()
k = int(input())
n = len(s)
t = set()
for i in range(k):
for j in range(n-i):
t.add(s[j:j+i+1])
lis = sorted(list(t))
print(lis[k-1])
``` | output | 1 | 75,725 | 0 | 151,451 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,726 | 0 | 151,452 |
"Correct Solution:
```
s = input()
K = int(input())
arr = set()
for i in range(len(s)):
for j in range(i + 1, min(i + K + 1, len(s) + 1)):
arr.add(s[i:j])
print(sorted(arr)[K - 1])
``` | output | 1 | 75,726 | 0 | 151,453 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,727 | 0 | 151,454 |
"Correct Solution:
```
s=input()
K=int(input())
a=[]
l=len(s)
for i in range(l):
for j in range(1,K+1):
a.append(s[i:(i+j)])
a=list(set(a))
a.sort()
print(a[K-1])
``` | output | 1 | 75,727 | 0 | 151,455 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,728 | 0 | 151,456 |
"Correct Solution:
```
s = input()
K = int(input())
ans = set()
for i in range(len(s)):
for j in range(i+1, K+i+1):
ans.add(s[i:j])
print(sorted(list(ans))[K-1])
``` | output | 1 | 75,728 | 0 | 151,457 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,729 | 0 | 151,458 |
"Correct Solution:
```
s = input()
K = int(input())
d = set()
for l in range(1, K+1):
for i in range(len(s)+1-l):
d |= {s[i:i+l]}
print(sorted(list(d))[K-1])
``` | output | 1 | 75,729 | 0 | 151,459 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z | instruction | 0 | 75,730 | 0 | 151,460 |
"Correct Solution:
```
s = input()
k = int(input())
l = []
for i in range(len(s)):
for j in range(1, k+1):
l.append(s[i:i+j])
print(sorted(set(l))[k-1])
``` | output | 1 | 75,730 | 0 | 151,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
s = input()
K = int(input())
sub = set()
for i in range(len(s) + 1):
for j in range(i + 1,i + K + 1):
sub.add(s[i:j])
ans = list(sorted(sub))
print(ans[K])
``` | instruction | 0 | 75,731 | 0 | 151,462 |
Yes | output | 1 | 75,731 | 0 | 151,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
s = input()
k = int(input())
subs = []
for i in range(len(s)):
for j in range(k):
subs.append(s[i:i+j+1])
subs = sorted(list(set(subs)))
print(subs[k-1])
``` | instruction | 0 | 75,732 | 0 | 151,464 |
Yes | output | 1 | 75,732 | 0 | 151,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
s = input()
K = int(input())
l = set()
for i in range(1, 6):
l |= set([s[n:n+i] for n in range(len(s))])
print(sorted(l)[K-1])
``` | instruction | 0 | 75,733 | 0 | 151,466 |
Yes | output | 1 | 75,733 | 0 | 151,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
s = input()
K = int(input())
S = set()
for i in range(1, K+1):
S |= set([s[n:n+i] for n in range(0, max(len(s)-i+1, 0))])
print(sorted(list(S))[K-1])
``` | instruction | 0 | 75,734 | 0 | 151,468 |
Yes | output | 1 | 75,734 | 0 | 151,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
S=input()
k=int(input())
candidate=set()
for i in range(1, len(S)+1):
for j in range(len(S)-i+1):
candidate.add(S[j:j+i])
candidate=sorted(list(candidate))
print(candidate[k-1])
``` | instruction | 0 | 75,735 | 0 | 151,470 |
No | output | 1 | 75,735 | 0 | 151,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde s=input() s='abcde'
# abcde s=list(input()) s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ) a=int(input()) a=5
# 1 2 | x,y = map(int,input().split())| x=1,y=2
# 1 2 3 4 5 ... n li = input().split() li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n li = list(map(int,input().split())) li=[1,2,3,4,5,...,n]
# FFFTFTTFF li = input().split('T') li=['FFF', 'F', '', 'FF']
# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]
import collections
def inpl(): return list(map(int, input().split()))
#### START
s = input()
K = int(input())
dic = set()
for i in range(len(s)+1):
for j in range(K):
if len(s[j:j+i])>0:
dic.add(s[j:j+i])
ans = sorted(list(dic))
print(ans[K-1])
``` | instruction | 0 | 75,736 | 0 | 151,472 |
No | output | 1 | 75,736 | 0 | 151,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
su = 0
co = 0
for i in range(max(a)):
for j in range(n):
if a[j] > 0 and co == 0:
co += 1
su += 1
if co >= 1:
if a[j] == 0:
co = 0
else:
a[j] -= 1
co = 0
print(su)
``` | instruction | 0 | 75,737 | 0 | 151,474 |
No | output | 1 | 75,737 | 0 | 151,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.
Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.
Constraints
* 1 ≤ |s| ≤ 5000
* s consists of lowercase English letters.
* 1 ≤ K ≤ 5
* s has at least K different substrings.
Input
Input is given from Standard Input in the following format:
s
K
Output
Print the K-th lexicographically smallest substring of K.
Examples
Input
aba
4
Output
b
Input
atcoderandatcodeer
5
Output
andat
Input
z
1
Output
z
Submitted Solution:
```
s = input()
k = int(input())
n = len(s)
a = set()
for i in range(n):
for j in range(n-i):
a.add(s[j:j+i+1])
a = sorted(list(a))
print(a[k-1])
``` | instruction | 0 | 75,738 | 0 | 151,476 |
No | output | 1 | 75,738 | 0 | 151,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Boboniu defines BN-string as a string s of characters 'B' and 'N'.
You can perform the following operations on the BN-string s:
* Remove a character of s.
* Remove a substring "BN" or "NB" of s.
* Add a character 'B' or 'N' to the end of s.
* Add a string "BN" or "NB" to the end of s.
Note that a string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Boboniu thinks that BN-strings s and t are similar if and only if:
* |s|=|t|.
* There exists a permutation p_1, p_2, …, p_{|s|} such that for all i (1≤ i≤ |s|), s_{p_i}=t_i.
Boboniu also defines dist(s,t), the distance between s and t, as the minimum number of operations that makes s similar to t.
Now Boboniu gives you n non-empty BN-strings s_1,s_2,…, s_n and asks you to find a non-empty BN-string t such that the maximum distance to string s is minimized, i.e. you need to minimize max_{i=1}^n dist(s_i,t).
Input
The first line contains a single integer n (1≤ n≤ 3⋅ 10^5).
Each of the next n lines contains a string s_i (1≤ |s_i| ≤ 5⋅ 10^5). It is guaranteed that s_i only contains 'B' and 'N'. The sum of |s_i| does not exceed 5⋅ 10^5.
Output
In the first line, print the minimum max_{i=1}^n dist(s_i,t).
In the second line, print the suitable t.
If there are several possible t's, you can print any.
Examples
Input
3
B
N
BN
Output
1
BN
Input
10
N
BBBBBB
BNNNBBNBB
NNNNBNBNNBNNNBBN
NBNBN
NNNNNN
BNBNBNBBBBNNNNBBBBNNBBNBNBBNBBBBBBBB
NNNNBN
NBBBBBBBB
NNNNNN
Output
12
BBBBBBBBBBBBNNNNNNNNNNNN
Input
8
NNN
NNN
BBNNBBBN
NNNBNN
B
NNN
NNNNBNN
NNNNNNNNNNNNNNNBNNNNNNNBNB
Output
12
BBBBNNNNNNNNNNNN
Input
3
BNNNBNNNNBNBBNNNBBNNNNBBBBNNBBBBBBNBBBBBNBBBNNBBBNBNBBBN
BBBNBBBBNNNNNBBNBBBNNNBB
BBBBBBBBBBBBBBNBBBBBNBBBBBNBBBBNB
Output
12
BBBBBBBBBBBBBBBBBBBBBBBBBBNNNNNNNNNNNN
Note
In the first example dist(B,BN)=dist(N,BN)=1, dist(BN,BN)=0. So the maximum distance is 1.
Submitted Solution:
```
n=int(input())
temp=[]
x,y=0,0
for i in range(n):
s=input()
a,b=s.count('B'),s.count('N')
temp.append((a,b))
x,y=max(x,a),max(y,b)
def dist(num,x,y):
x_c=y_c=0
dis=0
for i,j in num:
if i>=x and j>=y:
val=min(i-x,j-y)
val+=i+j-x-y-2*val
elif i<=x and j<=y:
val=min(x-i,y-j)
val+=x+y-i-j-2*val
else:
val=abs(x-i)+abs(y-j)
x_c+=i-x
y_c+=j-y
dis=max(dis,val)
return x_c,y_c,dis
seen=set([(x,y)])
nx,ny,out=dist(temp,x,y)
ans=(x,y)
x//=2
y//=2
while (x,y)not in seen:
seen.add((x,y))
nx,ny,val=dist(temp,x,y)
if val<out:
out=val
ans=(x,y)
x,y=int(x+nx//n),int(y+ny//n)
print(out)
x,y=ans
print('B'*x+'N'*y)
``` | instruction | 0 | 76,054 | 0 | 152,108 |
No | output | 1 | 76,054 | 0 | 152,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Boboniu defines BN-string as a string s of characters 'B' and 'N'.
You can perform the following operations on the BN-string s:
* Remove a character of s.
* Remove a substring "BN" or "NB" of s.
* Add a character 'B' or 'N' to the end of s.
* Add a string "BN" or "NB" to the end of s.
Note that a string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Boboniu thinks that BN-strings s and t are similar if and only if:
* |s|=|t|.
* There exists a permutation p_1, p_2, …, p_{|s|} such that for all i (1≤ i≤ |s|), s_{p_i}=t_i.
Boboniu also defines dist(s,t), the distance between s and t, as the minimum number of operations that makes s similar to t.
Now Boboniu gives you n non-empty BN-strings s_1,s_2,…, s_n and asks you to find a non-empty BN-string t such that the maximum distance to string s is minimized, i.e. you need to minimize max_{i=1}^n dist(s_i,t).
Input
The first line contains a single integer n (1≤ n≤ 3⋅ 10^5).
Each of the next n lines contains a string s_i (1≤ |s_i| ≤ 5⋅ 10^5). It is guaranteed that s_i only contains 'B' and 'N'. The sum of |s_i| does not exceed 5⋅ 10^5.
Output
In the first line, print the minimum max_{i=1}^n dist(s_i,t).
In the second line, print the suitable t.
If there are several possible t's, you can print any.
Examples
Input
3
B
N
BN
Output
1
BN
Input
10
N
BBBBBB
BNNNBBNBB
NNNNBNBNNBNNNBBN
NBNBN
NNNNNN
BNBNBNBBBBNNNNBBBBNNBBNBNBBNBBBBBBBB
NNNNBN
NBBBBBBBB
NNNNNN
Output
12
BBBBBBBBBBBBNNNNNNNNNNNN
Input
8
NNN
NNN
BBNNBBBN
NNNBNN
B
NNN
NNNNBNN
NNNNNNNNNNNNNNNBNNNNNNNBNB
Output
12
BBBBNNNNNNNNNNNN
Input
3
BNNNBNNNNBNBBNNNBBNNNNBBBBNNBBBBBBNBBBBBNBBBNNBBBNBNBBBN
BBBNBBBBNNNNNBBNBBBNNNBB
BBBBBBBBBBBBBBNBBBBBNBBBBBNBBBBNB
Output
12
BBBBBBBBBBBBBBBBBBBBBBBBBBNNNNNNNNNNNN
Note
In the first example dist(B,BN)=dist(N,BN)=1, dist(BN,BN)=0. So the maximum distance is 1.
Submitted Solution:
```
n=int(input())
temp=[]
x,y=0,0
for i in range(n):
s=input()
a,b=s.count('B'),s.count('N')
temp.append((a,b))
x,y=max(x,a),max(y,b)
def dist(num,x,y):
x_c=y_c=0
dis=0
for i,j in num:
if i>=x and j>=y:
val=min(i-x,j-y)
val+=i+j-x-y-2*val
elif i<=x and j<=y:
val=min(x-i,y-j)
val+=x+y-i-j-2*val
else:
val=abs(x-i)+abs(y-j)
x_c+=i-x
y_c+=j-y
dis=max(dis,val)
return x_c,y_c,dis
seen=set([(x,y)])
nx,ny,out=dist(temp,x//2,y//2)
ans=(x,y)
x,y=int(x+nx//n),int(y+ny//n)
while (x,y)not in seen:
seen.add((x,y))
nx,ny,val=dist(temp,x,y)
if val<out:
out=val
ans=(x,y)
x,y=int(x+nx//n),int(y+ny//n)
print(out)
x,y=ans
print('B'*x+'N'*y)
``` | instruction | 0 | 76,055 | 0 | 152,110 |
No | output | 1 | 76,055 | 0 | 152,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.
String a is a substring of string b if it is possible to choose several consecutive letters in b in such a way that they form a. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof".
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of strings.
The next n lines contain the given strings. The number of letters in each string is from 1 to 100, inclusive. Each string consists of lowercase English letters.
Some strings might be equal.
Output
If it is impossible to reorder n given strings in required order, print "NO" (without quotes).
Otherwise print "YES" (without quotes) and n given strings in required order.
Examples
Input
5
a
aba
abacaba
ba
aba
Output
YES
a
ba
aba
aba
abacaba
Input
5
a
abacaba
ba
aba
abab
Output
NO
Input
3
qwerty
qwerty
qwerty
Output
YES
qwerty
qwerty
qwerty
Note
In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba". | instruction | 0 | 76,441 | 0 | 152,882 |
Tags: sortings, strings
Correct Solution:
```
try:
n=int(input())
a=[]
for i in range(n):
a.append(input())
a=sorted(a, key=len)
if(n==1):
print("YES")
print(a[0])
for i in range(1,n):
if(a[i].count(a[i-1])>=1):
if(i==n-1):
print("YES")
for ele in a:
print(ele)
else:
print("NO")
break
except:
pass
``` | output | 1 | 76,441 | 0 | 152,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.
String a is a substring of string b if it is possible to choose several consecutive letters in b in such a way that they form a. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof".
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of strings.
The next n lines contain the given strings. The number of letters in each string is from 1 to 100, inclusive. Each string consists of lowercase English letters.
Some strings might be equal.
Output
If it is impossible to reorder n given strings in required order, print "NO" (without quotes).
Otherwise print "YES" (without quotes) and n given strings in required order.
Examples
Input
5
a
aba
abacaba
ba
aba
Output
YES
a
ba
aba
aba
abacaba
Input
5
a
abacaba
ba
aba
abab
Output
NO
Input
3
qwerty
qwerty
qwerty
Output
YES
qwerty
qwerty
qwerty
Note
In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba". | instruction | 0 | 76,442 | 0 | 152,884 |
Tags: sortings, strings
Correct Solution:
```
def f(s):
return len(s)
n=int(input())
a=[]
for i in range(n):
a.append(input())
a.sort(key=f)
ans="YES"
for i in range(n-1):
if not(a[i] in a[i+1]):
ans="NO"
break
print(ans)
if ans=="YES":
for s in a:
print(s)
``` | output | 1 | 76,442 | 0 | 152,885 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.