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.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
x=input()
s=0
n=len(x)
for i in range(len(x)):
if x[i]=="S":
s+=1
elif s>0:
s-=1
n-=2
print(n)
``` | instruction | 0 | 57,891 | 0 | 115,782 |
Yes | output | 1 | 57,891 | 0 | 115,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
t=m=0
for c in input():t+=2if c=="T"else-2;m=max(m,t)
print(m)
``` | instruction | 0 | 57,892 | 0 | 115,784 |
Yes | output | 1 | 57,892 | 0 | 115,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
X = input()
N = len(X)
t = 0
ans = N
for c in X[::-1]:
if c == 'T':
t += 1
elif t > 0:
ans -= 2
t -= 1
print(ans)
``` | instruction | 0 | 57,893 | 0 | 115,786 |
Yes | output | 1 | 57,893 | 0 | 115,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
a=b=0
for i in input():c=i=="T";d=a>0;a+=1-c-c*d;b+=c*(1-d)
print(a+b)
``` | instruction | 0 | 57,894 | 0 | 115,788 |
Yes | output | 1 | 57,894 | 0 | 115,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
x = input()
#tのラストとsの最初を知りたい
n = len(x)
t1 = 0
s1 = 0
a = 0
su = 0
for i in range(n):
if x[i]=="T":
t1 = i
else:
if a==0:
s1 = i
a = 1
#print(t1,s1)
#後ろにあるSは残る
sn = n//2-(n-t1-1)
tn = n//2-(s1)
su = 2*min(sn,tn)
print(n-su)
``` | instruction | 0 | 57,895 | 0 | 115,790 |
No | output | 1 | 57,895 | 0 | 115,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
import itertools
X = input().strip()
continuous_S = 0
continuous_T = 0
ans = len(X)
for key, value in itertools.groupby(X):
if key == 'S':
continuous_S = len(list(value))
else:
continuous_T = len(list(value))
ans -= min(continuous_S, continuous_T) * 2
print(ans)
``` | instruction | 0 | 57,896 | 0 | 115,792 |
No | output | 1 | 57,896 | 0 | 115,793 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
import numpy as np
import functools
import math
import scipy
import fractions
import itertools
def solve():
x = input()
le = len(x)
limit = len(x)//2
i = 0
while True:
bit = 0
for i in range(le-1):
if x[i:i+2] == "ST":
x = x[0:i]+x[i+2:le]
bit = 1
le -= 2
break
if bit == 0:
print(len(x))
break
if i >= limit:
break
return 0
if __name__ == "__main__":
solve()
``` | instruction | 0 | 57,897 | 0 | 115,794 |
No | output | 1 | 57,897 | 0 | 115,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`.
Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times:
* Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.
Constraints
* 2 ≦ |X| ≦ 200,000
* The length of X is even.
* Half the characters in X are `S`, and the other half are `T`.
Input
The input is given from Standard Input in the following format:
X
Output
Print the eventual length of X.
Examples
Input
TSTTSS
Output
4
Input
SSTTST
Output
0
Input
TSSTTTSS
Output
4
Submitted Solution:
```
# -*- coding: utf-8 -*-
text = list(input())
txt_stuck = []
for i in text:
if i == 'S':
txt_stuck.append(i)
elif i == 'T' and txt_stuck == []:
txt_stuck.append(i)
elif i == 'T' and txt_stuck[-1] == 'T':
txt_stuck.append(i)
elif i == 'T' and txt_stuck[-1] == 'S':
txt_stuck.pop()
print(''.join(txt_stuck))
``` | instruction | 0 | 57,898 | 0 | 115,796 |
No | output | 1 | 57,898 | 0 | 115,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon likes strings. Today he created a game about strings:
String s_1, s_2, …, s_n is beautiful if and only if for each 1 ≤ i < n, s_i ≠ s_{i+1}.
Initially, Dreamoon has a string a. In each step Dreamoon can choose a beautiful substring of a and remove it. Then he should concatenate the remaining characters (in the same order).
Dreamoon wants to use the smallest number of steps to make a empty. Please help Dreamoon, and print any sequence of the smallest number of steps to make a empty.
Input
The first line contains an integer t (1 ≤ t ≤ 200 000), denoting the number of test cases in the input.
For each test case, there's one line with a non-empty string of lowercase Latin letters a.
The total sum of lengths of strings in all test cases is at most 200 000.
Output
For each test case, in the first line, you should print m: the smallest number of steps to make a empty. Each of the following m lines should contain two integers l_i, r_i (1 ≤ l_i ≤ r_i ≤ |a|), denoting, that the i-th step is removing the characters from index l_i to r_i in the current string. (indices are numbered starting from 1).
Note that after the deletion of the substring, indices of remaining characters may change, and r_i should be at most the current length of a.
If there are several possible solutions, you can print any.
Example
Input
4
aabbcc
aaabbb
aaa
abacad
Output
3
3 3
2 4
1 2
3
3 4
2 3
1 2
3
1 1
1 1
1 1
1
1 6
Submitted Solution:
```
def build(lo, hi, seg):
left[seg] = lo
right[seg] = hi
if lo == hi - 1:
return
else:
mid = (lo + hi)//2
build(lo, mid, 2 * seg + 1)
build(mid, hi, 2 * seg + 2)
seg_tree[seg] = seg_tree[2 * seg + 1] + seg_tree[2 * seg + 2]
def update(i, upd, seg):
if left[seg] <= i < right[seg]:
if left[seg] == right[seg] - 1:
seg_tree[seg] += upd
else:
update(i, upd, 2 * seg + 1)
update(i, upd, 2 * seg + 2)
seg_tree[seg] += upd
def query(seg, i, j):
if i >= right[seg] or j <= left[seg]:
return 0
elif left[seg] >= i and right[seg] <= j:
return seg_tree[seg]
else:
return query(2 * seg + 1, i, j) + query(2 * seg + 2, i, j)
def MakeSet(x):
x.parent = x
x.rank = 0
def Union(x, y):
xRoot = Find(x)
yRoot = Find(y)
leftNew = min(xRoot.left, yRoot.left)
rightNew = max(xRoot.right, yRoot.right)
sizeNew = xRoot.size + yRoot.size
mainNew = y.main
if xRoot.rank > yRoot.rank:
yRoot.parent = xRoot
elif xRoot.rank < yRoot.rank:
xRoot.parent = yRoot
elif xRoot != yRoot:
yRoot.parent = xRoot
xRoot.rank = xRoot.rank + 1
Find(x).left = leftNew
Find(x).right = rightNew
Find(x).size = sizeNew
Find(x).main = mainNew
def Find(x):
if x.parent == x:
return x
else:
x.parent = Find(x.parent)
return x.parent
class Node:
def __init__ (self, label, size,main):
self.label = label
self.left = label
self.right = label
self.size = size
self.main = main
def __str__(self):
return self.label
import sys
input = sys.stdin.readline
import heapq
outTotal = []
t = int(input())
for _ in range(t):
out = []
s = input().strip() + '0'
l = []
lsize = []
start = 0
curr = s[0]
for i in range(1, len(s)):
if s[i] != curr:
if i > start + 1:
l.append((start, i - 1))
lsize.append(i - 1 - start)
start = i
curr = s[i]
size = len(s) - 1
seg_tree = [0] * (2<<(size-1).bit_length())
left = [0] * (2<<(size-1).bit_length())
right = [0] * (2<<(size-1).bit_length())
build(0, size, 0)
nodes = [Node(i, lsize[i],l[i][0]) for i in range(len(l))]
[MakeSet(node) for node in nodes]
q = []
for i in range(len(l)):
heapq.heappush(q, (l[i][0] - l[i][1], i))
while q:
val, ind = heapq.heappop(q)
node = Find(nodes[ind])
if val + node.size == 0:
if node.left > 0:
leftN = Find(nodes[node.left - 1])
rightN = node
elif node.right < len(l) - 1:
leftN = node
rightN = Find(nodes[node.right + 1])
else:
leftN = node
rightN = node
lBaseInd = leftN.main #need to move to right later
rBaseInd = rightN.main
lDel = query(0, 0, lBaseInd)
rDel = query(0, 0, rBaseInd)
lAct = lBaseInd - lDel + leftN.size
rAct = rBaseInd - rDel
lAct = min(lAct, rAct) #Fix case where left == right
size -= (rAct - lAct + 1)
out.append((str(lAct + 1) + ' ' + str(rAct + 1)))
#Update segtree
update(lBaseInd, rAct - lAct, 0)
update(rBaseInd, 1, 0)
#Update other
for node in set((leftN, rightN)):
node.size -= 1
for node in set((leftN, rightN)):
node = Find(node)
if node.size == 0:
if node.left > 0:
Union(node, nodes[node.left - 1])
elif node.right < len(l) - 1:
Union(node, nodes[node.right + 1])
else:
heapq.heappush(q, (-node.size, node.label))
#print()
#for node in nodes:
# print(node.label, Find(node).label, node.size)
if size:
out.append('1 '+str(size))
outTotal.append(str(len(out)))
outTotal.append('\n'.join(out))
print('\n'.join(outTotal))
``` | instruction | 0 | 58,179 | 0 | 116,358 |
No | output | 1 | 58,179 | 0 | 116,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon likes strings. Today he created a game about strings:
String s_1, s_2, …, s_n is beautiful if and only if for each 1 ≤ i < n, s_i ≠ s_{i+1}.
Initially, Dreamoon has a string a. In each step Dreamoon can choose a beautiful substring of a and remove it. Then he should concatenate the remaining characters (in the same order).
Dreamoon wants to use the smallest number of steps to make a empty. Please help Dreamoon, and print any sequence of the smallest number of steps to make a empty.
Input
The first line contains an integer t (1 ≤ t ≤ 200 000), denoting the number of test cases in the input.
For each test case, there's one line with a non-empty string of lowercase Latin letters a.
The total sum of lengths of strings in all test cases is at most 200 000.
Output
For each test case, in the first line, you should print m: the smallest number of steps to make a empty. Each of the following m lines should contain two integers l_i, r_i (1 ≤ l_i ≤ r_i ≤ |a|), denoting, that the i-th step is removing the characters from index l_i to r_i in the current string. (indices are numbered starting from 1).
Note that after the deletion of the substring, indices of remaining characters may change, and r_i should be at most the current length of a.
If there are several possible solutions, you can print any.
Example
Input
4
aabbcc
aaabbb
aaa
abacad
Output
3
3 3
2 4
1 2
3
3 4
2 3
1 2
3
1 1
1 1
1 1
1
1 6
Submitted Solution:
```
t = int(input())
if t == 4:
print("3\n3 3\n2 4\n1 2\n3\n3 4\n2 3\n1 2\n3\n1 1\n1 1\n1 1\n1\n1 6")
else:
for i in range(104):
input()
print(input())
``` | instruction | 0 | 58,180 | 0 | 116,360 |
No | output | 1 | 58,180 | 0 | 116,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon likes strings. Today he created a game about strings:
String s_1, s_2, …, s_n is beautiful if and only if for each 1 ≤ i < n, s_i ≠ s_{i+1}.
Initially, Dreamoon has a string a. In each step Dreamoon can choose a beautiful substring of a and remove it. Then he should concatenate the remaining characters (in the same order).
Dreamoon wants to use the smallest number of steps to make a empty. Please help Dreamoon, and print any sequence of the smallest number of steps to make a empty.
Input
The first line contains an integer t (1 ≤ t ≤ 200 000), denoting the number of test cases in the input.
For each test case, there's one line with a non-empty string of lowercase Latin letters a.
The total sum of lengths of strings in all test cases is at most 200 000.
Output
For each test case, in the first line, you should print m: the smallest number of steps to make a empty. Each of the following m lines should contain two integers l_i, r_i (1 ≤ l_i ≤ r_i ≤ |a|), denoting, that the i-th step is removing the characters from index l_i to r_i in the current string. (indices are numbered starting from 1).
Note that after the deletion of the substring, indices of remaining characters may change, and r_i should be at most the current length of a.
If there are several possible solutions, you can print any.
Example
Input
4
aabbcc
aaabbb
aaa
abacad
Output
3
3 3
2 4
1 2
3
3 4
2 3
1 2
3
1 1
1 1
1 1
1
1 6
Submitted Solution:
```
def build(lo, hi, seg):
left[seg] = lo
right[seg] = hi
if lo == hi - 1:
return
else:
mid = (lo + hi)//2
build(lo, mid, 2 * seg + 1)
build(mid, hi, 2 * seg + 2)
seg_tree[seg] = seg_tree[2 * seg + 1] + seg_tree[2 * seg + 2]
def update(i, upd, seg):
if left[seg] <= i < right[seg]:
if left[seg] == right[seg] - 1:
seg_tree[seg] += upd
else:
update(i, upd, 2 * seg + 1)
update(i, upd, 2 * seg + 2)
seg_tree[seg] += upd
def query(seg, i, j):
if i >= right[seg] or j <= left[seg]:
return 0
elif left[seg] >= i and right[seg] <= j:
return seg_tree[seg]
else:
return query(2 * seg + 1, i, j) + query(2 * seg + 2, i, j)
def MakeSet(x):
x.parent = x
x.rank = 0
def Union(x, y):
xRoot = Find(x)
yRoot = Find(y)
leftNew = min(xRoot.left, yRoot.left)
rightNew = max(xRoot.right, yRoot.right)
sizeNew = xRoot.size + yRoot.size
mainNew = y.main
charNew = y.char
if xRoot.rank > yRoot.rank:
yRoot.parent = xRoot
elif xRoot.rank < yRoot.rank:
xRoot.parent = yRoot
elif xRoot != yRoot:
yRoot.parent = xRoot
xRoot.rank = xRoot.rank + 1
Find(x).left = leftNew
Find(x).right = rightNew
Find(x).size = sizeNew
Find(x).main = mainNew
Find(x).char = charNew
def Find(x):
if x.parent == x:
return x
else:
x.parent = Find(x.parent)
return x.parent
class Node:
def __init__ (self, label, size,main,char):
self.label = label
self.left = label
self.right = label
self.size = size
self.main = main
self.char = char
def __str__(self):
return self.label
import sys
input = sys.stdin.readline
import heapq
outTotal = []
t = int(input())
for _ in range(t):
out = []
s = input().strip() + '0'
l = []
lsize = []
start = 0
curr = s[0]
for i in range(1, len(s)):
if s[i] != curr:
if i > start + 1:
l.append((start, i - 1,curr))
lsize.append(i - 1 - start)
start = i
curr = s[i]
size = len(s) - 1
seg_tree = [0] * (2<<(size-1).bit_length())
left = [0] * (2<<(size-1).bit_length())
right = [0] * (2<<(size-1).bit_length())
build(0, size, 0)
nodes = [Node(i, lsize[i],l[i][0], l[i][2]) for i in range(len(l))]
[MakeSet(node) for node in nodes]
q = []
for i in range(len(l)):
heapq.heappush(q, (l[i][0] - l[i][1], i))
while q:
val, ind = heapq.heappop(q)
node = Find(nodes[ind])
if val + node.size == 0:
if node.left > 0:
leftN = Find(nodes[node.left - 1])
rightN = node
elif node.right < len(l) - 1:
leftN = node
rightN = Find(nodes[node.right + 1])
else:
leftN = node
rightN = node
lBaseInd = leftN.main #need to move to right later
rBaseInd = rightN.main
lDel = query(0, 0, lBaseInd)
rDel = query(0, 0, rBaseInd)
lAct = lBaseInd - lDel + leftN.size
rAct = rBaseInd - rDel
lAct = min(lAct, rAct) #Fix case where left == right
size -= (rAct - lAct + 1)
out.append((str(lAct + 1) + ' ' + str(rAct + 1)))
#Update segtree
update(lBaseInd, rAct - lAct, 0)
update(rBaseInd, 1, 0)
#Update other
for node in set((leftN, rightN)):
node.size -= 1
leftN = Find(leftN)
rightN = Find(rightN)
if leftN.char == rightN.char and leftN != rightN:
Union(rightN, leftN)
node = Find(leftN)
node.size += 1
heapq.heappush(q, (-node.size, node.label))
for node in set((leftN, rightN)):
node = Find(node)
if node.size == 0:
if node.left > 0:
Union(node, nodes[node.left - 1])
elif node.right < len(l) - 1:
Union(node, nodes[node.right + 1])
else:
heapq.heappush(q, (-node.size, node.label))
#print()
#for node in nodes:
# print(node.label, Find(node).label, node.size)
if size:
out.append('1 '+str(size))
outTotal.append(str(len(out)))
outTotal.append('\n'.join(out))
print('\n'.join(outTotal))
``` | instruction | 0 | 58,181 | 0 | 116,362 |
No | output | 1 | 58,181 | 0 | 116,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant has found a ragged old black-and-white string s on the attic.
The characters of string s are numbered from the left to the right from 1 to |s|, where |s| is the length of the string. Let's denote the i-th character of string s as si. As the string is black-and-white, each character of the string is either letter "B", or letter "W". Unfortunately, the string is very old and some characters are damaged. The damaged positions are denoted as "X".
The Little Elephant in determined to restore the string and hang it on the wall. For that he needs to replace each character "X" by a "B" or a "W". The string must look good on the wall, so it must be beautiful. The Little Elephant considers a string beautiful if it has two non-intersecting substrings of a given length k, such that the left one fully consists of characters "B", and the right one fully consists of characters "W". More formally, there are four integers a, b, c, d (1 ≤ a ≤ b < c ≤ d ≤ |s|; b - a + 1 = d - c + 1 = k) such that si = "B" (a ≤ i ≤ b) and sj = "W" (c ≤ j ≤ d).
Help the Little Elephant find the number of different beautiful strings he can obtain from string s. Two strings are considered different if there is such position, where the character in the first string differs from the corresponding character in the second string. If this string doesn't contain characters «X» and it is already beautiful — the answer is 1.
As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Input
The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 106). The second line contains string s. String s has length n and only consists of characters "W", "B" and "X".
Output
On a single line print an integer — the answer to the problem modulo 1000000007 (109 + 7).
Examples
Input
3 2
XXX
Output
0
Input
4 2
XXXX
Output
1
Input
10 2
XXBXXWXXXX
Output
166 | instruction | 0 | 58,308 | 0 | 116,616 |
Tags: dp
Correct Solution:
```
Mod=1000000007
n,k=map(int,input().split(' '))
s=' '+input()
f,fs,g,gs,w=[0]*1000005,[0]*1000005,[0]*1000005,[0]*1000005,[0]*1000005
qx,qw,qb=[0]*1000005,[0]*1000005,[0]*1000005
q=0
f[0]=fs[0]=1
for i in range(1,n+1):
lg=(i-k if i-k>=q else q)
if s[i]!='B':
f[i]=fs[i-1]-fs[lg-1]+Mod
f[i]-=(Mod if f[i]>=Mod else 0)
else:
f[i]=0
fs[i]=fs[i-1]+f[i]
fs[i]-=(Mod if fs[i]>=Mod else 0)
if s[i]=='W':
q=i;
g[n+1]=gs[n+1]=1
q=n+1
for i in range(n,0,-1):
rg=(i+k if i+k<=q else q)
if s[i]!='W':
g[i]=gs[i+1]-gs[rg+1]+Mod
g[i]-=(Mod if g[i]>=Mod else 0)
else:
g[i]=0
gs[i]=gs[i+1]+g[i]
gs[i]-=(Mod if gs[i]>=Mod else 0)
if s[i]=='B':
q=i;
for i in range(1,n+1):
qx[i],qb[i],qw[i]=qx[i-1]+(s[i]=='X'),qb[i-1]+(s[i]=='B'),qw[i-1]+(s[i]=='W')
for i in range(n,0,-1):
w[i]=w[i+1]
if s[i]=='X':
w[i]*=2
w[i]-=(Mod if w[i]>=Mod else 0)
if i+k-1<=n:
if qb[i+k-1]-qb[i-1]==0:
w[i]+=g[i+k]
w[i]-=(Mod if w[i]>=Mod else 0)
ans=0
for i in range(k,n+1):
if qw[i]-qw[i-k]==0:
ans=(ans+f[i-k]*w[i+1])%Mod
print(ans)
``` | output | 1 | 58,308 | 0 | 116,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,315 | 0 | 116,630 |
Tags: brute force, strings
Correct Solution:
```
I=input
s=' '.join(I()for _ in '0'*int(I()))
R=list(map(chr,range(122,96,-1)))
for i in R:
for j in R:
if s.count(i+j)==0:r=i+j
for i in R:
if s.count(i)==0:r=i
print(r)
``` | output | 1 | 58,315 | 0 | 116,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,316 | 0 | 116,632 |
Tags: brute force, strings
Correct Solution:
```
n=int(input())
ch=""
while(n):
sh=input()
ch+=' ' +sh
n-=1
l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
t=False
for x in l:
if x not in ch:
t=True
print(x)
break
if not t:
for i in range(len(l)):
if not t:
for j in range(len(l)):
s=l[i]+l[j]
if s not in ch :
print (s)
t=True
break
``` | output | 1 | 58,316 | 0 | 116,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,317 | 0 | 116,634 |
Tags: brute force, strings
Correct Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int,minp().split())
all = set()
def subs(s):
for i in range(len(s)):
for j in range(i+1,len(s)+1):
all.add(s[i:j])
n = mint()
for i in range(n):
subs(minp())
def rec(s,n):
if n == 0:
if s not in all:
print(s)
exit(0)
return
for i in range(ord('a'),ord('z')+1):
rec(s+chr(i),n-1)
for i in range(1,100):
rec('',i)
``` | output | 1 | 58,317 | 0 | 116,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,318 | 0 | 116,636 |
Tags: brute force, strings
Correct Solution:
```
n = int(input())
arr = []
for i in range(n):
arr += [input()]
s = '+'.join(arr)
q = 'abcdefghijklmnopqrstuvwxyz'
for i in q:
if i not in s:
print(i)
break
else:
for i in q:
for j in q:
if i+j not in s:
print(i+j)
exit()
``` | output | 1 | 58,318 | 0 | 116,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,319 | 0 | 116,638 |
Tags: brute force, strings
Correct Solution:
```
alpha = "abcdefghijklmnopqrstuvwxyz"
n = int(input())
s = ""
for _ in range(n):
s += input() + " "
for x in alpha:
if x not in s:
print(x)
break
else:
for i in alpha:
for j in alpha:
if i+j not in s:
print(i+j)
quit()
``` | output | 1 | 58,319 | 0 | 116,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,320 | 0 | 116,640 |
Tags: brute force, strings
Correct Solution:
```
import re
import itertools
from collections import Counter
class Task:
strings = []
answer = ""
def getData(self):
numberOfStrings = int(input())
for i in range(0, numberOfStrings):
self.strings += [input()]
def solve(self):
badStrings = set()
for current in self.strings:
for left in range(0, len(current)):
for right in range(left + 1, len(current) + 1):
badStrings.add(current[left : right])
alphabet = []
for character in range(ord('a'), ord('z') + 1):
alphabet += [chr(character)]
for answerLength in range(1, 21 + 1):
for p in itertools.product(alphabet, repeat = answerLength):
string = re.sub("[^\w]", "", str(p));
if string not in badStrings:
self.answer = string
return
def printAnswer(self):
print(self.answer)
task = Task();
task.getData();
task.solve();
task.printAnswer();
``` | output | 1 | 58,320 | 0 | 116,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,321 | 0 | 116,642 |
Tags: brute force, strings
Correct Solution:
```
n = int(input())
t = ' '.join(input() for i in range(n))
ans = 0
for i in 'abcdefghijklmnopqrstuvwxyz':
if not i in t:
ans = i
break
if not ans:
for i in 'abcdefghijklmnopqrstuvwxyz':
for j in 'abcdefghijklmnopqrstuvwxyz':
if not i + j in t:
ans = i + j
break
if ans: break
print(ans)
``` | output | 1 | 58,321 | 0 | 116,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title. | instruction | 0 | 58,322 | 0 | 116,644 |
Tags: brute force, strings
Correct Solution:
```
import string
s=' '.join(input()for i in range(int(input())))
alph= string.ascii_lowercase
for i in alph:
if i not in s:print(i);exit()
for i in alph:
for j in alph:
if i+j not in s:print(i+j);exit()
``` | output | 1 | 58,322 | 0 | 116,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
n=int(input())
l=[]
for i in range(n):
l+=[input()]
s="+".join(l)
q="abcdefghijklmnopqrstuvwxyz"
for i in q:
if i not in s:
print(i)
break
else:
for i in q:
for j in q:
if i+j not in s:
print(i+j)
exit()
# Made By Mostafa_Khaled
``` | instruction | 0 | 58,323 | 0 | 116,646 |
Yes | output | 1 | 58,323 | 0 | 116,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
from itertools import *
alphabet="abcdefghijklmnopqrstuvwxyz";
def newProblem(names):
for i in range(1,3):
for s in product(alphabet, repeat=i):
st=""
for c in s:
st+=c
if names.find(st)==-1:
return st
n=int(input())
names=""
for i in range(0,n):
name= input()
names+=name+ " "
print(newProblem(names))
``` | instruction | 0 | 58,324 | 0 | 116,648 |
Yes | output | 1 | 58,324 | 0 | 116,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
#!/usr/bin/python3
n = int(input())
lst = [input() for _ in range(n)]
alf = [chr(ord('a') + _) for _ in range(26)]
ans = None
for a in alf:
if not [1 for w in lst if w.find(a) != -1]:
ans = a
break
if ans is not None:
print(ans)
else:
for a in alf:
for b in alf:
t = a + b
if not [1 for w in lst if w.find(t) != -1]:
ans = t
break
if ans is not None:
print(ans)
break
``` | instruction | 0 | 58,325 | 0 | 116,650 |
Yes | output | 1 | 58,325 | 0 | 116,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
n=int(input())
strings=[]
all=[]
alps=[]
for i in range(26):
alps.append(chr(ord('a')+i))
all.append(alps[i])
for i in range(26):
for j in range(i,26):
all.append(alps[i]+alps[j])
for i in range(n):
s=input()
for j in range(len(s)):
for k in range(j,len(s)):
strings.append(s[j:k+1])
ans=''
#print(strings)
for i in all:
f=1
for j in strings:
if i==j:
f=0
break
if f==1:
print(i)
break
``` | instruction | 0 | 58,326 | 0 | 116,652 |
Yes | output | 1 | 58,326 | 0 | 116,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
n=int(input())
strings=[]
all=[]
alps=[]
for i in range(26):
alps.append(chr(ord('a')+i))
all.append(alps[i])
for i in range(26):
for j in range(i,26):
all.append(alps[i]+alps[j])
for i in range(n):
s=input()
for j in range(len(s)):
for k in range(j+1,len(s)):
strings.append(s[j:k])
ans=''
for i in all:
f=1
for j in strings:
if i==j:
f=0
break
if f==1:
print(i)
break
``` | instruction | 0 | 58,327 | 0 | 116,654 |
No | output | 1 | 58,327 | 0 | 116,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
n = int(input())
mas = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
set_one, set_two, set_let, set_st = set(), set(), set(mas), set()
for i in range(n):
p = input()
for j in range(len(p)):
set_one.add(p[j])
for j in range(len(p)-1):
set_two.add(p[i:i+2])
for i in mas:
for j in mas:
set_st.add(i+j)
set_one = set_let - set_one
if len(set_one) != 0:
set_one = sorted(set_one)
print(set_one[0])
else:
set_two = set_st - set_two
set_two = sorted(set_two)
print(set_two[0])
``` | instruction | 0 | 58,328 | 0 | 116,656 |
No | output | 1 | 58,328 | 0 | 116,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
def n(n,l):
a=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
s=set([])
for i in range (n) :
s=s|set(l[i])
for i in range (n):
if s!=set(a):
for j in range(len(a)):
if (a[j] in s)==False:
return (a[j])
else :
for j in(a) :
for k in(a) :
if (j+k in l[i])==False:
return (j+k)
``` | instruction | 0 | 58,329 | 0 | 116,658 |
No | output | 1 | 58,329 | 0 | 116,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.
You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.
A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
Input
The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
Output
Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.
Examples
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.
In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
Submitted Solution:
```
import string
digs = string.digits + string.ascii_letters
from bisect import *
gamma=[]
def int2base(x, base):
if x < 0:
sign = -1
elif x == 0:
return digs[0]
else:
sign = 1
x *= sign
digits = []
while x:
digits.append(digs[int(x % base)])
x = int(x / base)
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)
a=int(input())
ans=[]
for i in range(a):
s=input()
for i in range(len(s)):
for j in range(i,len(s)):
t=s[i:j+1]
total=0
if(len(t)>4):
continue;
else:
for m in range(len(t)):
total+=pow(26,len(t)-m-1)*(ord(t[m])-ord('a')+1)
gamma.append(total)
gamma=list(set(gamma))
gamma.sort()
if(1 not in gamma):
print('a')
else:
for j in range(1,len(gamma)):
if(gamma[j]!=j+1):
val=gamma[j-1]+1
timp=''
while(val):
rem=val%26
timp+=chr(97+rem-1)
val=val//26
print(timp[::-1])
exit()
val=gamma[-1]+1
timp=''
while(val):
rem=val%26
timp+=chr(97+rem-1)
val=val//26
print(timp[::-1])
``` | instruction | 0 | 58,330 | 0 | 116,660 |
No | output | 1 | 58,330 | 0 | 116,661 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,377 | 0 | 116,754 |
Tags: brute force, implementation, strings
Correct Solution:
```
s, r = input(), int(input())
def compare(a, b):
return a[:len(b)] == b
def f(s, n, N):
if not s or n < 2*N:
return N
for i in range(n//2, 0, -1):
#print(s, n, i, N)
if compare(s[:i], s[i:2 * i]):
return f(s[1:], n-1, max(i, N))
return f(s[1:], n-1, N)
if len(s) < r:
print((len(s) + r) // 2 * 2)
else:
print(2*f(s, len(s) + r, 0))
``` | output | 1 | 58,377 | 0 | 116,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,378 | 0 | 116,756 |
Tags: brute force, implementation, strings
Correct Solution:
```
# Author : raj1307 - Raj Singh
# Date : 03.01.2020
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(100000000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import ceil,floor,log,sqrt,factorial
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *,threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[1]
def sort2(l):return sorted(l, key=getKey)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def main():
#for _ in range(ii()):
s=si()
k=ii()
n=len(s)
for i in range(k):
s+='?'
mx=0
for i in range(1,n//2+1):
for j in range(n):
if j+2*i-1<=n:
if s[j:j+i]==s[j+i:j+2*i]:
mx=2*i
break
l=len(s)
mx=max((k//2)*2,mx)
for i in range(n):
for j in range(l//2):
if s[i+j]==s[i+j+l//2]:
continue
elif s[i+j]!=s[i+j+l//2] and s[i+j+l//2]=='?':
mx=max(mx,(l//2)*2)
#print(i,j,mx)
break
else:
break
l-=1
print(mx)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#dmain()
# Comment Read()
``` | output | 1 | 58,378 | 0 | 116,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,379 | 0 | 116,758 |
Tags: brute force, implementation, strings
Correct Solution:
```
a=input()
b=int(input())
j=0
l=0
for start in range(len(a)+b):
for end in range(start+1,len(a)+b):
if ((end-start)+1)%2==0:
middle=(start+end)//2+1
k=0
for traverse in range(start,len(a)):
if k+middle>=len(a) or k+middle>end:
j=end-start+1
if j>l:
l=j
elif k+middle<len(a) and a[traverse]!=a[middle+k]:
break
elif k+middle<len(a) and a[traverse]==a[middle+k]:
k+=1
print(l)
``` | output | 1 | 58,379 | 0 | 116,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,380 | 0 | 116,760 |
Tags: brute force, implementation, strings
Correct Solution:
```
s = input()
k = int(input())
s = s + '?'*k
for i in range(len(s)//2 + 1):
a=[0]*len(s)
for j in range(len(s)-i):
if (s[j] == s[j+i])or (s[j+i] == '?'):
a[j] = 1
c = 0
mx = 0
for j in a:
if j == 1:
c+=1
else:
mx = max(mx,c)
c = 0
if mx >= i:
ans = i*2
print(ans)
``` | output | 1 | 58,380 | 0 | 116,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,381 | 0 | 116,762 |
Tags: brute force, implementation, strings
Correct Solution:
```
def tandem(s,start,l,max_len,n):
if start >= n:
if max_len-start >= 2*l:
return True
return False
if max_len-start < 2*l:
return False
for i in range(start,start+l):
si = s[start]
if i+l > max_len:
return False
if i+l < len(s) and s[i] != s[i+l]:
return False
return True
def found(s,l,max_len):
if l == 0:
return True
n = len(s)
l = l//2
for i in range(1,n):
start = i
if tandem(s,start,l,max_len,n):
return True
#print(s,l,start)
while len(s) != n:
s.pop()
return False
def solve(s,k):
max_len = 2*((len(s)+k)//2)
s1 = [0]
s1.extend(s)
s = s1[:]
while True:
if found(s,max_len,len(s)+k):
print(max_len)
return
max_len -= 2
def main():
s = list(input())
k = int(input())
solve(s,k)
main()
``` | output | 1 | 58,381 | 0 | 116,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,382 | 0 | 116,764 |
Tags: brute force, implementation, strings
Correct Solution:
```
s = input()
k = int(input())
s += "?"*k
for i in range(len(s)-len(s)%2, 0, -2):
for j in range(len(s)-i+1):
if all(s[x+i//2] in {"?", s[x]} for x in range(j, j+i//2)):
print(i)
exit(0)
``` | output | 1 | 58,382 | 0 | 116,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,383 | 0 | 116,766 |
Tags: brute force, implementation, strings
Correct Solution:
```
s = input()
k = int(input())
ans = 0
for l in range(1,(len(s)+k)//2+1):
cur = s+'a'*k
cur = ''.join(cur[i] if i < max(l,len(s)) else cur[i-l] for i in range(len(s)+k))
check = ''.join('01'[cur[i] == cur[i+l]] for i in range(len(s)+k-l))
if '1'*l in check:
ans = max(ans,l)
print(2*ans)
``` | output | 1 | 58,383 | 0 | 116,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | instruction | 0 | 58,384 | 0 | 116,768 |
Tags: brute force, implementation, strings
Correct Solution:
```
s=input()
k=int(input())
s+='?'*k
n=len(s)
best=0
for i in range(n):
for j in range(i+1,n):
if((j-i+1)%2!=0):
continue
x=j-i+1
valid=True
for e in range(i,j+1):
if(e+x//2>j):
break
if(s[e]=='?' or s[e+x//2]=='?' or s[e]==s[e+x//2]):
continue
valid=False
if(valid):
best=max(best,x)
print(best)
``` | output | 1 | 58,384 | 0 | 116,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
def solve():
s = input()
k = int(input())
len_s = len(s)
len_f = len_s + k
max_n = int(len_f / 2)
# print('max_n = %d' % max_n)
for i in range(max_n, 0, -1):
for j in range(len_f):
# print('%d %d', i, j)
if len_f - j >= 2*i:
ok = True
for k in range(j, j+i):
if k+i < len_s and s[k] != s[k+i]:
ok = False
if ok:
print(i*2)
return
solve()
``` | instruction | 0 | 58,385 | 0 | 116,770 |
Yes | output | 1 | 58,385 | 0 | 116,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
s = input()
k = int(input())
s += '?' * k
n = len(s)
res = 0
for i in range(n):
for j in range(i+1, n):
d = j - i + 1
if(d % 2 != 0):
continue
valid = True
for e in range(i, j+1):
if(e + d//2 > j):
break
if(s[e]=='?' or s[e + d//2] == '?' or s[e] == s[e + d//2]):
continue
valid = False
if(valid):
res = max(res, d)
print(res)
``` | instruction | 0 | 58,386 | 0 | 116,772 |
Yes | output | 1 | 58,386 | 0 | 116,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
s = input()
n = int(input())
def is_tandem(string, half):
if len(string) <= half:
return True
second = string[half:]
first = string[0 : len(second)]
return first == second
min_len = 0
if n >= len(s):
min_len = (len(s) + n) // 2 * 2
for i in range(n, len(s)+1, 2):
total_len = i + n
half = total_len // 2
sub_str = s[len(s) - i:]
if is_tandem(sub_str, half):
min_len = i + n
if min_len < len(s):
for i in range(len(s)):
for j in range(i + 2, len(s) + 1, 2):
sub_str = s[i: j]
half = (j - i) // 2
if is_tandem(sub_str, half) and len(sub_str) > min_len:
min_len = len(sub_str)
print(min_len)
``` | instruction | 0 | 58,387 | 0 | 116,774 |
Yes | output | 1 | 58,387 | 0 | 116,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
def tandem(s):
n=len(s)//2
for i in range(n):
if(s[i]!=s[i+n] and s[i]!='#' and s[i+n]!='#'):
return False
return True
def main():
s=input()
k=int(input())
s=s+"#"*k
n=len(s)
ans=0
for i in range(n):
for j in range(i+2,n+1,2):
if(tandem(s[i:j])):
ans=max(ans,j-i)
print(ans)
main()
``` | instruction | 0 | 58,388 | 0 | 116,776 |
Yes | output | 1 | 58,388 | 0 | 116,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
s=input()
k=int(input())
n=len(s)
if k>=n:
print(int(2*n))
raise SystemExit
for l in range((n+k)//2,k-1,-1):
if s[n-(l-k):n]==s[n+k-2*l:n-l]:
print(int(2*l))
raise SystemExit
``` | instruction | 0 | 58,389 | 0 | 116,778 |
No | output | 1 | 58,389 | 0 | 116,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
s=input()
k=int(input())
n=len(s)
for l in range((n+k)//2,k-1,-1):
if s[n-(l-k)+1:n+1]==s[n+k-2*l+1:n-l+1]:
print(int(2*l))
raise SystemExit
``` | instruction | 0 | 58,390 | 0 | 116,780 |
No | output | 1 | 58,390 | 0 | 116,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
# from debug import debug
# import sys
# mod = int(1e9)+7
def indexFind(s, index):
a = s[index]
store = []
for i in range(index+1, len(s)):
if a == s[i]:
store.append(i)
ans = 0
for i in store:
j = 0
v = 0
while (i+j < n) and (index+j<i) and s[index:index+j] == s[i:i+j]:
v+=1
j+=1
ans = max(ans, v)
return ans
s = input()
k = int(input())
n = len(s)
if k>=n:
print(((k+n)>>1)<<1)
else:
c = 0
i = 0
while n-1-i-1-k >= 0:
if s[n-1-i] == s[n-1-i-1-k]:
c+=1
else:
break
i+=1
answer = 0
for i in range(n):
ans = indexFind(s, i)
answer = max(answer, ans)
print(max(2*answer, 2*(c+k)))
``` | instruction | 0 | 58,391 | 0 | 116,782 |
No | output | 1 | 58,391 | 0 | 116,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
Input
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Output
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
Examples
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
Submitted Solution:
```
x=input()
y= input()
z=int(len(x))+int(y)
print(z)
``` | instruction | 0 | 58,392 | 0 | 116,784 |
No | output | 1 | 58,392 | 0 | 116,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | instruction | 0 | 58,393 | 0 | 116,786 |
Tags: greedy, strings
Correct Solution:
```
import sys
def main():
# fin = open("input.txt", "r")
fin = sys.stdin
fout = sys.stdout
L = list("abcdefghijklmnopqrstuvwxyz")
n, p = map(int, fin.readline().split())
A = list(fin.readline())
for i in range(n - 1, 1, -1):
pr = ord(A[i - 1]) - ord("a")
pp = ord(A[i - 2]) - ord("a")
cur = ord(A[i]) - ord("a") + 1
# print pr, pp, cur
while cur < p and (cur == pr or cur == pp):
cur += 1
if cur < p:
A[i] = chr(cur + ord("a"))
print("".join(A[:i]), end="")
print(chr(cur + ord("a")), end="")
for j in range(i + 1, n):
pr = ord(A[j - 1]) - ord("a")
pp = ord(A[j - 2]) - ord("a")
cur = 0
while cur < p and (cur == pr or cur == pp):
cur += 1
print(chr(cur + ord("a")), end="")
A[j] = chr(cur + ord("a"))
return
if n >= 2:
i = 1
pr = ord(A[i - 1]) - ord("a")
pp = -1
cur = ord(A[i]) - ord("a") + 1
# print pr, pp, cur
while cur < p and (cur == pr or cur == pp):
cur += 1
if cur < p:
A[i] = chr(cur + ord("a"))
print("".join(A[:i]), end="")
print(chr(cur + ord("a")), end="")
for j in range(i + 1, n):
pr = ord(A[j - 1]) - ord("a")
pp = ord(A[j - 2]) - ord("a")
cur = 0
while cur < p and (cur == pr or cur == pp):
cur += 1
print(chr(cur + ord("a")), end="")
A[j] = chr(cur + ord("a"))
return
i = 0
pr = pp = -1
cur = ord(A[i]) - ord("a") + 1
# print pr, pp, cur
while cur < p and (cur == pr or cur == pp):
cur += 1
if cur < p:
A[i] = chr(cur + ord("a"))
# print("".join(A[:i]), end="")
print(chr(cur + ord("a")), end="")
if n == 1:
return
j = 1
pr = ord(A[j - 1]) - ord("a")
pp = -1
cur = 0
while cur < p and (cur == pr or cur == pp):
cur += 1
print(chr(cur + ord("a")), end="")
A[j] = chr(cur + ord("a"))
for j in range(i + 2, n):
pr = ord(A[j - 1]) - ord("a")
pp = ord(A[j - 2]) - ord("a")
cur = 0
while cur < p and (cur == pr or cur == pp):
cur += 1
print(chr(cur + ord("a")), end="")
A[j] = chr(cur + ord("a"))
return
print("NO")
fin.close()
fout.close()
main()
``` | output | 1 | 58,393 | 0 | 116,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | instruction | 0 | 58,394 | 0 | 116,788 |
Tags: greedy, strings
Correct Solution:
```
def trans(c):
return chr(ord(c) + 1)
n, p = list(map(int, input().split()))
s = list(input())
s[n-1] = trans(s[n-1])
i = n - 1
while i >= 0 and i < n:
if ord(s[i]) >= ord('a') + p:
s[i] = 'a'
i -= 1
s[i] = trans(s[i])
elif i > 0 and s[i] == s[i-1] or i > 1 and s[i] == s[i-2]:
s[i] = trans(s[i])
else:
i += 1
else:
print("NO" if i < 0 else "".join(s))
# Made By Mostafa_Khaled
``` | output | 1 | 58,394 | 0 | 116,789 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | instruction | 0 | 58,395 | 0 | 116,790 |
Tags: greedy, strings
Correct Solution:
```
def nxt(c):
return chr(ord(c) + 1) if c < maxc else 'a'
n, p = map(int, input().split())
s = input()
if p == 2 and s != 'ab' and s != 'a':
print("NO")
else:
s = list("#%" + s)
maxc = chr(ord('a') + p - 1)
found = False
for i in range(-1, -n - 1, -1):
while s[i] < maxc:
s[i] = nxt(s[i])
while s[i] == s[i - 1] or s[i] == s[i - 2]:
if s[i] == maxc:
break
s[i] = nxt(s[i])
else:
found = True
if i < -1:
j = i + 1
s[j] = 'a'
while s[j] == s[j - 1] or s[j] == s[j - 2]:
s[j] = nxt(s[j])
for j in range(i + 2, 0):
#s[j] = nxt(s[j - 1])
s[j] = 'a'
while s[j] == s[j - 1] or s[j] == s[j - 2]:
s[j] = nxt(s[j])
break
if found:
print(*s[2:], sep="")
break
else:
print("NO")
``` | output | 1 | 58,395 | 0 | 116,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | instruction | 0 | 58,396 | 0 | 116,792 |
Tags: greedy, strings
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
# from fractions import *
# from heapq import*
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz/'
M=10**9+7
EPS=1e-6
def Ceil(a,b): return a//b+int(a%b>0)
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n,p=value()
s=list(input())[::-1]
s[0]=ALPHA[ALPHA.index(s[0])+1]
i=0
while(i>=0 and i<n):
cur=ALPHA.index(s[i])
if(cur>=p):
s[i]='a'
i+=1
if(i<n): s[i]=ALPHA[ALPHA.index(s[i])+1]
else:
if(i+1<n and s[i+1]==s[i]):
s[i]=ALPHA[ALPHA.index(s[i])+1]
elif(i+2<n and s[i+2]==s[i]):
s[i]=ALPHA[ALPHA.index(s[i])+1]
else:
i-=1
s=s[::-1]
if(s==['a']*n): print("NO")
else: print(*s,sep="")
``` | output | 1 | 58,396 | 0 | 116,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | instruction | 0 | 58,397 | 0 | 116,794 |
Tags: greedy, strings
Correct Solution:
```
def nxt(c):
return chr(ord(c) + 1) if c < maxc else 'a'
n, p = map(int, input().split())
s = input()
if p == 2 and s != 'ab' and s != 'a':
print("NO")
else:
s = list("#%" + s)
maxc = chr(ord('a') + p - 1)
found = False
for i in range(-1, -n - 1, -1):
while s[i] < maxc:
s[i] = nxt(s[i])
while s[i] == s[i - 1] or s[i] == s[i - 2]:
if s[i] == maxc:
break
s[i] = nxt(s[i])
else:
found = True
for j in range(i + 1, 0):
s[j] = 'a'
while s[j] == s[j - 1] or s[j] == s[j - 2]:
s[j] = nxt(s[j])
break
if found:
print(*s[2:], sep="")
break
else:
print("NO")
``` | output | 1 | 58,397 | 0 | 116,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
Input
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
Output
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
cba
Output
NO
Input
3 4
cba
Output
cbd
Input
4 4
abcd
Output
abda
Note
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed.
Submitted Solution:
```
import sys
def main():
# fin = open("input.txt", "r")
fin = sys.stdin
fout = sys.stdout
L = list("abcdefghijklmnopqrstuvwxyz")
n, p = map(int, fin.readline().split())
A = list(fin.readline())
for i in range(n - 1, 1, -1):
pr = ord(A[i - 1]) - ord("a")
pp = ord(A[i - 2]) - ord("a")
cur = ord(A[i]) - ord("a") + 1
# print pr, pp, cur
while cur < p and (cur == pr or cur == pp):
cur += 1
if cur < p:
print("".join(A[:i]), end="")
print(chr(cur + ord("a")), end="")
for j in range(i + 1, n):
pr = ord(A[j - 1]) - ord("a")
pp = ord(A[j - 2]) - ord("a")
cur = 0
while cur < p and (cur == pr or cur == pp):
cur += 1
print(chr(cur + ord("a")), end="")
return
print("NO")
fin.close()
fout.close()
main()
``` | instruction | 0 | 58,398 | 0 | 116,796 |
No | output | 1 | 58,398 | 0 | 116,797 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.