message
stringlengths 2
23.8k
| message_type
stringclasses 2
values | message_id
int64 0
1
| conversation_id
int64 97
109k
| cluster
float64 0
0
| __index_level_0__
int64 194
217k
|
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,221
| 0
| 212,442
|
Tags: games
Correct Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
a,b = map(int,input().split())
s = input()
n = len(s)
ls = []
if s[0] == ".":
cnt = 1
else:
cnt = 0
for i in range(1,n+1):
if i < n and s[i] == ".":
cnt += 1
else:
if cnt >= b:
ls.append(cnt)
cnt = 0
if not ls:
print("NO")
continue
ls.sort()
if a >= 2*b:
if len(ls) >= 2 or ls[0] > a+(b-1)*2 or ls[0] < a:
print("NO")
else:
print("YES")
else:
if ls[0] < a:# or ls[-1] > a+(2*b-1)*2:
print("NO")
elif len(ls) >= 2 and ls[-2] >= 2*b:
print("NO")
else:
l = len(ls)-1
t = ls[-1]
for i in range(t):
if i+a <= t:
p,q = i,t-(i+a)
if not (b<=p<a or b<=q<a or p>=2*b or q>=2*b):
x = l+(a<=p<2*b)+(a<=q<2*b)
if x%2 == 0:
print("YES")
break
else:
print("NO")
```
|
output
| 1
| 106,221
| 0
| 212,443
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,222
| 0
| 212,444
|
Tags: games
Correct Solution:
```
import sys
from itertools import groupby
readline = sys.stdin.readline
t = int(input())
for _ in range(t):
a, b = map(int, readline().split())
dots = [len(list(v))
for k, v in groupby(readline().rstrip()) if k == '.']
lose = 0
med = 0
long = 0
longest = 0
for x in dots:
if x < b:
continue
if b <= x < a:
lose = 1
elif 2*b <= x:
long += 1
if longest < x:
longest = x
elif a <= x < 2*b:
med += 1
if lose or long > 1:
print("NO")
continue
if long:
for left, right in zip(range(longest), range(longest-a, -1, -1)):
if (left < b or a <= left < 2*b) and (right < b or a <= right < 2*b):
if (((med + (1 if a <= left else 0) + (1 if a <= right else 0)) & 1) == 0):
print("YES")
break
else:
print("NO")
else:
print("YES" if med & 1 else "NO")
```
|
output
| 1
| 106,222
| 0
| 212,445
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,223
| 0
| 212,446
|
Tags: games
Correct Solution:
```
# import sys
# input = sys.stdin.readline
Q = int(input())
for _ in range(Q):
a, b = map(int, input().split())
S = input() + "X"
s = 0
x = x1 = x2 = x3 = xx = 0
for i in range(len(S)):
if S[i] == "X":
if s < b:
pass
elif a <= s < 2*b:
x += 1
elif a < 2*b and (3*a <= s < a+3*b-1 or 2*a <= s < a+2*b-1):
xx += 1
elif a < 2*b and 3*a <= s < a+4*b-1:
x3 += 1
elif a < 2*b and 2*a <= s < a+3*b-1:
x2 += 1
elif a <= s < a+2*b-1:
x1 += 1
else:
# print("a, b, s =", a, b, s)
print("NO")
break
s = 0
else:
s += 1
else:
# print("x, x1, x2, x3 =", x, x1, x2, x3)
if xx+x1+x2+x3 >= 2:
print("NO")
elif xx:
print("YES")
elif (x + x1 + x2*2 + x3*3) % 2 == 0:
print("NO")
else:
print("YES")
```
|
output
| 1
| 106,223
| 0
| 212,447
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,224
| 0
| 212,448
|
Tags: games
Correct Solution:
```
import os, sys
# zeilen = [l.strip() for l in sys.stdin.readlines()]
q = int(sys.stdin.readline().strip())
for _ in range(q):
a, b = map(int, sys.stdin.readline().strip().split())
word = sys.stdin.readline().strip()
gaps = sorted([len(gap) for gap in word.split('X') if len(gap)>= b])
if len(gaps) == 0:
print('NO')
elif gaps[0] < a:
print('NO')
elif len(gaps) > 1 and gaps[-2] >= 2*b:
print('NO')
elif gaps[-1] < 2*b: # no problematic, need only count
print('YES' if (len(gaps) % 2) else 'NO')
else: # exactly one problematic gap
p = gaps[-1]
if (len(gaps) % 2): # A tries to make this gap into zero or two
if p <= (a + 2*b - 2): # short enough for 0
print('YES')
elif p < 3*a: # we have to try two
print('NO') # not long enough
elif p > (a + 4*b - 2): # too long
print('NO')
else:
print('YES')
else: # A tries to make this gap into one
if p < 2*a: # too short
print('NO')
elif p > (a + 3*b - 2):# too long
print('NO')
else:
print('YES')
```
|
output
| 1
| 106,224
| 0
| 212,449
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,225
| 0
| 212,450
|
Tags: games
Correct Solution:
```
import sys
input = sys.stdin.readline
q=int(input())
for testcases in range(q):
a,b=map(int,input().split())
S=input().strip()
S+="X"
T=[]
NOW=0
for s in S:
if s==".":
NOW+=1
else:
if NOW!=0:
T.append(NOW)
NOW=0
count=0
noflag=0
other=[]
for t in T:
if b<=t<a:
noflag=1
break
if a<=t<2*b:
count+=1
elif t>=2*b:
other.append(t)
if len(other)>=2:
noflag=1
if noflag==1:
print("NO")
continue
if len(other)==0:
if count%2==1:
print("YES")
else:
print("NO")
continue
OTHER=other[0]
for left in range(OTHER-a+1):
right=OTHER-a-left
if left>=2*b or right>=2*b or b<=left<a or b<=right<a:
continue
count2=count
if left>=a:
count2+=1
if right>=a:
count2+=1
if count2%2==0:
print("YES")
break
else:
print("NO")
```
|
output
| 1
| 106,225
| 0
| 212,451
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,226
| 0
| 212,452
|
Tags: games
Correct Solution:
```
def sol():
a, b = map(int, input().split())
s = input().replace("X", " ")
small_segs = 0
critical_region = -1
for p in map(len, s.split()):
if p < b:
continue
if p < a and p >= b:
return False
if ((p - a) % 2 == 0 and (p >= a + 4 * b)) or ((p - a) % 2 == 1 and p >= a + 4 * b - 1):
return False
if p >= 2*b:
if critical_region != -1:
return False
critical_region = p
elif p >= a:
small_segs += 1
if critical_region == -1:
return small_segs % 2 == 1
can = [0] * 3
for l in range(critical_region+1-a):
r = critical_region - l - a
if (l >= b and l < a) or l >= 2 * b:
continue
if (r >= b and r < a) or r >= 2 * b:
continue
canl = int(l >= a)
canr = int(r >= a)
can[canl + canr] = 1
for choice, val in enumerate(can):
if val and (small_segs + choice) % 2 == 0:
return True
return False
n = int(input())
for _ in range(n):
print("YES" if sol() else "NO")
```
|
output
| 1
| 106,226
| 0
| 212,453
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,227
| 0
| 212,454
|
Tags: games
Correct Solution:
```
q = int(input())
for rwer in range(q):
a, b = map(int,input().split())
s = input()
tab = []
count = 0
for i in range(len(s)):
if s[i] == '.':
count += 1
if s[i] == 'X':
if count > 0:
tab.append(count)
count = 0
if count > 0:
tab.append(count)
#print(tab)
bat = [j for j in tab if j >= b]
tab = bat.copy()
if len(tab) == 0:
print("no")
else:
roomb = 0
for i in tab:
if i >= 2*b:
roomb += 1
if roomb > 1:
print("no")
else:
alice = 1
for i in tab:
if i < a:
alice = 0
break
if alice == 0:
print("no")
else:
if roomb == 0:
if len(tab) % 2 == 1:
print("yes")
else:
print("no")
if roomb == 1:
jednostrzaly = len(tab) - 1
duzy = max(tab)
#alice gra w duzym i co ona tam moze zrobic?
if duzy >= (a + 4*b - 1):
print("no")
else:
if jednostrzaly % 2 == 0:
#jesli alice da rade zostawic 0 albo 2 sloty to wygrala
if duzy <= a + 2 * b - 2: ########poprawka z -1
print("yes")
elif duzy >= 3 * a:
print("yes")
else:
print("no")
if jednostrzaly % 2 == 1:
#alice musi zostawic 1 slota
if duzy >= 2*a and duzy <= (a + 3*b-2):
print("yes")
else:
print("no")
```
|
output
| 1
| 106,227
| 0
| 212,455
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
|
instruction
| 0
| 106,228
| 0
| 212,456
|
Tags: games
Correct Solution:
```
def removeX(s,p):
if (p==len(s)):
return s
while s[p]!='X':
p+=1
if (p==len(s)):
return s
j=0
while s[p+j]=='X':
j+=1
if (p+j==len(s)):
break
return removeX(s[0:p+1]+s[p+j:],p+1)
def doable(lenght,a,b,parity):
for x in range((lenght-a)//2+1):
y=lenght-a-x
#print(x,y)
if ((not((x>=b) & (x<a)))&(not(x>=2*b))&(not((y>=b) & (y<a)))&(not(y>=2*b))):
e=0
#print('oui')
if((x>=a)&(x<2*b)):
e+=1
if((y>=a)&(y<2*b)):
e+=1
if((parity+e)%2==0):
return 1
return 0
for i in range(int(input())):
a,b=[int(x) for x in input().split()]
v=[0,0,0]
s=removeX(input(),0)
for y in s.split('X'):
k=len(y)
if ((k>=b) & (k<a)):
v[0]+=1
elif ((k>=a) & (k<2*b)):
v[1]+=1
elif (k>=2*b):
v[2]+=1
lenght=k
#print(v)
if ((v[0]>0) | (v[2]>1)):
print('NO')
elif (v[2]==1):
if (lenght-a>=4*b):
print('NO')
else:
if (doable(lenght,a,b,v[1]%2)==1):
print('YES')
else:
print('NO')
else:
if (v[1]%2==0):
print('NO')
else:
print('YES')
```
|
output
| 1
| 106,228
| 0
| 212,457
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
Q = int(input())
for _ in range(Q):
a, b = map(int, input().split())
S = input() + "X"
s = 0
x = x1 = x2 = x3 = xx = 0
for i in range(len(S)):
if S[i] == "X":
if s < b:
pass
elif a <= s < 2*b:
x += 1
elif a < 2*b and (3*a <= s < a+3*b-1 or 2*a <= s < a+2*b-1):
xx += 1
elif a < 2*b and 3*a <= s < a+4*b-1:
x3 += 1
elif a < 2*b and 2*a <= s < a+3*b-1:
x2 += 1
elif a <= s < a+2*b-1:
x1 += 1
else:
# print("a, b, s =", a, b, s)
print("NO")
break
s = 0
else:
s += 1
else:
# print("x, x1, x2, x3 =", x, x1, x2, x3)
if xx+x1+x2+x3 >= 2:
print("NO")
elif xx:
print("YES")
elif (x + x1 + x2*2 + x3*3) % 2 == 0:
print("NO")
else:
print("YES")
```
|
instruction
| 0
| 106,229
| 0
| 212,458
|
Yes
|
output
| 1
| 106,229
| 0
| 212,459
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
def solve(a, b, s):
l = map(lambda x: len(x), s.split('X'))
tot = 0
mx = 0
for j in l:
if j < b:
continue
if j < a:
return 'NO'
if j >= 2 * b:
if mx > 0:
return 'NO'
mx = j
tot += 1
if mx > 0:
if a < 2 * b:
one = mx >= a and mx <= a + 2 * b - 2
two = mx >= 2 * a and mx <= a + 3 * b - 2
three = mx >= 3 * a and mx <= a + 4 * b - 2
if not (one or two or three):
return 'NO'
if (one and two) or (two and three):
return 'YES'
if two:
tot += 1
if a >= 2 * b:
if not (mx >= a and mx <= a + 2 * b - 2):
return 'NO'
if tot % 2 == 1:
return 'YES'
else:
return 'NO'
q = int(input())
for i in range(q):
a, b = map(int, input().split())
s = input()
print(solve(a, b, s))
```
|
instruction
| 0
| 106,230
| 0
| 212,460
|
Yes
|
output
| 1
| 106,230
| 0
| 212,461
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
def main():
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
a, b = map(int, input().split())
S = input().rstrip('\n')
S += '!'
win = 1
cnt = 0
flg = 0
num = 0
for s in S:
if s == '.':
cnt += 1
else:
if cnt:
if b <= cnt < a:
win = 0
break
elif cnt < b:
pass
elif cnt < 2*b:
num += 1
else:
num += 1
if flg:
flg = 2
win = 0
break
else:
flg = 1
tmp = cnt
cnt = 0
if flg == 1:
if num % 2 == 0:
if a >= 2*b:
win = 0
else:
if not 2*a <= tmp <= a+3*b-2:
win = 0
else:
if tmp <= a+2*b-2:
pass
elif 3*a <= tmp <= a+4*b-2:
pass
else:
win = 0
elif flg == 0:
if num % 2 == 0:
win = 0
if win:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
```
|
instruction
| 0
| 106,231
| 0
| 212,462
|
Yes
|
output
| 1
| 106,231
| 0
| 212,463
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
q = int(input())
for i in range(q):
a, b = [int(item) for item in input().split()]
s = input().rstrip() + "#"
cnt = 0
bob_world = 0
alice_world = 0
length = 0
bob_winning = False
for ch in s:
if ch == ".":
cnt += 1
else:
if cnt < b:
pass
elif b <= cnt < a:
bob_winning = True
break
elif a <= cnt < b * 2:
alice_world += 1
elif cnt >= b * 2:
length = cnt
bob_world += 1
if bob_world >= 2:
bob_winning = True
break
cnt = 0
if bob_winning:
print("No")
else:
if length == 0:
if alice_world % 2 == 1:
print("Yes")
else:
print("No")
continue
possibility = set()
for i in range(length - a + 1):
l = i; r = length - a - i
if b <= l < a or b <= r < a:
continue
if l >= b * 2 or r >= b * 2:
continue
val = 0
if a <= l < b * 2:
val += 1
if a <= r < b * 2:
val += 1
possibility.add(val + 1)
alice_win = False
for item in possibility:
if (alice_world + item) % 2 == 1:
alice_win = True
if alice_win:
print("Yes")
else:
print("No")
```
|
instruction
| 0
| 106,232
| 0
| 212,464
|
Yes
|
output
| 1
| 106,232
| 0
| 212,465
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
import sys
input = sys.stdin.readline
q=int(input())
for testcases in range(q):
a,b=map(int,input().split())
S=input().strip()
T=[]
NOW=0
for s in S:
if s==".":
NOW+=1
else:
if NOW!=0:
T.append(NOW)
NOW=0
T.sort()
LA=[]
LB=[]
flag=0
SP=0
for t in T:
LA.append(t//a)
LB.append(t//b)
SP+=t//b-t//a
if sum(LA)%2==1 and SP<=1:
print("YES")
else:
print("NO")
```
|
instruction
| 0
| 106,233
| 0
| 212,466
|
No
|
output
| 1
| 106,233
| 0
| 212,467
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
T = int(input())
for i in range(T):
[a, b] = [int(i) for i in input().split()]
game = input()
length = 0
spaces = []
aspace = 0
bspace = 0
for g in game:
if(g=='.'):length+=1
else:
if(length>=b): spaces.append(length)
length = 0
for s in spaces:
if(s >= a):
aspace+=s//a
bspace+=int((s%a+1)//2 >= b)
elif(s >= b): bspace+=1
if(bspace>1 or aspace%3==2): print("NO")
else: print("YES")
```
|
instruction
| 0
| 106,234
| 0
| 212,468
|
No
|
output
| 1
| 106,234
| 0
| 212,469
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
def main():
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
a, b = map(int, input().split())
S = input().rstrip('\n')
S += '!'
win = 1
cnt = 0
flg = 0
num = 0
for s in S:
if s == '.':
cnt += 1
else:
if cnt:
if b <= cnt < a:
win = 0
break
elif cnt < b:
pass
elif cnt < 2*b:
num += 1
else:
num += 1
if flg:
flg = 2
win = 0
break
else:
flg = 1
tmp = cnt
cnt = 0
if flg == 1:
if num % 2 == 0:
if a >= 2*b:
win = 0
else:
if not 2*a <= tmp <= a+3*b-2:
win = 0
else:
if tmp > a+2*b-2:
win = 0
elif flg == 0:
if num % 2 == 0:
win = 0
if win:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
```
|
instruction
| 0
| 106,235
| 0
| 212,470
|
No
|
output
| 1
| 106,235
| 0
| 212,471
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
Submitted Solution:
```
Q = int(input())
for q in range(Q):
a, b = tuple(map(int, input().split()))
s = input()
ones = []
count = 0
flag = False
for i in s:
if i == "X":
if a > count >= b:
print('NO')
flag = True
break
elif count >= b:
ones.append(count)
count = 0
else:
count += 1
if count != 0:
ones.append(count)
if a > count >= b:
print('NO')
continue
if flag:
continue
if len(ones) == 0:
print('NO')
continue
count_more2b = 0
more2b = 0
for one in ones:
if one >= 2 * b:
count_more2b += 1
more2b = one
if count_more2b >= 2:
print('NO')
flag = True
break
if flag:
continue
if count_more2b == 0:
if len(ones) % 2 == 0:
print('NO')
else:
print('YES')
continue
elif more2b - a >= 2 * b:
print('NO')
continue
else:
if a > more2b - a >= b:
print('NO')
continue
if more2b - a < b:
ones.pop()
if len(ones) % 2 == 0:
print('YES')
else:
print('NO')
```
|
instruction
| 0
| 106,236
| 0
| 212,472
|
No
|
output
| 1
| 106,236
| 0
| 212,473
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,217
| 0
| 214,434
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq 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=998244353
EPS=1e-6
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()
for _ in range(Int()):
s=input()
x=Int()
n=len(s)
w=[1]*n
for i in range(n):
if(s[i]=='0'):
if(i+x<n):
w[i+x]=0
if(i-x>=0):
w[i-x]=0
ok=True
for i in range(n):
if(s[i]=='0'):
if(i+x<n and w[i+x]==1):
ok=False
if(i-x>=0 and w[i-x]==1):
ok=False
else:
if(i+x>=n or w[i+x]==0):
if(i-x<0 or w[i-x]==0):
ok=False
if(ok):
print(*w,sep="")
else:
print(-1)
```
|
output
| 1
| 107,217
| 0
| 214,435
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,218
| 0
| 214,436
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
t = int(input())
for i in range(t):
s = input()
m = len(s)
x = int(input())
ANS = [1] * m
for i in range(m):
if s[i] == "0":
if i-x >= 0:
ANS[i-x] = 0
if i+x < m:
ANS[i+x] = 0
ng = 0
for i in range(m):
one = 0
if (i-x >= 0 and ANS[i-x] == 1) or (i+x < m and ANS[i+x] == 1):
one = 1
if (one == 1 and s[i] == "0") or (one == 0 and s[i] == "1"):
ng = 1
break
if ng == 1:
print(-1)
else:
print("".join([str(i) for i in ANS]))
```
|
output
| 1
| 107,218
| 0
| 214,437
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,219
| 0
| 214,438
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
import sys;input=sys.stdin.readline
def process():
s = input().strip()
x, = map(int, input().split())
n = len(s)
t = [1] * n
for i in range(n):
if int(s[i]) == 0:
if i-x >= 0:
t[i-x] = 0
if i+x < n:
t[i+x] = 0
for i in range(n):
if int(s[i]) == 1:
r = 0
if i-x >= 0:
if t[i-x] == 1:
r = 1
if i+x < n:
if t[i+x] == 1:
r = 1
if not r:
print(-1)
return
print("".join(str(c) for c in t))
T, = map(int, input().split())
for _ in range(T):
process()
```
|
output
| 1
| 107,219
| 0
| 214,439
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,220
| 0
| 214,440
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
s = inp()
x = int(inp())
w = ['1']*len(s)
for i in range(len(s)):
if s[i] == '0':
if i - x > -1:
w[i-x] = '0'
if i + x < len(s):
w[i+x] = '0'
for i in range(len(s)):
if s[i] == '1':
if not ((i - x > -1 and w[i-x] == '1' ) or (i + x < len(s) and w[i+x] =='1')):
print(-1)
break
else:
print(''.join(map(str,w)))
```
|
output
| 1
| 107,220
| 0
| 214,441
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,221
| 0
| 214,442
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
def get():
n=len(s)
w = [1] * n
for i in range(n):
if s[i] == 0:
if i - x >= 0: w[i - x] = 0
if i + x < n: w[i + x] = 0
for i in range(n):
if s[i]:
if i-x>=0 and w[i-x]==1:continue
elif i+x<n and w[i+x]==1:continue
else:return [-1]
return w
for _ in range(int(input())):
s=list(map(int,input()))
x=int(input())
for i in get():
print(i,end='')
print()
```
|
output
| 1
| 107,221
| 0
| 214,443
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,222
| 0
| 214,444
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
def process(n, x):
l = [0 for i in range(len(n))]
for i in range(len(l)):
if i-x >= 0 and n[i-x] == 1: l[i] = 1
if i+x < len(l) and n[i+x] == 1: l[i] = 1
return l
for t in range(int(input())):
l = list(map(int, list(input())))
x = int(input())
n = [1 for i in range(len(l))]
#print(l, x)
for i in range(len(n)):
if l[i] == 0:
if i+x < len(n):
n[i+x] = 0
if i-x > -1:
n[i-x] = 0
#print(n)
if process(n, x) != l:
print(-1)
else:
print(''.join(map(str, n)))
```
|
output
| 1
| 107,222
| 0
| 214,445
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,223
| 0
| 214,446
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
def const(S):
n=len(S)
ANS=["0"]*n
for i in range(n):
if i-x>=0 and S[i-x]=="1":
ANS[i]="1"
if i+x<n and S[i+x]=="1":
ANS[i]="1"
return "".join(ANS)
t=int(input())
for tests in range(t):
S=input().strip()
x=int(input())
n=len(S)
ANS=["1"]*n
for i in range(n):
if S[i]=="0":
if i-x>=0:
ANS[i-x]="0"
if i+x<n:
ANS[i+x]="0"
A="".join(ANS)
if const(A)==S:
print(A)
else:
print(-1)
```
|
output
| 1
| 107,223
| 0
| 214,447
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
* if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1);
* if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x β€ n and w_{i+x} = 1, then s_i = 1);
* if both of the aforementioned conditions are false, then s_i is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2 β€ |s| β€ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 β€ x β€ |s| - 1).
The total length of all strings s in the input does not exceed 10^5.
Output
For each test case, print the answer on a separate line as follows:
* if no string w can produce the string s at the end of the process, print -1;
* otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
Example
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
instruction
| 0
| 107,224
| 0
| 214,448
|
Tags: 2-sat, brute force, constructive algorithms, greedy
Correct Solution:
```
res = ''
for _ in range(int(input())):
s = input()
x = int(input())
ls = len(s)
a = ['1'] * ls
for i, k in enumerate(s):
if k == '0':
if i-x>=0: a[i-x] = '0'
if i + x < ls: a[i+x] = '0'
for i, k in enumerate(s):
if k == '1':
f = 0
if i-x>=0: f |= int(a[i-x])
if i + x < ls: f |= int(a[i+x])
if not f:
res += '-1\n'
break
else: res += ''.join(a) + '\n'
print(res)
```
|
output
| 1
| 107,224
| 0
| 214,449
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,235
| 0
| 214,470
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
from itertools import accumulate, chain
BITS = 20
for _ in range(int(input())):
n, k = map(int, input().split());s = input();antis = [1 - int(si) for si in s];antis_s = ''.join(map(str, antis));antis_sums = tuple(chain((0,), accumulate(antis)));fbd = set();ans = 0
for i in range(n + 1 - k):
if k < BITS or antis_sums[i] == antis_sums[i + k - BITS]:fbd.add(int(antis_s[max(i, i + k - BITS):i + k], base=2))
while ans in fbd:ans += 1
t = bin(ans)[2:]
if len(t) > k:print("NO")
else:print("YES");print(t.rjust(k, '0'))
```
|
output
| 1
| 107,235
| 0
| 214,471
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,236
| 0
| 214,472
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
from collections import deque
T = int(input())
r = 1
maxdigit = 20
def fastfrac(a,b,M):
numb = pow(b,M-2,M)
return ((a%M)*(numb%M))%M
def getnum(k,s):
n = len(s)
dic = {}
prefixnum = max(0,k-maxdigit)
real = k - prefixnum
maxreal = (1<<real) - 1
front = 0
pres = deque()
for i in range(prefixnum):
if s[i]=='1': pres.append(i)
num = int(s[prefixnum:prefixnum+real],2)
if len(pres)==prefixnum:
dic[maxreal-num] = 1
if maxreal-num==front: front += 1
# print(pres)
for i in range(n-k):
if prefixnum>0:
if pres and pres[0]==i: pres.popleft()
if s[i+prefixnum] =='1': pres.append(i+prefixnum)
# print(pres)
num = num % ((maxreal+1)//2)
num = 2*num + int(s[i+k])
if len(pres)<prefixnum: continue
dic[maxreal-num] = 1
while front in dic:
front += 1
# print(dic)
if front>maxreal: return -1
else: return front
while r<=T:
n,k = map(int,input().split())
s = input()
ans = getnum(k,s)
if ans>=0:
print("YES")
subs = bin(ans)[2:]
subs = '0'*(k-len(subs)) + subs
print(subs)
else:
print("NO")
r += 1
```
|
output
| 1
| 107,236
| 0
| 214,473
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,237
| 0
| 214,474
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
import math
t = int(input())
for _ in range(t):
n,m = list(map(int,input().split()))
k = min(32,m)
s = input().rstrip()
D = set()
v = 2**k - 1
for i in range(m-k, len(s)-k+1):
temp = s[i:i+k]
#print(temp)
D.add(v - int(temp,2))
ok = 0
cc = s[:m].count('0')
if cc>0:
on = 1
else:
on = 0
for i in range(len(s)-m):
if cc==0:
on = 0
if s[i]=='0':
cc -= 1
if s[i+m]=='0':
cc += 1
if cc==0:
on = 0
if on==1:
print('YES')
print(m*'0')
ok = 1
else:
for i in range(v+1):
if i not in D:
ans = (bin(i))[2:]
ans = (m - len(ans)) * '0' + ans
print('YES')
print(ans)
ok = 1
break
if ok==0:
print('NO')
```
|
output
| 1
| 107,237
| 0
| 214,475
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,238
| 0
| 214,476
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.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")
"""
recursively make the set smaller
I need to remove from consideration anything with a 0 in its first K-20 digits
"""
out = []
for _ in range(int(input())):
N, K = [int(s) for s in input().split()]
S = input()
K2 = min(K,20)
start_str = -1
curr = 0
max_bit = pow(2,K2) - 1
no = set()
for i in range(N):
curr *= 2
if S[i] == '0': curr += 1
if curr > max_bit:
curr &= max_bit
start_str = i
if i >= K-1 and (i - start_str) >= (K-K2): no.add(curr)
flag = False
for i in range(pow(2,K2)):
if i not in no:
out.append("YES")
out.append('0'*(K-len(bin(i)[2:])) + bin(i)[2:])
flag = True
break
if not flag:
out.append("NO")
print('\n'.join(out))
#print(time.time()-start_time)
```
|
output
| 1
| 107,238
| 0
| 214,477
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,239
| 0
| 214,478
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
outL = []
for _ in range(t):
n, k = map(int, input().split())
s = input().strip()
bad = set()
k2 = min(k, 20)
last = -1
curr = 0
mask = (1 << k2) - 1
for i in range(n):
curr *= 2
if s[i] == '0':
curr += 1
if curr > mask:
curr &= mask
last = i
if i >= k-1 and (i - last) >= (k-k2):
bad.add(curr)
for i in range(1 << k2):
if i not in bad:
outL.append('YES')
out = bin(i)[2:]
outL.append('0'*(k-len(out))+out)
break
else:
outL.append('NO')
print('\n'.join(outL))
```
|
output
| 1
| 107,239
| 0
| 214,479
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,240
| 0
| 214,480
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
a = int(input())
for case in range(a):
n, k0 = [int(x) for x in input().split(' ')]
start_inds = set([i for i in range(n-k0+1)])
s = input()
k = min(20, k0)
if k < k0:
next_0 = []
curr_0 = n
for i in range(n-1,-1,-1):
if s[i] == '0':
curr_0 = i
next_0.append(curr_0)
next_0.reverse()
for i in range(n-k0+1):
if next_0[i] < i + k0 - 20:
start_inds.remove(i)
Z = 1 << k
avoid = set()
for i in start_inds:
r = int(s[i + k0 - k:i + k0], 2)
avoid.add(Z - 1 - r)
j = 0
while j in avoid and j < Z:
j += 1
ans = bin(j)[2:]
if j == Z:
print("NO")
else:
print("YES")
ans = '0' * (k0 - len(ans)) + ans
print(ans)
```
|
output
| 1
| 107,240
| 0
| 214,481
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,241
| 0
| 214,482
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for case in range(t):
n, k0 = [int(x) for x in input().split(' ')]
starts = set([i for i in range(n - k0 + 1)])
s = input()
k = min(20, k0)
if k < k0:
nz = []
cz = n
for i in range(n - 1, -1, -1):
if s[i] == '0':
cz = i
nz.append(cz)
nz.reverse()
for i in range(n - k0 + 1):
if nz[i] < i + k0 - 20:
starts.remove(i)
Z = 1 << k
avoid = set()
for i in starts:
r = int(s[i + k0 - k:i + k0], 2)
avoid.add(Z - 1 - r)
j = 0
while j in avoid and j < Z:
j += 1
ans = bin(j)[2:]
if j == Z:
print("NO")
else:
print("YES")
ans = '0' * (k0 - len(ans)) + ans
print(ans)
```
|
output
| 1
| 107,241
| 0
| 214,483
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
|
instruction
| 0
| 107,242
| 0
| 214,484
|
Tags: bitmasks, brute force, hashing, string suffix structures, strings, two pointers
Correct Solution:
```
# Author: yumtam
# Created at: 2020-12-30 16:53
def solve():
n, k = [int(t) for t in input().split()]
A = [int(c) for c in input()]
for i in range(n):
A[i] = 1 - A[i]
ctz = []
zeros = 0
for x in reversed(A):
if x == 0:
zeros += 1
else:
zeros = 0
ctz.append(zeros)
ctz = ctz[::-1]
S = n - k + 1
L = S.bit_length()
V = set()
for i in range(n-k+1):
if ctz[i] >= k - L:
v = 0
d = 1
pos = i+k-1
for _ in range(min(L, k)):
v += d * A[pos]
pos -= 1
d *= 2
V.add(v)
for ans in range(2**k):
if ans not in V:
print("YES")
res = bin(ans)[2:]
res = '0'*(k-len(res)) + res
print(res)
break
else:
print("NO")
import sys, os, io
input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
for _ in range(int(input())):
solve()
os.write(1, stdout.getvalue())
```
|
output
| 1
| 107,242
| 0
| 214,485
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n, k = map(int, stdin.readline().split())
maxx_digit = min(21, k)
maxx_so = 2 ** maxx_digit
arr = stdin.readline()
so = 0
to = 2 ** k
dct = {}
lientiep = 0
for i in range(k):
so = so * 2 + 1 - int(arr[i])
if so >= maxx_so:
so -= maxx_so
for j in range(k - maxx_digit):
if arr[j] == '1':
lientiep += 1
else:
lientiep = 0
dau = k - maxx_digit
if lientiep >= k - maxx_digit:
dct[so] = 1
for i in range(k, n):
so = so * 2 + 1 - int(arr[i])
if so >= maxx_so:
so -= maxx_so
if arr[dau] == '1':
lientiep += 1
else:
lientiep = 0
if lientiep >= k - maxx_digit:
dct[so] = 1
dau += 1
ans = 0
while ans in dct:
ans += 1
ans = bin(ans)[2:]
if k < len(ans):
print('NO')
else:
ans = '0' * (k - len(ans)) + ans
print('YES')
print(ans)
```
|
instruction
| 0
| 107,243
| 0
| 214,486
|
Yes
|
output
| 1
| 107,243
| 0
| 214,487
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for case in range(t):
n, k0 = [int(x) for x in input().split(' ')]
start_inds = set([i for i in range(n-k0+1)])
s = input()
k = min(20, k0)
if k < k0:
next_0 = []
curr_0 = n
for i in range(n-1,-1,-1):
if s[i] == '0':
curr_0 = i
next_0.append(curr_0)
next_0.reverse()
for i in range(n-k0+1):
if next_0[i] < i + k0 - 20:
start_inds.remove(i)
Z = 1 << k
avoid = set()
for i in start_inds:
r = int(s[i + k0 - k:i + k0], 2)
avoid.add(Z - 1 - r)
j = 0
while j in avoid and j < Z:
j += 1
ans = bin(j)[2:]
if j == Z:
print("NO")
else:
print("YES")
ans = '0' * (k0 - len(ans)) + ans
print(ans)
```
|
instruction
| 0
| 107,244
| 0
| 214,488
|
Yes
|
output
| 1
| 107,244
| 0
| 214,489
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
from itertools import accumulate, chain
BITS = 20
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
antis = [1 - int(si) for si in s]
antis_s = ''.join(map(str, antis))
antis_sums = tuple(chain((0,), accumulate(antis)))
fbd = set()
for i in range(n + 1 - k):
if k < BITS or antis_sums[i] == antis_sums[i + k - BITS]:
cur = int(antis_s[max(i, i + k - BITS):i + k], base=2)
fbd.add(cur)
ans = 0
while ans in fbd:
ans += 1
t = bin(ans)[2:]
if len(t) > k:
print("NO")
else:
print("YES")
print(t.rjust(k, '0'))
```
|
instruction
| 0
| 107,245
| 0
| 214,490
|
Yes
|
output
| 1
| 107,245
| 0
| 214,491
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
T = int(input())
ans = []
for t in range(T):
n,k = map(int,input().split())
s = list(input())
for i in range(n):
if s[i]=="0":
s[i]="1"
else:
s[i]="0"
count1 = 0
if k > 20:
count1 = s[0:k-20].count("1")
s = "".join(s)
dic = {}
for i in range(n-k+1):
if k <= 20:
dic[int(s[i:i+k],2)]=0
else:
if count1==0:
dic[int(s[i+k-20:i+k],2)]=0
count1 -= int(s[i])
count1 += int(s[i+k-20])
tmpans=10**10
for i in range(0,n+1):
if not(i in dic):
tmpans=i
break
if tmpans < pow(2,k):
ans.append("YES")
ans.append(format(tmpans,"0"+str(k)+"b"))
else:
ans.append("NO")
for i in range(len(ans)):
print(ans[i])
```
|
instruction
| 0
| 107,246
| 0
| 214,492
|
Yes
|
output
| 1
| 107,246
| 0
| 214,493
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
import math
t = int(input())
for _ in range(t):
n,m = list(map(int,input().split()))
k = min(21,m)
s = input().rstrip()
D = set()
v = 2**k - 1
for i in range(len(s)-k+1):
temp = s[i:i+k]
#print(temp)
D.add(v - int(temp,2))
ok = 0
if s[:m].count('1')==0 and m>len(s)//2:
print('YES')
print('0' * m)
ok = 1
else:
for i in range(v+1):
if i not in D:
ans = (bin(i))[2:]
ans = (m - len(ans)) * '0' + ans
print('YES')
print(ans)
ok = 1
break
if ok==0:
print('NO')
```
|
instruction
| 0
| 107,247
| 0
| 214,494
|
No
|
output
| 1
| 107,247
| 0
| 214,495
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
from sys import stdin, gettrace, stdout
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve(ob):
n, k = map(int, inputi().split())
ss = inputi()
kk = min((n - k + 1).bit_length(), k)
asize = 1<<kk
mask = asize - 1
found = bytearray(asize)
v = 0
for s in ss[0:kk-1]:
v <<=1
if s == 0x31:
v += 1
for s in ss[kk-1:n]:
v <<=1
v &= mask
if s == 0x31:
v += 1
found[v] = 1
ndx = found.rfind(0)
if ndx == -1:
ob.write(b'NO\n')
return
else:
ob.write(b'YES\n')
v = asize - ndx - 1
res = bytearray(k)
for i in range(k-1, -1, -1):
res[i] = (v&1) + 0x30
v >>= 1
ob.write(res)
ob.write(b'\n')
def main():
t = int(inputi())
ob = stdout.buffer
for _ in range(t):
solve(ob)
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 107,248
| 0
| 214,496
|
No
|
output
| 1
| 107,248
| 0
| 214,497
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
import bisect
import sys
input=sys.stdin.readline
from math import ceil
t=int(input())
while t:
n,k=map(int,input().split())
s=input().split()[0]
z=[]
for i in range(n):
if(s[i]=='0'):
z.append(i)
co=0
cz=0
l=0
r=k-1
lol=0
for i in range(k):
if(s[i]=='0'):
cz+=1
else:
co+=1
lol1=0
comp=[0]*k
if(co==k):
lol=1
if(cz==k):
lol1=1
#print(cz,co)
if(cz==1):
ind=bisect.bisect_left(z,r)
ind-=1
#print(ind,"ind")
comp[ind]=1
while r<n:
if(s[l]=='0'):
cz-=1
else:
co-=1
l+=1
r+=1
if(r==n):
break
if(s[r]=='0'):
cz+=1
else:
co+=1
if(co==k):
lol=1
if(cz==k):
lol1=1
if(cz==1):
#print(r)
ind=bisect.bisect_right(z,r)
ind-=1
#print(ind,"ind")
ind = ind-l+1
comp[ind]=1
yes=0
for i in range(k):
if(comp[i]==1):
comp[i]='0'
else:
yes=1
comp[i]='1'
if(lol==1 and yes==0):
if(lol1==0):
print("YES")
print("1"*k)
else:
print("NO")
elif(lol==1 and yes==1):
print("YES")
print("".join(comp))
else:
print("YES")
print('0'*k)
t-=1
```
|
instruction
| 0
| 107,249
| 0
| 214,498
|
No
|
output
| 1
| 107,249
| 0
| 214,499
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i β [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that x_i < y_i, and for any j (1 β€ j < i) x_j = y_j.
Input
The first line contains one integer q (1 β€ q β€ 10000) β the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
N, K = map(int, input().split())
S = list(map(int, list(input())[: -1]))
res = [0] * K
ln = min(K, 25)
s = set()
for i in range(N - K + 1):
t = S[i + K - ln: i + K]
x = 0
for j in range(ln): x |= t[j] << (ln - j - 1)
s.add(x)
for x in range((1 << ln) - 1, -1, -1):
if x in s: continue
print("YES")
bn = list(bin(x))[2: ]
bn = [0] * (ln - len(bn)) + bn
print("".join([str(int(i) ^ 1) for i in bn]).zfill(K))
break
else: print("NO")
```
|
instruction
| 0
| 107,250
| 0
| 214,500
|
No
|
output
| 1
| 107,250
| 0
| 214,501
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
|
instruction
| 0
| 107,306
| 0
| 214,612
|
Tags: greedy
Correct Solution:
```
#!/usr/bin/python3
n, k = map(int, input().split())
if n < k:
print(-1)
exit(0)
if k == 1 and n == 1:
print("a")
exit(0)
if k == 1:
print(-1)
exit(0)
ans = []
b = 0
for i in range(n):
if i + k - 2 >= n:
ans.append(chr(ord('a') + k - (n - i)))
else:
ans.append(chr(ord('a') + b))
b ^= 1
print(''.join(ans))
```
|
output
| 1
| 107,306
| 0
| 214,613
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
|
instruction
| 0
| 107,307
| 0
| 214,614
|
Tags: greedy
Correct Solution:
```
a=list(map(int,input().split()))
if(a[0]>1)&(a[1]==1):print(-1)
elif a[0]==a[1]==1:print('a')
elif a[1]>a[0]:print(-1)
else:print('ab'*((a[0]+2-a[1])//2)+'a'*((a[0]-a[1])%2)+''.join(map(lambda x:chr(ord('c')+x),range(a[1]-2))))
```
|
output
| 1
| 107,307
| 0
| 214,615
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
|
instruction
| 0
| 107,309
| 0
| 214,618
|
Tags: greedy
Correct Solution:
```
X = list(map(int, input().split()))
Temp = "".join([chr(ord("a") + i) for i in range(X[1])])
if X[1] == 1 and X[0] == 1:
print("a")
elif X[1] > X[0] or X[1] == 1:
print(-1)
else:
print("ab" * ((X[0] - X[1] + 2) // 2) + "a" * ((X[0] - X[1] + 2) % 2) + Temp[2:])
# Hope the best for Ravens
# Never give up
```
|
output
| 1
| 107,309
| 0
| 214,619
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
|
instruction
| 0
| 107,311
| 0
| 214,622
|
Tags: greedy
Correct Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
n, k = I()
c = [chr(i + 97) for i in range(k)]
if n < k or (k == 1 and n > 1):
print(-1)
exit()
if n == 1:
print(c[0])
exit()
m = n - k + 2
ans = 'ab'*(m//2) + 'a'*(m%2)
print(ans + ''.join(c[2:]))
```
|
output
| 1
| 107,311
| 0
| 214,623
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
|
instruction
| 0
| 107,313
| 0
| 214,626
|
Tags: greedy
Correct Solution:
```
import sys
import math
n, k = [int(x) for x in (sys.stdin.readline()).split()]
if(k == 1 and n == 1):
print("a")
exit()
if(k == 1 or k > n):
print(-1)
exit()
res = ['a', 'b'] * (int(n / 2))
if(n % 2 != 0):
res.append('a')
t = 2
for i in range(n - (k - 2), n):
res[i] = (chr(t + 97))
t += 1
"""t = 2
for i in range(n):
if(n - (k - 2) > i):
if(i % 2 == 0):
res.append('a')
else:
res.append('b')
else:
res.append(chr(t + 97))
t += 1"""
print("".join(res))
```
|
output
| 1
| 107,313
| 0
| 214,627
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
Submitted Solution:
```
import sys
(n,k)=map(int,sys.stdin.readline().split())
if (k==1 and n!=1) or k>n:
print(-1)
elif n==1:
print('a')
else:
tmp=n-k+2
s="ab"*(tmp//2)
if tmp%2==1:
s+="a"
for i in range(2,k):
s+=chr(ord('a')+i)
print (s)
```
|
instruction
| 0
| 107,314
| 0
| 214,628
|
Yes
|
output
| 1
| 107,314
| 0
| 214,629
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
Submitted Solution:
```
n,k=[int(x) for x in input().split()]
if n<k or (k==1 and n>=2):
print(-1)
exit()
if k==1:
print('a')
exit()
st=""
flag=1
for i in range(n-k+2):
if flag:
st+='a'
flag=0
else:
st+='b'
flag=1
num=ord('c')
for i in range(0,k-2):
st+=chr(num+i)
print(st)
```
|
instruction
| 0
| 107,315
| 0
| 214,630
|
Yes
|
output
| 1
| 107,315
| 0
| 214,631
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
Submitted Solution:
```
n, t = map(int, input().split())
alphas = "abcdefghijklmnopqrstuvwxyz"
if t > n:
print(-1)
elif n > 1 and t == 1:
print(-1)
elif n == 1 and t == 1:
print("a")
elif t == n:
print(alphas[:n])
else:
ans = ''
if (n-(t-2)) % 2 == 1:
ans += "ab"*((n-(t-2))//2)
ans += "a"
if t > 2:
ans += alphas[2:(t-2) + 2]
else:
ans += "ab"*((n-(t-2))//2)
if t > 2:
ans += alphas[2:(t-2) + 2]
print(ans)
```
|
instruction
| 0
| 107,316
| 0
| 214,632
|
Yes
|
output
| 1
| 107,316
| 0
| 214,633
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
Submitted Solution:
```
def main():
n,m = [int(v) for v in input().split()]
if m>n:
print(-1)
return
if m==1 and n>1:
print(-1)
return
p = "ab"
prefix = p*((n-(m-2))//2)
if (n-(m-2))%2!=0:
prefix+="a"
for i in range(2, 2+(m-2)):
prefix+=chr(ord('a')+i)
print(prefix[0:n])
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 107,317
| 0
| 214,634
|
Yes
|
output
| 1
| 107,317
| 0
| 214,635
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
2. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds, si β si + 1(1 β€ i < n).
3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
Input
A single line contains two positive integers n and k (1 β€ n β€ 106, 1 β€ k β€ 26) β the string's length and the number of distinct letters.
Output
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
Examples
Input
7 4
Output
ababacd
Input
4 7
Output
-1
Submitted Solution:
```
n,m=input().split(" ")
n=int(n)
m=int(m)
str=""
list=[]
for i in range(0,n):
list.append("")
list[0]="a"
if(m==n and n==1):
print("a")
if(m>n or m==1):
print("-1")
else:
d=ord("a")
i=1
while(i<n-m+2):
list[i]="b"
i=i+2
j=2
while(j<=n-m+2):
list[j]="a"
j=j+2
j=2
i=n-m+2
while(i<n):
list[i]=(chr(d+j))
j=j+1
i=i+1
c=str.join(list)
print(c)
```
|
instruction
| 0
| 107,318
| 0
| 214,636
|
No
|
output
| 1
| 107,318
| 0
| 214,637
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.