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.
A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters '?', the condition s_i β s_{i+1} should be satisfied for all 1 β€ i β€ |s| - 1, where |s| is the length of the string s.
Input
The first line contains positive integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters 'a', 'b', 'c' and '?'.
It is guaranteed that in each test case a string s has at least one character '?'. The sum of lengths of strings s in all test cases does not exceed 10^5.
Output
For each test case given in the input print the answer in the following format:
* If it is impossible to create a beautiful string, print "-1" (without quotes);
* Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are "ababcb", "abcacb", "abcbcb", "acabcb" and "acbacb". The two answers "abcbab" and "abaabc" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is "acbac".
Submitted Solution:
```
import random
t = int(input())
for _ in range(t):
txt = input()
for i in range(len(txt)-1):
chars = ['a', 'b', 'c']
if txt[i] != '?' and txt[i] == txt[i+1]:
print(-1)
break
if txt[i] == '?':
if i>0 and txt[i-1] != '?':
chars.remove(txt[i-1])
if i<(len(txt)-1) and txt[i+1] != '?' and txt[i+1] in chars:
chars.remove(txt[i+1])
k = random.randint(0, len(chars)-1)
txt = txt[:i]+chars[k]+txt[i+1:]
# txt[i] = chars[k]
else:
print(txt)
```
|
instruction
| 0
| 103,039
| 0
| 206,078
|
No
|
output
| 1
| 103,039
| 0
| 206,079
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters '?', the condition s_i β s_{i+1} should be satisfied for all 1 β€ i β€ |s| - 1, where |s| is the length of the string s.
Input
The first line contains positive integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters 'a', 'b', 'c' and '?'.
It is guaranteed that in each test case a string s has at least one character '?'. The sum of lengths of strings s in all test cases does not exceed 10^5.
Output
For each test case given in the input print the answer in the following format:
* If it is impossible to create a beautiful string, print "-1" (without quotes);
* Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are "ababcb", "abcacb", "abcbcb", "acabcb" and "acbacb". The two answers "abcbab" and "abaabc" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is "acbac".
Submitted Solution:
```
def getChar(x, y):
opts = ['a', 'b', 'c']
if x in opts: opts.remove(x)
if y in opts: opts.remove(y)
return opts[0]
def findBS(s):
length = len(s)
opts = ['a', 'b', 'c']
if length == 1:
if '?' in s: print(-1)
else: print(s)
return
for i in range(len(opts)):
if s[0] == '?' and s[1] != opts[i]:
s[0] = opts[i]
if s[length-1] == '?' and s[length-2] != opts[i]:
s[length-1] = opts[i]
for i in range(1, length-1):
if s[i] == '?': s[i] = getChar(s[i-1], s[i+1])
if s[i] == s[i-1] or s[i] == s[i+1]:
print('-1')
return
if '?' in opts:
print(-1)
print(''.join(s))
t = int(input())
for _ in range(t):
s = list(input().rstrip())
findBS(s)
```
|
instruction
| 0
| 103,040
| 0
| 206,080
|
No
|
output
| 1
| 103,040
| 0
| 206,081
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters '?', the condition s_i β s_{i+1} should be satisfied for all 1 β€ i β€ |s| - 1, where |s| is the length of the string s.
Input
The first line contains positive integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters 'a', 'b', 'c' and '?'.
It is guaranteed that in each test case a string s has at least one character '?'. The sum of lengths of strings s in all test cases does not exceed 10^5.
Output
For each test case given in the input print the answer in the following format:
* If it is impossible to create a beautiful string, print "-1" (without quotes);
* Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are "ababcb", "abcacb", "abcbcb", "acabcb" and "acbacb". The two answers "abcbab" and "abaabc" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is "acbac".
Submitted Solution:
```
def solve(p):
if not p:
return []
if len(p)==1:
if p[0] == '?':
return ['a']
else:
return p
s = set()
s.add('a')
s.add('b')
s.add('c')
for i in range(1,len(p)-1):
if p[i] == '?':
rem = list(s.difference(set([p[i-1], p[i+1], '?'])))
if not rem:
return -1
else:
p[i] = rem[0]
if p[0] == '?':
p[0] = list(s.difference(set([p[1], '?'])))[0]
if p[-1] == '?':
p[-1] = list(s.difference(set([p[-2], '?'])))[0]
for i in range(1, len(p)):
if p[i]==p[i-1]:
return -1
return p
problems = []
for i in range(int(input())):
problems += [list(input())]
for p in problems:
print(p)
p = solve(p)
if p!= -1:
p = ''.join(p)
print(p)
```
|
instruction
| 0
| 103,041
| 0
| 206,082
|
No
|
output
| 1
| 103,041
| 0
| 206,083
|
Provide a correct Python 3 solution for this coding contest problem.
Problem
Jennifer and Marian presented Carla with the string S.
However, Carla is not happy to receive the string S.
I wanted the string T.
The three decided to work together to change the string S to the string T.
Jennifer first sorts the letters in any order.
Marian then exchanges the lowercase letters of the two alphabets any number of times.
This operation exchanges all the same characters in the string, for example:
* aab-> Swap a and b-> bba
* aab-> Swap a and c-> ccb
Finally, Carla replaces one character with another and repeats until T.
Jennifer and Marian decided to try to reduce the number of Carla replacements.
Find the minimum number of replacements that Carla makes.
Constraints
* 1 β€ n β€ 105
* S and T contain only'a'~'z'
* | S | = | T | = n
Input
n
S
T
The length n of the string is given on the first line.
The character string S is given on the second line, and the character string T is given on the third line.
Output
Print the minimum number of Carla replacements on one line.
Examples
Input
3
abc
xyz
Output
0
Input
5
aaabb
xyxyz
Output
1
|
instruction
| 0
| 103,618
| 0
| 207,236
|
"Correct Solution:
```
# AOJ 1568: String Conversion
# Python3 2018.7.13 bal4u
cha, chzn = ord('a'), ord('z')+1
S, T = [0]*128, [0]*128
input()
a = input()
for x in a: S[ord(x)] += 1
S = sorted(S[cha:chzn], reverse=True)
a = input()
for x in a: T[ord(x)] += 1
T = sorted(T[cha:chzn], reverse=True)
ans = 0
for i in range(26): ans += abs(S[i]-T[i])
print(ans >> 1)
```
|
output
| 1
| 103,618
| 0
| 207,237
|
Provide a correct Python 3 solution for this coding contest problem.
Problem
Jennifer and Marian presented Carla with the string S.
However, Carla is not happy to receive the string S.
I wanted the string T.
The three decided to work together to change the string S to the string T.
Jennifer first sorts the letters in any order.
Marian then exchanges the lowercase letters of the two alphabets any number of times.
This operation exchanges all the same characters in the string, for example:
* aab-> Swap a and b-> bba
* aab-> Swap a and c-> ccb
Finally, Carla replaces one character with another and repeats until T.
Jennifer and Marian decided to try to reduce the number of Carla replacements.
Find the minimum number of replacements that Carla makes.
Constraints
* 1 β€ n β€ 105
* S and T contain only'a'~'z'
* | S | = | T | = n
Input
n
S
T
The length n of the string is given on the first line.
The character string S is given on the second line, and the character string T is given on the third line.
Output
Print the minimum number of Carla replacements on one line.
Examples
Input
3
abc
xyz
Output
0
Input
5
aaabb
xyxyz
Output
1
|
instruction
| 0
| 103,619
| 0
| 207,238
|
"Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import defaultdict
N = int(input())
s1 = input()
s2 = input()
def make_appears(s):
chars = list(map(ord, s))
hist = defaultdict(int)
for ch in chars:
hist[ch] += 1
appears = list(hist.values())
appears.sort()
appears.reverse()
return appears
appears1 = make_appears(s1)
appears2 = make_appears(s2)
ans = 0
for i, j in zip(appears1, appears2):
ans += abs(i - j)
shorter = min(len(appears1), len(appears2))
ans += sum(appears1[shorter:]) + sum(appears2[shorter:])
print(ans//2)
```
|
output
| 1
| 103,619
| 0
| 207,239
|
Provide a correct Python 3 solution for this coding contest problem.
Problem
Jennifer and Marian presented Carla with the string S.
However, Carla is not happy to receive the string S.
I wanted the string T.
The three decided to work together to change the string S to the string T.
Jennifer first sorts the letters in any order.
Marian then exchanges the lowercase letters of the two alphabets any number of times.
This operation exchanges all the same characters in the string, for example:
* aab-> Swap a and b-> bba
* aab-> Swap a and c-> ccb
Finally, Carla replaces one character with another and repeats until T.
Jennifer and Marian decided to try to reduce the number of Carla replacements.
Find the minimum number of replacements that Carla makes.
Constraints
* 1 β€ n β€ 105
* S and T contain only'a'~'z'
* | S | = | T | = n
Input
n
S
T
The length n of the string is given on the first line.
The character string S is given on the second line, and the character string T is given on the third line.
Output
Print the minimum number of Carla replacements on one line.
Examples
Input
3
abc
xyz
Output
0
Input
5
aaabb
xyxyz
Output
1
|
instruction
| 0
| 103,620
| 0
| 207,240
|
"Correct Solution:
```
from collections import Counter
n = int(input())
counter1 = Counter(input())
counter2 = Counter(input())
values1 = [0] * (26 - len(counter1)) + sorted(counter1.values())
values2 = [0] * (26 - len(counter2)) + sorted(counter2.values())
print(sum([abs(i - j) for i, j in zip(values1, values2)]) // 2)
```
|
output
| 1
| 103,620
| 0
| 207,241
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Problem
Jennifer and Marian presented Carla with the string S.
However, Carla is not happy to receive the string S.
I wanted the string T.
The three decided to work together to change the string S to the string T.
Jennifer first sorts the letters in any order.
Marian then exchanges the lowercase letters of the two alphabets any number of times.
This operation exchanges all the same characters in the string, for example:
* aab-> Swap a and b-> bba
* aab-> Swap a and c-> ccb
Finally, Carla replaces one character with another and repeats until T.
Jennifer and Marian decided to try to reduce the number of Carla replacements.
Find the minimum number of replacements that Carla makes.
Constraints
* 1 β€ n β€ 105
* S and T contain only'a'~'z'
* | S | = | T | = n
Input
n
S
T
The length n of the string is given on the first line.
The character string S is given on the second line, and the character string T is given on the third line.
Output
Print the minimum number of Carla replacements on one line.
Examples
Input
3
abc
xyz
Output
0
Input
5
aaabb
xyxyz
Output
1
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import defaultdict
N = int(input())
s1 = input()
s2 = input()
chars1 = list(map(ord, s1))
chars2 = list(map(ord, s2))
hist1 = defaultdict(int)
hist2 = defaultdict(int)
for ch in chars1:
hist1[ch] += 1
for ch in chars2:
hist2[ch] += 1
appears1 = list(hist1.values())
appears2 = list(hist2.values())
ans = 0
for i, j in zip(appears1, appears2):
ans += abs(i - j)
longer = max(len(appears1), len(appears2))
ans += sum(appears1[longer:]) + sum(appears2[longer:])
print(ans)
```
|
instruction
| 0
| 103,622
| 0
| 207,244
|
No
|
output
| 1
| 103,622
| 0
| 207,245
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integers l and r.
Let's call an integer x modest, if l β€ x β€ r.
Find a string of length n, consisting of digits, which has the largest possible number of substrings, which make a modest integer. Substring having leading zeros are not counted. If there are many answers, find lexicographically smallest one.
If some number occurs multiple times as a substring, then in the counting of the number of modest substrings it is counted multiple times as well.
Input
The first line contains one integer l (1 β€ l β€ 10^{800}).
The second line contains one integer r (l β€ r β€ 10^{800}).
The third line contains one integer n (1 β€ n β€ 2 000).
Output
In the first line, print the maximum possible number of modest substrings.
In the second line, print a string of length n having exactly that number of modest substrings.
If there are multiple such strings, print the lexicographically smallest of them.
Examples
Input
1
10
3
Output
3
101
Input
1
11
3
Output
5
111
Input
12345
12346
6
Output
1
012345
Note
In the first example, string Β«101Β» has modest substrings Β«1Β», Β«10Β», Β«1Β».
In the second example, string Β«111Β» has modest substrings Β«1Β» (3 times) and Β«11Β» (2 times).
Submitted Solution:
```
n = int(input())
x = [int(x) for x in input().split()]
h = x[0]
l = x[0]
a = 0
for i in range(0,n):
if x[i] > h:
h = x[i]
a+=1
if x[i] < l:
l = x[i]
a+=1
print(a)
```
|
instruction
| 0
| 103,731
| 0
| 207,462
|
No
|
output
| 1
| 103,731
| 0
| 207,463
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,075
| 0
| 208,150
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
n,k=map(int, input().split())
s=input()
a=0
b=0
ans=0
x=0
for i in range(n):
if s[i]=='a':
a+=1
else:
b+=1
if(min(a,b)<=k):
ans=max(ans,a+b)
else:
if s[x]=='a':
a-=1
else:
b-=1
x+=1
print(ans)
```
|
output
| 1
| 104,075
| 0
| 208,151
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,076
| 0
| 208,152
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
def executa(n, k, letra, s):
r = 0
tk = k
p = 0
for i in range(n):
while r < n and tk >= 0:
if a[r] == letra:
if tk == 0:
break
tk -= 1
r += 1
p += 1
s = max(s, p)
p -= 1
if a[i] == letra:
tk += 1
if r == n:
break
return s
if __name__ == '__main__':
entrada = input()
entrada_str = list(entrada.split(" "))
entrada_int = list(map(int, entrada_str))
n = entrada_int[0]
k = entrada_int[1]
a = input()
soma = executa(n, k, "b", 0)
soma = executa(n, k, "a", soma)
print(soma)
```
|
output
| 1
| 104,076
| 0
| 208,153
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,077
| 0
| 208,154
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
n, k = list(map(int, input().split()))
s = input()
res = 0
count_a = 0
count_b = 0
left = 0
right = 0
while True:
if s[right] == 'a':
count_a += 1
else:
count_b += 1
if count_a <= k or count_b <= k:
if right - left + 1 > res:
res = right - left + 1
else:
if s[left] == 'a':
count_a -= 1
else:
count_b -= 1
left += 1
right += 1
if n - left < res or right == n:
break
print(res)
```
|
output
| 1
| 104,077
| 0
| 208,155
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,078
| 0
| 208,156
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
def answer(n,k,A):
if n==1:
return 1
dp=[0]*(n+1)
for i in range(1,n+1):
if A[i-1]=="a":
dp[i]=dp[i-1]+1
else:
dp[i]=dp[i-1]
l=0;r=0
maxi=0
while r>=l and r<n:
x=dp[r+1]-dp[l]
if min(x, r+1-l-x)<=k:
r+=1
else:
maxi=max(maxi,r-l)
l+=1
maxi=max(maxi,r-l)
return maxi
n,k=map(int,input().split())
A=input()
print(answer(n,k,A))
```
|
output
| 1
| 104,078
| 0
| 208,157
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,079
| 0
| 208,158
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
le, ch = map(int, input().split())
st = input()
ca, cb, si, mx = [0] * 4
for x in st:
if x == 'a':
ca += 1
else:
cb += 1
if min(ca, cb) > ch:
if st[si] == 'a':
ca -= 1
else:
cb -= 1
si += 1
else:
mx += 1
print(mx)
```
|
output
| 1
| 104,079
| 0
| 208,159
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,080
| 0
| 208,160
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
from sys import stdin,stdout
def fn(a,ch):
# print(a)
ans=0
for i in range(n):
l,r=i,n-1
while l<=r:
mid=(l+r)>>1
req=a[i]+k-int(s[i]!=ch)
if a[mid]<=req:l=mid+1
else:r=mid-1
# print(i,r)
ans=max(ans,r-i+1)
return ans
for _ in range(1):#int(stdin.readline())):
# n=int(stdin.readline())
n,k=list(map(int,stdin.readline().split()))
s=input()
make_a=[];make_b=[]
make_a+=[int(s[0]=='a')]
make_b+=[int(s[0]=='b')]
for i in range(1,n):
make_a+=[make_a[-1]+int(s[i]=='a')]
make_b+=[make_b[-1]+int(s[i]=='b')]
# print(make_a)
# print(make_b)
ans=max(fn(make_a,'b'),fn(make_b,'a'))
print(ans)
'''
10 1
bbabbabbba
'''
```
|
output
| 1
| 104,080
| 0
| 208,161
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,081
| 0
| 208,162
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
num = input()
n, k = num.split()
n = int(n)
k = int(k)
string = input()
count_a = 0
count_b = 0
def check(ch):
i = 0
j = 0
count = 0
answer = 0
for i in range(n):
if(string[i] == ch):
count += 1
if(count > k):
while(count > k):
if(string[j] == ch):
count -= 1
j += 1
answer = max(answer, i-j +1)
return answer
max_a = check('a')
max_b = check('b')
print(max(max_a, max_b))
```
|
output
| 1
| 104,081
| 0
| 208,163
|
Provide tags and a correct Python 3 solution for this coding contest problem.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
|
instruction
| 0
| 104,082
| 0
| 208,164
|
Tags: binary search, dp, strings, two pointers
Correct Solution:
```
read = lambda: map(int, input().split())
n, k = read()
s = input()
i = j = 0
cur = k
while cur and j < n:
if s[j] == 'a':
cur -= 1
j += 1
while j < n and s[j] == 'b':
j += 1
ans = j
while j < n:
while i < n and s[i] == 'b':
i += 1
i += 1
j += 1
while j < n and s[j] == 'b':
j += 1
ans = max(ans, j - i)
i = j = 0
cur = k
while cur and j < n:
if s[j] == 'b':
cur -= 1
j += 1
while j < n and s[j] == 'a':
j += 1
ans = max(ans, j)
while j < n:
while i < n and s[i] == 'a':
i += 1
i += 1
j += 1
while j < n and s[j] == 'a':
j += 1
ans = max(ans, j - i)
print(ans)
```
|
output
| 1
| 104,082
| 0
| 208,165
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
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---##############################
n,k=zz()
s=z()
m=k
a=s[:k].count('a')
b=k-a
l=0
for i in range(k,n):
if s[i]=='a':a+=1
else:b+=1
while min(a,b)>k:
if s[l]=='a':a-=1
else:b-=1
l+=1
m=max(m,i-l+1)
print(m)
```
|
instruction
| 0
| 104,083
| 0
| 208,166
|
Yes
|
output
| 1
| 104,083
| 0
| 208,167
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
n, m = input().split(' ')
n, m = int(n), int(m)
ans = 0
s = input()
for i in range(2):
left, used = 0, 0
for right in range(n):
if ord(s[right]) != ord('a') + i:
used += 1
while used > m:
if ord(s[left]) != ord('a') + i:
used -= 1
left += 1
ans = max(ans, right - left + 1)
print(ans)
```
|
instruction
| 0
| 104,084
| 0
| 208,168
|
Yes
|
output
| 1
| 104,084
| 0
| 208,169
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
def main():
n, k = liee()
s = input()
l, ans, h = 1, 0, n
while l <= h:
m = (l + h) // 2
if fun(m, n, s, k):
ans, l = m, m + 1
else:
h = m - 1
print(ans)
def fun(m, n, s, k):
a, b = s[0:m].count('a'), s[0:m].count('b')
if max(a, b) + k >= m:
return 1
for i in range(m, n):
if s[i - m] == 'a':
a -= 1
else:
b -= 1
if s[i] == 'a':
a += 1
else:
b += 1
if max(a, b) + k >= m:
return 1
return 0
def fun2(s):
cnt, num = 0, s.count('0')
for i in s[1:]:
if i == '1':
cnt += 1
elif num == 1:
cnt += 1
break
else:
break
return cnt
from sys import *
import inspect
import re
from math import *
import threading
from collections import *
from pprint import pprint as pp
mod = 998244353
MAX = 10**5
def lie():
return int(input())
def liee():
return map(int, input().split())
def array():
return list(map(int, input().split()))
def deb(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bdeb\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
print('%s %d' % (m.group(1), p))
def vector(size, val=0):
vec = [val for i in range(size)]
return vec
def matrix(rowNum, colNum, val=0):
mat = []
for i in range(rowNum):
collumn = [val for j in range(colNum)]
mat.append(collumn)
return mat
def dmain():
setrecursionlimit(100000000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()
if __name__ == '__main__':
# main()
dmain()
```
|
instruction
| 0
| 104,085
| 0
| 208,170
|
Yes
|
output
| 1
| 104,085
| 0
| 208,171
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
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")
####################################################################################
n,k=map(int,input().split())
from bisect import bisect
s=input()
l1=[]
l2=[]
l3=[]
l4=[]
c,d=0,0
for i in range(n):
if s[i]=='a':
l1.append(i)
d+=1
else:
c+=1
l3.append(i)
l2.append(c)
l4.append(d)
if len(l1)!=0 and c!=0:
ans=0
ans1=0
for i in l1:
a=bisect(l2,l2[i]+k)
ans=max(ans,a-i)
for i in l3:
a=bisect(l4,l4[i]+k)
ans1=max(ans1,a-i)
if l1[0]==0:
a=bisect(l4,k)
ans1=max(ans1,a)
if l3[0]==0:
a=bisect(l2,k)
ans=max(ans,a)
else:
ans=len(s)
ans1=len(s)
print(max(ans,ans1))
```
|
instruction
| 0
| 104,086
| 0
| 208,172
|
Yes
|
output
| 1
| 104,086
| 0
| 208,173
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
aplha=[chr(i) for i in range(ord('a'),ord('z')+1)]
#for _ in range(int(input())):
n,k=map(int,input().split())
s=list(input())
out=0
for i in aplha:
l=0
r=0
t=k
while l<n and r<n:
if s[r]==i:
r+=1
else:
if t>0:
t-=1
r+=1
continue
else:
if s[l]==i:
l+=1
else:
t+=1
l+=1
out=max(out,r-l)
print(out)
```
|
instruction
| 0
| 104,087
| 0
| 208,174
|
No
|
output
| 1
| 104,087
| 0
| 208,175
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
# !/bin/env python3
# encoding: UTF-8
# βͺ H4WK3yEδΉ‘
# Mohd. Farhan Tahir
# Indian Institute Of Information Technology and Management,Gwalior
# Question Link
# https://codeforces.com/problemset/problem/676/C
#
# ///==========Libraries, Constants and Functions=============///
import sys
inf = float("inf")
mod = 1000000007
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline()
# ///==========MAIN=============///
def solve(c, s, k):
ans = 0
r = 0
balance = 0
for i in range(len(s)):
while r < len(s) and (s[r] == c or balance < k):
if s[r] != c:
balance += 1
r += 1
ans = max(ans, r-i)
if s[i] != c:
balance -= 1
return ans
def main():
n, k = get_ints()
s = input()
print(max(solve('a', s, k), solve('b', s, k)))
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 104,088
| 0
| 208,176
|
No
|
output
| 1
| 104,088
| 0
| 208,177
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
from collections import Counter
def good(s, k, l):
c = Counter()
for i in range(len(s)):
c[s[i]] += 1
if i - l >= 0:
c[s[i - l]] -= 1
if i + 1 >= l:
if min(c.values()) <= k:
return True
return False
def solve(s, k):
low = 1
high = 1
while good(s, k, high):
high *= 2
while low + 1 < high:
mid = (low + high) // 2
if good(s, k, mid):
low = mid
else:
high = mid
return low
n, k = map(int, input().split())
s = input()
print(solve(s, k))
```
|
instruction
| 0
| 104,089
| 0
| 208,178
|
No
|
output
| 1
| 104,089
| 0
| 208,179
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 0 β€ k β€ n) β the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer β the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Examples
Input
4 2
abba
Output
4
Input
8 1
aabaabaa
Output
5
Note
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
Submitted Solution:
```
n, k = [int(i) for i in input().split()]
s = input()
def beauty (x):
ret = 0
cnt = 0
r = 0
for l in range(n):
while r < n and (cnt < k or s[r] != x):
if s[r] == x:
cnt += 1
r += 1
if r == x:
cnt -= 1
ret = max(ret, r-l)
return ret
print(max(beauty('a'), beauty('b')))
exit(0)
```
|
instruction
| 0
| 104,090
| 0
| 208,180
|
No
|
output
| 1
| 104,090
| 0
| 208,181
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,136
| 0
| 208,272
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
s = input()
n = len(s)
max_palin = [[0 for i in range(n + 1)] for j in range(n + 1)]
count = [0 for i in range(n + 1)]
for sub_len in range(1, n + 1):
for left in range(0, n - sub_len + 1):
right = left + sub_len - 1
if sub_len == 1:
max_palin[left][right] = 1
elif sub_len == 2:
if s[left] == s[right]:
max_palin[left][right] = 2
else:
max_palin[left][right] = 0
else:
if s[left] == s[right] and max_palin[left + 1][right - 1] > 0:
mid = (left + right) // 2
if sub_len % 2 == 0:
max_palin[left][right] = max_palin[left][mid] + 1
else:
max_palin[left][right] = max_palin[left][mid - 1] + 1
count[max_palin[left][right]] += 1
for i in range(n - 1, 0, -1):
count[i] += count[i + 1]
for i in range(1, n + 1):
print(count[i], end=' ')
print()
```
|
output
| 1
| 104,136
| 0
| 208,273
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,137
| 0
| 208,274
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
class HString:
def __init__(self, string, base=257, modulo=1000000007):
self.__base, self.__modulo = base, modulo
self.__prefix_hash, self.__base_pow, self.__size = [], [1], 0
self += string
def __add__(self, string):
for ch in string:
self.__base_pow.append((self.__base_pow[-1] * self.__base) % self.__modulo)
if self.__size == 0: self.__prefix_hash.append(ord(ch))
else: self.__prefix_hash.append((self.__prefix_hash[-1] * self.__base + ord(ch)) % self.__modulo)
self.__size += 1
return self
def size(self): return self.__size
def getModulo(self): return self.__modulo
def getHashValue(self, st, en):
value = self.__prefix_hash[en]
if st > 0:
value -= ((self.__prefix_hash[st-1] * self.__base_pow[en-st+1]) % self.__modulo)
if value < 0: value += self.__modulo
return value
def palindromic_characteristics(s):
n, org, rev = len(s), HString(s), HString(s[::-1])
palindrome_level = [[0 for _ in range(n)] for _ in range(n)]
palindrome_level_count = [0 for _ in range(n + 1)]
for i in range(n):
for j in range(i, n):
if org.getHashValue(i, j) == rev.getHashValue(n-1-j, n-1-i):
mid = (i + j) // 2 + (i + j) % 2
if i > mid-1: palindrome_level[i][j] = 1
else: palindrome_level[i][j] = palindrome_level[i][mid-1] + 1
palindrome_level_count[palindrome_level[i][j]] += 1
for i in range(n-1, 0, -1): palindrome_level_count[i] += palindrome_level_count[i+1]
return palindrome_level_count[1:]
s = input()
print(' '.join(map(str, palindromic_characteristics(s))))
```
|
output
| 1
| 104,137
| 0
| 208,275
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,138
| 0
| 208,276
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
def PanlidromicCharacteristics(string):
n = len(string)
res = [[0 for i in range (n)] for j in range (n)]
count = [0 for i in range (n + 1)]
# for i in range (n):
# res[i][i] = 1
# count[1] += 1
for length in range (1, n + 1):
for i in range (n-length + 1):
j = i + length - 1
if length == 1:
res[i][j] = 1
elif length == 2 and string[i] == string[j]:
res[i][j] = 2
elif string[i] == string[j] and res[i + 1][j - 1] > 0:
res[i][j] = res[i][i + length//2 - 1] + 1
count[res[i][j]] += 1
# k-palindrome is also a (k - 1)-palindrome
for i in range (len(count) - 1, 0, -1):
count[i - 1] += count[i]
for i in range (1, len(count)):
print(count[i], end = " ")
return
string = input()
PanlidromicCharacteristics(string)
```
|
output
| 1
| 104,138
| 0
| 208,277
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,139
| 0
| 208,278
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
s = input()
n = len(s)
isPalindrome = [[False for i in range(n)] for i in range(n)]
for i in range(n):
isPalindrome[i][i] = True
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
if (s[i] == s[j] and (i + 1 == j or isPalindrome[i + 1][j - 1] == True)):
isPalindrome[i][j] = True
degreePalindrome = [[0 for i in range(n)] for i in range(n)]
#degreePalindrome[i][j] = degreePalindrome[i][mid] + 1
res = [0] * (n + 1)
for i in range(n):
for j in range(i, n):
if (i == j):
degreePalindrome[i][j] = 1
elif isPalindrome[i][j]:
mid = (i + j - 1) // 2
degreePalindrome[i][j] = degreePalindrome[i][mid] + 1
res[degreePalindrome[i][j]] += 1
for i in range( n - 2, 0, -1):
res[i] += res[i + 1]
print(*res[1::])
```
|
output
| 1
| 104,139
| 0
| 208,279
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,140
| 0
| 208,280
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
s = input()
n = len(s)
dp = [[0 for i in range(n - le + 1)] for le in range(n + 1)]
ans = [0 for i in range(n + 1)]
for le in range(1, n + 1):
for l in range(0, n - le + 1):
r = l + le
if s[l] != s[r - 1]:
continue
if le == 1:
dp[1][l] = 1
ans[1] += 1
elif le == 2:
ans[2] += 1
dp[2][l] = 2
elif dp[le - 2][l + 1]:
v = 1
m = (l + r) // 2
st = m + 1 if le & 1 else m
le2 = m - l
q = dp[le2][l]
if q:
v = q + 1
ans[v] += 1
dp[le][l] = v
for i in range(n - 1, 0, -1):
ans[i] += ans[i + 1]
print(*ans[1:])
```
|
output
| 1
| 104,140
| 0
| 208,281
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,141
| 0
| 208,282
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
s = input()
size = len(s)
dp = [[0 for l in range(size)] for li in range(size)]
ans = [0]*(size+1)
for i in range(1, size+1):
if i == 1:
for j in range(0, size):
dp[j][j] = 1
ans[1] += 1
elif i == 2:
for j in range(0, size-1):
if s[j+1] == s[j]:
dp[j][j+1] = 2
ans[1] += 1
ans[2] += 1
else:
dp[j][j+1] = 0
else:
for j in range(0, size-i+1):
if s[j] != s[j+i-1] or dp[j+1][j+i-2] == 0:
dp[j][j+i-1] = 0
else:
dp[j][j+i-1] = dp[j][int((j+j+i-2)/2)] + 1
for p in range(1, dp[j][j+i-1]+1):
ans[p] += 1
for i in range(1, size):
print(ans[i], end="")
print(" ", end="")
print(ans[size])
```
|
output
| 1
| 104,141
| 0
| 208,283
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,142
| 0
| 208,284
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
def main():
s = input()
n = len(s)
isPalindrome = [[False for i in range(n + 1)] for i in range(n + 1)]
for i in range(n):
isPalindrome[i][i] = True
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
if (s[i] == s[j] and (i + 1 == j or isPalindrome[i + 1][j - 1] == True)):
isPalindrome[i][j] = True
degreePalindrome = [[0 for i in range(n)] for i in range(n)]
#degreePalindrome[i][j] = degreePalindrome[i][mid] + 1
res = [0] * (n + 1)
for i in range(n):
for j in range(i, n):
if (i == j):
degreePalindrome[i][j] = 1
elif isPalindrome[i][j]:
mid = (i + j - 1) // 2
degreePalindrome[i][j] = degreePalindrome[i][mid] + 1
res[degreePalindrome[i][j]] += 1
for i in range( n - 2, 0, -1):
res[i] += res[i + 1]
print(*res[1::])
if __name__ == "__main__":
main()
```
|
output
| 1
| 104,142
| 0
| 208,285
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
instruction
| 0
| 104,143
| 0
| 208,286
|
Tags: brute force, dp, hashing, strings
Correct Solution:
```
s = input()
dp = [[0]*5005 for _ in range(5005)]
n = len(s)
ans = [0 for _ in range(5005)]
for length in range(1,n+1):
for l in range(n-length+1):
r = l+length
if(length == 1):
dp[l][r] = 1
continue
elif(length == 2):
dp[l][r] = 2 if(s[l] == s[r-1]) else 0
continue
if(s[l] != s[r-1] or dp[l+1][r-1] == 0):
continue
dp[l][r] = 1
m = (l+r) // 2
if(length&1):
if(dp[l][m] and dp[m+1][r]):
dp[l][r] = dp[l][m]+1
else:
if(dp[l][m] and dp[m][r]):
dp[l][r] = dp[l][m]+1
for length in range(1,n+1):
for l in range(n-length+1):
ans[dp[l][l+length]] += 1
for i in range(n-1,0,-1):
ans[i] += ans[i+1]
for i in range(1,n+1):
print(ans[i],end=' ')
print()
```
|
output
| 1
| 104,143
| 0
| 208,287
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
class HString:
def __init__(self, string, base=257, modulo=1000000007):
self.__base, self.__modulo = base, modulo
self.__prefix_hash, self.__base_pow, self.__size = [], [1], 0
self += string
def __add__(self, string):
for ch in string:
self.__base_pow.append((self.__base_pow[-1] * self.__base) % self.__modulo)
if self.__size == 0: self.__prefix_hash.append(ord(ch))
else: self.__prefix_hash.append((self.__prefix_hash[-1] * self.__base + ord(ch)) % self.__modulo)
self.__size += 1
return self
def size(self): return self.__size
def getModulo(self): return self.__modulo
def getHashValue(self, st, en):
value = self.__prefix_hash[en]
if st > 0:
value -= ((self.__prefix_hash[st-1] * self.__base_pow[en-st+1]) % self.__modulo)
if value < 0: value += self.__modulo
return value
def palindromic_characteristics(s):
n, org, rev = len(s), HString(s), HString(s[::-1])
palindrome_level = [[0 for _ in range(n)] for _ in range(n)]
palindrome_level_count = [0 for _ in range(n + 1)]
i, j = 0, 0
while i < n:
j = i
while j < n:
if org.getHashValue(i, j) == rev.getHashValue(n-1-j, n-1-i):
mid = (i + j) // 2 + (i + j) % 2
if i > mid-1: palindrome_level[i][j] = 1
else: palindrome_level[i][j] = palindrome_level[i][mid-1] + 1
palindrome_level_count[palindrome_level[i][j]] += 1
j += 1
i += 1
for i in range(n-1, 0, -1): palindrome_level_count[i] += palindrome_level_count[i+1]
return palindrome_level_count[1:]
s = input()
print(' '.join(map(str, palindromic_characteristics(s))))
```
|
instruction
| 0
| 104,144
| 0
| 208,288
|
Yes
|
output
| 1
| 104,144
| 0
| 208,289
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
s = input()
n = len(s)
max_palin = [[0 for i in range(n + 1)] for j in range(n + 1)]
count = [0 for i in range(n + 1)]
for sub_len in range(1, n + 1):
for left in range(0, n - sub_len + 1):
right = left + sub_len - 1
if sub_len == 1:
max_palin[left][right] = 1
elif sub_len == 2:
if s[left] == s[right]:
max_palin[left][right] = 2
else:
max_palin[left][right] = 0
else:
if s[left] == s[right] and max_palin[left + 1][right - 1] > 0:
max_palin[left][right] = max_palin[left][left + sub_len // 2 - 1] + 1
count[max_palin[left][right]] += 1
for i in range(n - 1, 0, -1):
count[i] += count[i + 1]
for i in range(1, n + 1):
print(count[i], end=' ')
print()
```
|
instruction
| 0
| 104,145
| 0
| 208,290
|
Yes
|
output
| 1
| 104,145
| 0
| 208,291
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
def main():
s = input()
n = len(s)
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
count = [0 for i in range(n + 1)]
for sub_len in range(1, n + 1):
for left in range(0, n - sub_len + 1):
right = left + sub_len - 1
if sub_len == 1:
dp[left][right] = 1
elif sub_len == 2:
if s[left] == s[right]:
dp[left][right] = 2
else:
if s[left] == s[right] and dp[left + 1][right - 1] > 0:
dp[left][right] = dp[left][left + sub_len // 2 - 1] + 1
count[dp[left][right]] += 1
for i in range(n - 1, 0, -1):
count[i] += count[i + 1]
for i in range(1, n + 1):
print(count[i], end=' ')
print()
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 104,146
| 0
| 208,292
|
Yes
|
output
| 1
| 104,146
| 0
| 208,293
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
P = 311
# we use two mods to reduce the chance of collision
MOD1 = int(1e9) + 7
MOD2 = int(1e9) + 9
def main():
s = input()
n = len(s)
# Pre-compute
power_1 = [0 for i in range(n + 1)]
power_2 = [0 for i in range(n + 1)]
mod_inv_1 = [0 for i in range(n + 1)]
mod_inv_2 = [0 for i in range(n + 1)]
power_1[0] = 1
power_2[0] = 1
mod_inv_1[0] = 1
mod_inv_2[0] = 1
for i in range(1, n + 1):
power_1[i] = power_1[i - 1] * P % MOD1
power_2[i] = power_2[i - 1] * P % MOD1
mod_inv_1[i] = bin_exp(power_1[i], MOD1 - 2, MOD1)
mod_inv_2[i] = bin_exp(power_2[i], MOD2 - 2, MOD2)
# Compute hash values
hash_1 = 0
hash_2 = 0
forward_hash_1 = [0 for i in range(n + 1)]
forward_hash_2 = [0 for i in range(n + 1)]
for i in range(1, n + 1):
hash_1 += ord(s[i - 1]) * power_1[i]
hash_2 += ord(s[i - 1]) * power_2[i]
hash_1 %= MOD1
hash_2 %= MOD2
forward_hash_1[i] = hash_1
forward_hash_2[i] = hash_2
hash_1 = 0
hash_2 = 0
backward_hash_1 = [0 for i in range(n + 1)]
backward_hash_2 = [0 for i in range(n + 1)]
for i in range(1, n + 1):
hash_1 += ord(s[n - i]) * power_1[i]
hash_2 += ord(s[n - i]) * power_2[i]
hash_1 %= MOD1
hash_2 %= MOD2
backward_hash_1[i] = hash_1
backward_hash_2[i] = hash_2
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
count = [0 for i in range(n + 1)]
for sub_len in range(1, n + 1):
for left in range(0, n - sub_len + 1):
right = left + sub_len - 1
if sub_len == 1:
dp[left][right] = 1
elif sub_len == 2:
if s[left] == s[right]:
dp[left][right] = 2
else:
if s[left] == s[right] and dp[left + 1][right - 1] > 0:
dp[left][right] = dp[left][left + sub_len // 2 - 1] + 1
count[dp[left][right]] += 1
for i in range(n - 1, 0, -1):
count[i] += count[i + 1]
for i in range(1, n + 1):
print(count[i], end=' ')
print()
def bin_exp(a, x, mod):
res = 1
while x > 0:
if x & 1:
res *= a
res %= mod
a *= a
a %= mod
x >>= 1
return res
def get_forward_hash(forward_hash, mod_inv, left, right, mod):
return (forward_hash[right + 1] - forward_hash[left] + mod) * mod_inv[left] % mod
def get_backward_hash(backward_hash, mod_inv, n, left, right):
r_left = n - left - 1
r_right = n - right - 1
return (backward_hash[r_left + 1] - backward_hash[r_right] + mod) * mod_inv[r_right] % mod
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 104,147
| 0
| 208,294
|
Yes
|
output
| 1
| 104,147
| 0
| 208,295
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
def palh(s):
if s[:(len(s)//2)]==s[-(len(s)//2):]:
return palh(s[:(len(s)//2)])+1
else:
return 0
S=input()
N=len(S)
R=[0 for i in range(N+1)]
for l in range(0, N):
for r in range(l+1, N):
for i in range(0, palh(S[l:r])+1):
R[i]+=1
print(*R[:-1])
```
|
instruction
| 0
| 104,148
| 0
| 208,296
|
No
|
output
| 1
| 104,148
| 0
| 208,297
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
1. Its left half equals to its right half.
2. Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
Input
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
Output
Print |s| integers β palindromic characteristics of string s.
Examples
Input
abba
Output
6 1 0 0
Input
abacaba
Output
12 4 1 0 0 0 0
Note
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
Submitted Solution:
```
s = input()
size = len(s)
dp = [[0 for l in range(size)] for l in range(size)]
ans = [0]*(size+1)
for i in range(1, size+1):
if i == 1:
for j in range(0, size):
dp[j][j] = 1
ans[1] += 1
elif i == 2:
for j in range(0, size-1):
if s[j+1] == s[j]:
dp[j][j+1] = 2
ans[1] += 1
ans[2] += 1
else:
dp[j][j+1] = 0
else:
for j in range(0, size-i+1):
if s[j] != s[j+i-1] or dp[j+1][j+i-2] == 0:
dp[j][j+i-1] = 0
else:
dp[j][j+i-1] = dp[j][int((j+j+i-2)/2)] + 1
ans[dp[j][j+i-1]] += 1
for i in range(1, size):
print(ans[i], end="")
print(" ", end="")
print(ans[size])
```
|
instruction
| 0
| 104,149
| 0
| 208,298
|
No
|
output
| 1
| 104,149
| 0
| 208,299
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 β we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd.
You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T.
Input
The first line contains the string S, and the second β the string T (1 β€ |T| β€ |S| β€ 125000). Both strings consist of lowercase Latin letters from a to f.
Output
Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T.
Example
Input
abcdefa
ddcb
Output
2 3 3 3
Submitted Solution:
```
class UnionFind:
def __init__(self, n):
self.parent = [-1] * n
self.cnt = n
def root(self, x):
if self.parent[x] < 0:
return x
else:
self.parent[x] = self.root(self.parent[x])
return self.parent[x]
def merge(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.parent[x] > self.parent[y]:
x, y = y, x
self.parent[x] += self.parent[y]
self.parent[y] = x
self.cnt -= 1
def is_same(self, x, y):
return self.root(x) == self.root(y)
def get_cnt(self):
return self.cnt
s = [ord(char) - 97 for char in input()]
t = [ord(char) - 97 for char in input()][::-1]
s_ = [0] * 6
t_ = [0] * 6
st = [[0] * 6 for _ in range(6)]
for i, val in enumerate(s):
s_[val] |= 1 << i
for i, val in enumerate(t):
t_[val] |= 1 << i
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t) + 100)
length = len(t) - 1
ans = []
for i in range(len(s) - len(t) + 1):
shift = length + i
uf = UnionFind(6)
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
if st[ind_s][ind_t][shift] == "1":
uf.merge(ind_s, ind_t)
ans.append(6 - uf.get_cnt())
print(*ans)
```
|
instruction
| 0
| 104,168
| 0
| 208,336
|
No
|
output
| 1
| 104,168
| 0
| 208,337
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 β we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd.
You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T.
Input
The first line contains the string S, and the second β the string T (1 β€ |T| β€ |S| β€ 125000). Both strings consist of lowercase Latin letters from a to f.
Output
Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T.
Example
Input
abcdefa
ddcb
Output
2 3 3 3
Submitted Solution:
```
import sys
input = sys.stdin.readline
class UnionFind:
def __init__(self, n):
self.parent = [-1] * n
self.cnt = n
def root(self, x):
if self.parent[x] < 0:
return x
else:
self.parent[x] = self.root(self.parent[x])
return self.parent[x]
def merge(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.parent[x] > self.parent[y]:
x, y = y, x
self.parent[x] += self.parent[y]
self.parent[y] = x
self.cnt -= 1
def is_same(self, x, y):
return self.root(x) == self.root(y)
def get_cnt(self):
return self.cnt
s = [ord(char) - 97 for char in input()[:-1]]
t = [ord(char) - 97 for char in input()[:-1]][::-1]
s_ = [0] * 6
t_ = [0] * 6
st = [[0] * 6 for i in range(6)]
for i, val in enumerate(s):
s_[val] |= 1 << i
for i, val in enumerate(t):
t_[val] |= 1 << i
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t))
length = len(t) - 1
ans = []
for i in range(len(s) - len(t) + 1):
shift = length + i
uf = UnionFind(6)
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
if st[ind_s][ind_t][shift] == "1":
uf.merge(ind_s, ind_t)
ans.append(6 - uf.get_cnt())
print(*ans)
```
|
instruction
| 0
| 104,169
| 0
| 208,338
|
No
|
output
| 1
| 104,169
| 0
| 208,339
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 β we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd.
You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T.
Input
The first line contains the string S, and the second β the string T (1 β€ |T| β€ |S| β€ 125000). Both strings consist of lowercase Latin letters from a to f.
Output
Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T.
Example
Input
abcdefa
ddcb
Output
2 3 3 3
Submitted Solution:
```
class UnionFind:
def __init__(self, n):
self.parent = [-1] * n
self.cnt = n
def root(self, x):
if self.parent[x] < 0:
return x
else:
self.parent[x] = self.root(self.parent[x])
return self.parent[x]
def merge(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.parent[x] > self.parent[y]:
x, y = y, x
self.parent[x] += self.parent[y]
self.parent[y] = x
self.cnt -= 1
def is_same(self, x, y):
return self.root(x) == self.root(y)
def get_cnt(self):
return self.cnt
s = [ord(char) - 97 for char in input()]
t = [ord(char) - 97 for char in input()][::-1]
s_ = [0] * 6
t_ = [0] * 6
st = [[0] * 6 for _ in range(6)]
for i, val in enumerate(s):
s_[val] |= 1 << i
for i, val in enumerate(t):
t_[val] |= 1 << i
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t) + 100)
length = len(t) - 1
ans = []
for i in range(len(s) - len(t) + 1):
shift = length + i
uf = UnionFind(6)
for ind_s in range(6):
for ind_t in range(6):
if ind_s == ind_t:
continue
if st[ind_s][ind_t][shift] == "1":
uf.merge(ind_s, ind_t)
ans.append(6 - uf.get_cnt())
if len(s) > 100:
print(len(s))
print(len(t))
print(*ans)
```
|
instruction
| 0
| 104,170
| 0
| 208,340
|
No
|
output
| 1
| 104,170
| 0
| 208,341
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,539
| 0
| 209,078
|
Tags: implementation, math
Correct Solution:
```
"""
NTC here
"""
from sys import stdin
# import threading
# setrecursionlimit(10**6)
# threading.stack_size(2**26)
def iin(): return int(stdin.readline())
def lin(): return list(map(int, stdin.readline().split()))
# range = xrange
# input = raw_input
md=10**9+7
def main():
t=iin()
while t:
t-=1
x=iin()
s=list(input())
l=len(s)
ans=l
i=0
while i<x:
i+=1
ch=int(s[i-1])
ans=(i+ch*(ans-i))%md
if l>=x:continue
incr=l
ss=[]
for _ in range(ch):
for j in range(i,l):
if incr<ans and incr<x:
ss.append(s[j])
incr+=1
else:
break
l=incr
for k in ss:s.append(k)
#print(ans, l, s)
print(ans%md)
main()
#threading.Thread(target=main).start()
```
|
output
| 1
| 104,539
| 0
| 209,079
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,540
| 0
| 209,080
|
Tags: implementation, math
Correct Solution:
```
M = int(1e9+7)
n = int(input())
while n>0:
n -= 1
x = int(input())
s = input()
idx = 0
length = len(s)
while length < x:
t = ord(s[idx])-ord('1')
s += s[idx+1:]*t
length += (length-idx-1)*t
idx += 1
while idx != x:
t = ord(s[idx])-ord('1')
length += ((length-idx-1)*t)%M
idx += 1
print(length%M)
```
|
output
| 1
| 104,540
| 0
| 209,081
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,541
| 0
| 209,082
|
Tags: implementation, math
Correct Solution:
```
for _ in range(int(input())):
x = int(input())
s = list(input())
l = 0
ans = len(s)
while(l<x):
if(ans>=x):
break
temp = len(s)
ans+=(int(s[l])-1)*(temp-l-1)
for i in range(int(s[l])-1):
if(len(s)>x):
break
for j in range(l+1,temp):
s.append(s[j])
if(len(s)>x):
break
l+=1
while(l<x):
temp = ans
ans+=(int(s[l])-1)*(temp-l-1)
ans = ans%(10**9+7)
l+=1
print(ans)
```
|
output
| 1
| 104,541
| 0
| 209,083
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,542
| 0
| 209,084
|
Tags: implementation, math
Correct Solution:
```
M = 1000000007
t = int(input())
while t:
t += -1
x = int(input())
s = input()
i = 0
while len(s) < x:
s += s[i + 1: ] * (int(s[i]) - 1)
i += 1
n = len(s)
for j in range(i, x):
tmp = n - (j + 1)
n = j + 1 + tmp * (int(s[j]))
n = n % M
print(n % M)
```
|
output
| 1
| 104,542
| 0
| 209,085
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,543
| 0
| 209,086
|
Tags: implementation, math
Correct Solution:
```
def main():
TT = int(input())
for _ in range(TT):
x = int(input())
s = [int(c) for c in input().strip()]
ans = len(s)
for l in range(0, x):
ans += (ans - l - 1) * (s[l] - 1)
ans %= 1000000007
end = len(s)
for _ in range(1, s[l]):
if len(s) > x + 1:
break
for c in range(l + 1, end):
if len(s) > x + 1:
break
s.append(s[c])
print(ans)
if __name__ == '__main__':
main()
```
|
output
| 1
| 104,543
| 0
| 209,087
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,544
| 0
| 209,088
|
Tags: implementation, math
Correct Solution:
```
ttt = int(input())
mod = 10**9 + 7
s = "#"*(10**6 + 1500)
s=list(s)
for bo in range(ttt):
x=int(input())
st=input()
s[1:len(st)+1]=list(st)
siz=len(st)
p = 0
while siz < x:
p += 1
whe = siz
for de4d in range(int(s[p])-1):
for k in range(p+1,whe+1):
siz += 1
s[siz] = s[k]
if (siz>x):
break
if (siz>x):
break
if (siz>x):
break
ans = len(st)
for i in range(1,x+1):
ans = i + (ans-i)*int(s[i])
ans = ans%mod
print(ans)
```
|
output
| 1
| 104,544
| 0
| 209,089
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,545
| 0
| 209,090
|
Tags: implementation, math
Correct Solution:
```
t = int(input())
ans = []
arr = [None] * (10 ** 6)
cur = 0
MOD = 10 ** 9 + 7
for _ in range(t):
x, s = int(input()), list(map(int, str(input())))
max_p = cur + x
for i in range(cur, min(max_p, cur + len(s))):
arr[i] = s[i - cur]
def copy(start, length, times):
b = start + length
for i in range(times):
for j in range(length):
if b >= max_p: return
arr[b] = arr[start + j]
b += 1
val = len(s)
cnt = 1
while x:
if arr[max_p - 1] is None and arr[cur] > 1:
copy(cur + 1, val - cnt, arr[cur] - 1)
val = (cnt + (val - cnt) * arr[cur]) % MOD
cnt += 1
cur += 1
x -= 1
ans.append(val)
for a in ans:
print(a)
```
|
output
| 1
| 104,545
| 0
| 209,091
|
Provide tags and a correct Python 3 solution for this coding contest problem.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
|
instruction
| 0
| 104,546
| 0
| 209,092
|
Tags: implementation, math
Correct Solution:
```
def solve(s,x):
n = len(s)
mod = 10**9 + 7
for i in range(x):
n += (n-i-1)*(int(s[i])-1)
n %= mod
if len(s) < x:
s += s[i+1:]*(int(s[i])-1)
return n
for _ in range(int(input())):
x = int(input())
s = input()
print(solve(s,x))
```
|
output
| 1
| 104,546
| 0
| 209,093
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i.
There is one cursor. The cursor's location β is denoted by an integer in \{0, β¦, |s|\}, with the following meaning:
* If β = 0, then the cursor is located before the first character of s.
* If β = |s|, then the cursor is located right after the last character of s.
* If 0 < β < |s|, then the cursor is located between s_β and s_{β+1}.
We denote by s_left the string to the left of the cursor and s_right the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
* The Move action. Move the cursor one step to the right. This increments β once.
* The Cut action. Set c β s_right, then set s β s_left.
* The Paste action. Append the value of c to the end of the string s. Note that this doesn't modify c.
The cursor initially starts at β = 0. Then, we perform the following procedure:
1. Perform the Move action once.
2. Perform the Cut action once.
3. Perform the Paste action s_β times.
4. If β = x, stop. Otherwise, return to step 1.
You're given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 10^9 + 7.
It is guaranteed that β β€ |s| at any time.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1 β€ x β€ 10^6). The second line of each test case consists of the initial string s (1 β€ |s| β€ 500). It is guaranteed, that s consists of the characters "1", "2", "3".
It is guaranteed that the sum of x in a single file is at most 10^6. It is guaranteed that in each test case before the procedure will stop it will be true that β β€ |s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10^9 + 7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let's illustrate what happens with the first test case. Initially, we have s = 231. Initially, β = 0 and c = \varepsilon (the empty string). The following things happen if we follow the procedure above:
* Step 1, Move once: we get β = 1.
* Step 2, Cut once: we get s = 2 and c = 31.
* Step 3, Paste s_β = 2 times: we get s = 23131.
* Step 4: β = 1 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 2.
* Step 2, Cut once: we get s = 23 and c = 131.
* Step 3, Paste s_β = 3 times: we get s = 23131131131.
* Step 4: β = 2 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 3.
* Step 2, Cut once: we get s = 231 and c = 31131131.
* Step 3, Paste s_β = 1 time: we get s = 23131131131.
* Step 4: β = 3 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 4.
* Step 2, Cut once: we get s = 2313 and c = 1131131.
* Step 3, Paste s_β = 3 times: we get s = 2313113113111311311131131.
* Step 4: β = 4 not= x = 5, so we return to step 1.
* Step 1, Move once: we get β = 5.
* Step 2, Cut once: we get s = 23131 and c = 13113111311311131131.
* Step 3, Paste s_β = 1 times: we get s = 2313113113111311311131131.
* Step 4: β = 5 = x, so we stop.
At the end of the procedure, s has length 25.
Submitted Solution:
```
for _ in range(int(input())):
x=int(input())
a=input()
l=len(a)
a=[i for i in a]
ans=l
p=0
m=10**9+7
while ans<x:
for j in range(int(a[p])-1):
for i in range(p+1,ans):
a.append(a[i])
ans+=(int(a[p])-1)*((ans-p-1+m)%m)
p+=1
for j in range(p,x):
ans+=(int(a[j])-1)*((ans-j-1+m)%m)
ans%=m
print(ans)
```
|
instruction
| 0
| 104,547
| 0
| 209,094
|
Yes
|
output
| 1
| 104,547
| 0
| 209,095
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.