message stringlengths 2 23.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 97 109k | cluster float64 0 0 | __index_level_0__ int64 194 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jenya has recently acquired quite a useful tool — k-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length k from an arbitrary string s (its length should be at least 2·k in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut ab and de out of abcde and concatenate them into abde, but not ab and bc since they're intersecting.
It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings s and t. His question is whether it is possible to apply his scissors to string s such that the resulting concatenation contains t as a substring?
Input
The first line contains three integers n, m, k (2 ≤ m ≤ 2·k ≤ n ≤ 5·105) — length of s, length of t and the aforementioned scissors' parameter correspondingly.
The next two lines feature s and t consisting of lowercase latin letters.
Output
If there is no answer, print «No».
Otherwise print «Yes» and two integers L and R denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any.
Examples
Input
7 4 3
baabaab
aaaa
Output
Yes
1 5
Input
6 3 2
cbcbcb
bcc
Output
Yes
2 5
Input
7 5 3
aabbaaa
aaaaa
Output
No
Note
In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb.
Submitted Solution:
```
def getSubString(s, pos1, pos2, k):
return s[pos1 :pos1 + k], s[pos2: pos2 + k]
def solve(n, m, k, s, t):
for i in range(n - 2*k):
for j in range(i + k, n - 1):
s1, s2 = getSubString(s, i, j, k)
if t in s1 + s2: return i, j
return -1
n, m, k = [int(i) for i in input().split()]
s = input()
t = input()
if solve(n, m, k, s, t) == -1: print("No")
else:
print("Yes")
num1, num2 = solve(n, m, k, s, t)
print(str(num1 + 1) + " " + str(num2 + 1))
``` | instruction | 0 | 1,422 | 0 | 2,844 |
No | output | 1 | 1,422 | 0 | 2,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jenya has recently acquired quite a useful tool — k-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length k from an arbitrary string s (its length should be at least 2·k in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut ab and de out of abcde and concatenate them into abde, but not ab and bc since they're intersecting.
It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings s and t. His question is whether it is possible to apply his scissors to string s such that the resulting concatenation contains t as a substring?
Input
The first line contains three integers n, m, k (2 ≤ m ≤ 2·k ≤ n ≤ 5·105) — length of s, length of t and the aforementioned scissors' parameter correspondingly.
The next two lines feature s and t consisting of lowercase latin letters.
Output
If there is no answer, print «No».
Otherwise print «Yes» and two integers L and R denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any.
Examples
Input
7 4 3
baabaab
aaaa
Output
Yes
1 5
Input
6 3 2
cbcbcb
bcc
Output
Yes
2 5
Input
7 5 3
aabbaaa
aaaaa
Output
No
Note
In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb.
Submitted Solution:
```
n,m,k=map(int,input().split(' '))
line = input()
goal = input()
found = False
for i in range(len(line) - k + 1):
for j in range(i + 3, len(line) - k + 1):
test = line[i:i + k] + line[j:j + k]
if goal in test:
print('Yes')
print("%d %d" % (i + 1, j + 1))
found = True
break
if not found:
print('No')
``` | instruction | 0 | 1,423 | 0 | 2,846 |
No | output | 1 | 1,423 | 0 | 2,847 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,492 | 0 | 2,984 |
"Correct Solution:
```
n, q = map(int, input().split())
s = input()
a = [0, 0]
for i in range(2, n + 1):
a.append(a[i - 1] + (s[i - 2:i] == 'AC'))
for _ in range(q):
li, ri = map(int, input().split())
print(a[ri] - a[li])
``` | output | 1 | 1,492 | 0 | 2,985 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,493 | 0 | 2,986 |
"Correct Solution:
```
n,q = map(int,input().split())
s = input()
t = [0] * (n+1)
for i in range(n):
t[i+1] = t[i] + (1 if s[i:i+2] == 'AC' else 0)
#print (t)
for _ in range(q):
l,r = map(int,input().split())
print (t[r-1]-t[l-1])
``` | output | 1 | 1,493 | 0 | 2,987 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,494 | 0 | 2,988 |
"Correct Solution:
```
n,q=map(int,input().split())
s=input()
a = [0] * n
num = 0
for i in range(1, n):
if s[i]=='C' and s[i-1]=='A':
num += 1
a[i] = num
for _ in range(q):
l,r=map(int,input().split())
print(a[r-1] - a[l-1])
``` | output | 1 | 1,494 | 0 | 2,989 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,495 | 0 | 2,990 |
"Correct Solution:
```
n, q = (int(x) for x in input().split())
s = input()
t = [0]*(n+1)
for i in range(n):
t[i+1] = t[i] + (1 if s[i : i + 2] == 'AC' else 0)
for i in range(q):
l,r = (int(x) for x in input().split())
print(t[r-1]-t[l-1])
``` | output | 1 | 1,495 | 0 | 2,991 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,496 | 0 | 2,992 |
"Correct Solution:
```
n, q = map(int, input().split())
s=input()
d=[0]*n
c=0
for i in range(n-1):
if s[i:i+2]=="AC":
c+=1
d[i+1]=c
for i in range(q):
l,r= map(int, input().split())
print(d[r-1]-d[l-1])
``` | output | 1 | 1,496 | 0 | 2,993 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,497 | 0 | 2,994 |
"Correct Solution:
```
n, q = map(int, input().split())
s = input()
a = [list(map(int, input().split())) for i in range(q)]
c = [0] * n
for i in range(1,n):
c[i] = c[i-1] + (s[i-1]+s[i] == 'AC')
for x in a:
print(c[x[1]-1]-c[x[0]-1])
``` | output | 1 | 1,497 | 0 | 2,995 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,498 | 0 | 2,996 |
"Correct Solution:
```
n,q=map(int,input().split())
s=input()
l=[list(map(int,input().split())) for i in range(q)]
L=[0];ct=0
for i in range(n-1):
if s[i:i+2]=='AC':
ct+=1
L.append(ct)
for j in range(q):
print(L[l[j][1]-1]-L[l[j][0]-1])
``` | output | 1 | 1,498 | 0 | 2,997 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3 | instruction | 0 | 1,499 | 0 | 2,998 |
"Correct Solution:
```
n, q = map(int, input().split())
S = input()
t = [0] * (n + 1)
for i in range(n):
t[i + 1] = t[i] + (1 if S[i : i+2] == "AC" else 0)
for i in range(q):
l, r = map(int, input().split())
print(t[r-1] - t[l-1])
``` | output | 1 | 1,499 | 0 | 2,999 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
n,q=map(int,input().split())
s=input()
a=[0]*n
for i in range(n):
if i:
a[i]=a[i-1]+(s[i-1:i+1]=='AC')
for _ in range(q):
l,r=map(int,input().split())
print(a[r-1]-a[l-1])
``` | instruction | 0 | 1,500 | 0 | 3,000 |
Yes | output | 1 | 1,500 | 0 | 3,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
f = lambda : map(int,input().split())
n, q = f()
s = input()
ln = [0]*n
for i in range(1,n):
ln[i] = ln[i-1] + (s[i-1:i+1]=='AC')
for _ in range(q):
l, r = f()
print(ln[r-1]-ln[l-1])
``` | instruction | 0 | 1,501 | 0 | 3,002 |
Yes | output | 1 | 1,501 | 0 | 3,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
n,q=map(int,input().split())
s=input()
b=[0]*n
for i in range(1,n):
if s[i-1]+s[i]=='AC':
b[i]=b[i-1]+1
else:
b[i]=b[i-1]
for i in range(q):
l,r=map(int,input().split())
print(b[r-1]-b[l-1])
``` | instruction | 0 | 1,502 | 0 | 3,004 |
Yes | output | 1 | 1,502 | 0 | 3,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
N, Q = map(int, input().split())
S = input()
t = [0] * N
for i in range(N - 1):
t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
l, r = map(int, input().split())
print(t[r - 1] - t[l - 1])
``` | instruction | 0 | 1,503 | 0 | 3,006 |
Yes | output | 1 | 1,503 | 0 | 3,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
#入力
N ,Q = list(map(int, input().split()))
S = input()
lr_array = []
for i in range(Q):
lr_array.append(list(map(int, input().split())))
#本体
patent_var = 0
ans = 0
blank_array = [0] * (N+2)
for j in range(N):
if S[j] == "A" and S[j+1]=="C":
patent_var += 1
blank_array[j+2] = patent_var
else:
blank_array[j+2] = patent_var
for k in range(Q):
left = lr_array[k][0]
right = lr_array[k][1]
ans = blank_array[right] - blank_array[left]
print(ans)
ans = 0
``` | instruction | 0 | 1,504 | 0 | 3,008 |
No | output | 1 | 1,504 | 0 | 3,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
#encoding: utf-8
n, q = map(int, input().split())
s = input()
for i in range(q):
l, r = list(map(int, input().split()))
print(s[l - 1:r].count("AC"))
``` | instruction | 0 | 1,506 | 0 | 3,012 |
No | output | 1 | 1,506 | 0 | 3,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string S of length N consisting of `A`, `C`, `G` and `T`. Answer the following Q queries:
* Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does `AC` occurs as a substring?
Constraints
* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* S is a string of length N.
* Each character in S is `A`, `C`, `G` or `T`.
* 1 \leq l_i < r_i \leq N
Input
Input is given from Standard Input in the following format:
N Q
S
l_1 r_1
:
l_Q r_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Example
Input
8 3
ACACTACG
3 7
2 3
1 8
Output
2
0
3
Submitted Solution:
```
n, q = map(int, input().split())
s = input()
ac = [0 for i in range(n)]
for i in range(1, n - 1):
if s[i - 1] == 'A' and s[i] == 'C':
ac[i] = 1
for i in range(n - 1):
ac[i + 1] += ac[i]
ac = [0] + ac
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
print(ac[r + 1] - ac[l])
``` | instruction | 0 | 1,507 | 0 | 3,014 |
No | output | 1 | 1,507 | 0 | 3,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,885 | 0 | 3,770 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
for _ in range(int(input())):
a,b = map(int,input().split())
s = input()
n = len(s)
if a+b != n or (a%2==1 and b%2==1):
print("-1")
else:
x = y = z = 0
f = 0
l1 = ["0"]*(n//2)
l2 = ["0"]*(n//2)
for i in range(n//2):
if (s[i] == "0" and s[n-i-1]=="1") or (s[i] == "1" and s[n-i-1]=="0"):
f = 1
break
elif s[i]=="0":
x += 2
elif s[i]=="1":
l1[i] = "1"
l2[n//2-i-1] = "1"
y += 2
else:
if s[n-i-1]=="0":
x += 2
elif s[n-i-1]=="1":
y += 2
l1[i] = "1"
l2[n//2-i-1]="1"
else:
l1[i] = "2"
l2[n//2-i-1] ="2"
z += 2
if f==0:
if n%2==1:
if s[n//2]=="0":
l1 += ["0"]
x += 1
elif s[n//2]=="1":
l1 += ["1"]
y += 1
else:
if a%2 == 1:
l1 += ["0"]
x += 1
elif b%2 == 1:
l1 += ["1"]
y += 1
for i in range(n//2):
if l1[i]=="2":
if x<a:
l1[i] = "0"
l2[n//2-i-1] = "0"
x += 2
else:
l1[i] = "1"
l2[n//2-i-1] = "1"
y += 2
if x==a and y==b:
print("".join(l1),end="")
print("".join(l2))
else:
print("-1")
else:
print("-1")
``` | output | 1 | 1,885 | 0 | 3,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,886 | 0 | 3,772 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
# coding: utf-8
for ct in range(int(input())):
a, b = map(int, input().split())
n = a + b
s = list(input())
for i in range(n):
if s[i] == '?':
s[i] = s[n - i - 1]
a -= s.count('0')
b -= s.count('1')
for i in range(n // 2 + 1):
if i != n - i - 1 and s[i] == '?':
if a > 1:
s[i] = s[n - i - 1] = '0'
a -= 2
elif b > 1:
s[i] = s[n - i - 1] = '1'
b -= 2
elif s[i] == '?':
if a > 0:
s[i] = '0'
a -= 1
elif b > 0:
s[i] = '1'
b -= 1
s = ''.join(s)
print(s) if s == s[::-1] and a == b == 0 else print(-1)
``` | output | 1 | 1,886 | 0 | 3,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,887 | 0 | 3,774 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
def abpal():
a, b = map(int, input().split())
s = input()
n = len(s)
if a + b != n:
return -1
if a%2 != 0 and b%2 != 0:
return -1
if n == 1:
if a == 1 and s == '0':
return s
if b == 1 and s == '1':
return s
if s == '?':
if a == 1:
return '0'
if b == 1:
return '1'
return -1
ac = s.count('0')
bc = s.count('1')
if s.count('?') == 0:
if ac != a or bc != b:
return -1
if s == s[::-1]:
return s
else:
return -1
st = ""
for i in range(n//2):
if s[i] == '?':
if s[n-i-1] == '?':
st = st + '?'
else:
st = st + s[n-i-1]
else:
st = st + s[i]
rev = st[::-1]
if n%2 != 0:
if a%2 != 0:
st = st + '0'
else:
st = st + '1'
st = st + rev
if st != st[::-1]:
return -1
ac = st.count('0')
bc = st.count('1')
stg = ""
for i in range(n//2):
if st[i] == '?':
if ac < a:
ac+=2
stg = stg + '0'
else:
bc+=2
stg = stg + '1'
else:
stg = stg + st[i]
rev = stg[::-1]
if n%2 != 0:
if a%2 != 0:
stg = stg + '0'
else:
stg = stg + '1'
stg = stg + rev
ac = stg.count('0')
bc = stg.count('1')
if a != ac or b != bc:
return -1
if stg != stg[::-1]:
return -1
for i in range(n):
if s[i] != '?':
if s[i] != stg[i]:
return -1
return stg
n = int(input())
for i in range(n):
print(abpal())
``` | output | 1 | 1,887 | 0 | 3,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,888 | 0 | 3,776 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
'''9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
'''
def f(zeros, ones, s):
existing_ones = s.count('1')
existing_zeros = s.count('0')
needed_zeros = zeros - existing_zeros
needed_ones = ones - existing_ones
free_pairs = []
needs_one = []
needs_zero = []
for i in range(len(s) // 2):
l, r = i, len(s) - 1 - i
pair = s[l], s[r]
if pair == ('?', '?'):
free_pairs.append((l, r))
elif pair == ("?", "0"):
needs_zero.append(l)
elif pair == ('0', "?"):
needs_zero.append(r)
elif pair == ('1', "?"):
needs_one.append(r)
elif pair == ('?', '1'):
needs_one.append(l)
elif pair == ('1', '0'):
return '-1'
elif pair == ('0', '1'):
return '-1'
elif pair == ('1', '1'):
pass
elif pair == ('0', '0'):
pass
arr = list(s)
while needs_zero:
arr[needs_zero.pop()] = '0'
needed_zeros -= 1
if needed_zeros < 0:
return '-1'
while needs_one:
arr[needs_one.pop()] = '1'
needed_ones -= 1
if needed_ones < 0:
return '-1'
while free_pairs and needed_zeros >= 2:
l, r = free_pairs.pop()
arr[l] = arr[r] = '0'
needed_zeros -= 2
while free_pairs and needed_ones >= 2:
l, r = free_pairs.pop()
arr[l] = arr[r] = '1'
needed_ones -= 2
if free_pairs:
return '-1'
sum = needed_ones + needed_zeros
if sum == 0:
return ''.join(arr)
elif sum == 1:
if (len(s) & 1) and s[len(s) // 2] == '?':
arr[len(s) // 2] = '1' if needed_ones else '0'
return ''.join(arr)
return '-1'
def g(a, b, s):
result = f(a, b, s)
if result == '-1':
return '-1'
if result.count('0') == a and result.count('1') == b:
return result
else:
return '-1'
#
# assert f(0, 1, "?") == "0"
# assert f(1, 0, "?") == "0"
#
# assert f(2, 1, "???") == "010"
# assert f(1, 2, "???") == "101"
t = int(input())
output = []
for _ in range(t):
a, b = map(int, input().split())
line = input()
output.append(g(a, b, line))
print('\n'.join(output))
``` | output | 1 | 1,888 | 0 | 3,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,889 | 0 | 3,778 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
def main():
t=int(input())
allans=[]
for _ in range(t):
a,b=readIntArr()
n=a+b
arr=list(input())
possible=True
l=0
r=n-1
while l<=r:
if l==r:
if arr[l]=='0':
a-=1
elif arr[r]=='1':
b-=1
else: # skip '?'
pass
elif arr[l]!='?' and arr[r]!='?':
if arr[l]!=arr[r]:
possible=False
# print('break1') ##
break
else: # both same
if arr[l]=='0':
a-=2
else:
b-=2
elif arr[l]=='?' and arr[r]=='?': # skip for now
pass
elif arr[l]=='?':
arr[l]=arr[r]
if arr[r]=='0':
a-=2
else:
b-=2
elif arr[r]=='?':
arr[r]=arr[l]
if arr[l]=='0':
a-=2
else:
b-=2
else:
assert False
# print('l:{} r:{} a:{} b:{}'.format(l,r,a,b))
l+=1
r-=1
l=0
r=n-1
while l<=r:
if l==r:
if arr[l]=='?':
if a>0:
arr[l]='0'
a-=1
else:
arr[l]='1'
b-=1
elif arr[l]=='?' and arr[r]=='?':
if a>1:
arr[l]='0'
arr[r]='0'
a-=2
else:
arr[l]='1'
arr[r]='1'
b-=2
l+=1
r-=1
if not (a==0 and b==0):
# print('break2 a:{} b:{}'.format(a,b)) ##
possible=False
if possible:
allans.append(''.join(arr))
else:
allans.append(-1)
multiLineArrayPrint(allans)
return
import sys
# input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m])
dv=defaultVal;da=dimensionArr
if len(da)==1:return [dv for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(x,y):
print('? {} {}'.format(x,y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(ans))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
for _abc in range(1):
main()
``` | output | 1 | 1,889 | 0 | 3,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,890 | 0 | 3,780 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
t = int(input())
while t:
t -= 1
a0,b1 = map(int, input().split())
a = list(input())
n = len(a)
l = 0
r = n-1
cnt0 = 0
cnt1 = 0
flag = False
while l <= r:
if l == r:
if a[l] == '0':
cnt0+=1
if a[l] == '1':
cnt1 += 1
l += 1
continue
if a[l] == '?' or a[r] == '?':
if a[l] == a[r] == '?':
pass
elif a[l] == '?':
if a[r] == '0':
a[l] = '0'
else:
a[l] = '1'
else:
if a[l] == '0':
a[r] = '0'
else:
a[r] = '1'
if a[l] != a[r]:
flag = True
break
if a[l] == '0':
cnt0+=2
elif a[l] == '1':
cnt1 += 2
l += 1
r-= 1
if flag or cnt0 > a0 or cnt1 > b1:
print(-1)
continue
for i in range(n//2):
if a[i] != '?':
pass
elif cnt0 + 2 <= a0:
cnt0+=2
a[i] = a[n-1-i] = '0'
elif cnt1 + 2 <= b1:
cnt1+=2
a[i] = a[n-1-i] = '1'
continue
else:
flag = True
break
if n % 2 != 0:
i = n // 2
if a[i] != '?':
pass
elif cnt0 < a0:
cnt0 += 1
a[i] = '0'
elif cnt1 < b1:
cnt1 += 1
a[i] = '1'
else:
flag = True
break
if flag:
print(-1)
continue
if cnt0 == a0 and cnt1 == b1:
for elem in a:
print(elem,end='')
print()
else:
print(-1)
``` | output | 1 | 1,890 | 0 | 3,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,891 | 0 | 3,782 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
for i in range(int(input())):
a, b = map(int, input().split())
arr = list(input())
n = a+b
mid = n%2 and arr[n//2] == '?'
repeats = []
a_ = 0
b_ = 0
if n%2:
if arr[n//2] == '0':
a_ += 1
elif arr[n//2] == '1':
b_ += 1
for i in range(n//2):
if arr[i] != arr[-i-1]:
if arr[i] == '?':
arr[i] = arr[-i-1]
elif arr[-i-1] == '?':
arr[-i-1] = arr[i]
else:
print(-1)
break
elif arr[i] == '?':
repeats.append(i)
if arr[i] == '0':
a_ += 1
elif arr[i] == '1':
b_ += 1
if arr[-i-1] == '0':
a_ += 1
elif arr[-i-1] == '1':
b_ += 1
else:
if mid:
if (a-a_)%2 == (b-b_)%2 == 0:
print(-1)
continue
elif (a-a_)%2:
arr[n//2] = '0'
a_ += 1
elif (b-b_)%2:
arr[n//2] = '1'
b_ += 1
for i in repeats:
if (a-a_) >= 2:
a_ += 2
arr[i] = '0'
arr[-i-1] = '0'
elif (b-b_) >= 2:
b_ += 2
arr[i] = '1'
arr[-i-1] = '1'
if a_== a and b_ == b:
print(*arr, sep='')
else:
print(-1)
``` | output | 1 | 1,891 | 0 | 3,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010 | instruction | 0 | 1,892 | 0 | 3,784 |
Tags: constructive algorithms, implementation, strings
Correct Solution:
```
import math
rr = input
rri = lambda: int(rr())
rrm = lambda: list(map(int, rr().split()))
T = rri()
def split(word):
return [char for char in word]
def solve():
a, b = rrm()
s = split(input())
if a % 2 == 1 and b % 2 == 1:
return -1
n = a + b
if n % 2 == 1:
if a % 2:
if s[n//2] == '1':
return -1
s[n//2] = '0'
elif b % 2:
if s[n//2] == '0':
return -1
s[n//2] = '1'
ac = s.count('0')
bc = s.count('1')
if ac > a or bc > b:
return -1
aleft = a - ac
bleft = b - bc
for i in range(n//2):
temp1 = s[i]
temp2 = s[-i-1]
if temp2 != temp1:
if temp2 == '?':
s[-i-1] = temp1
elif temp1 == '?':
s[i] = temp2
else:
return -1
if s[i] == '0':
aleft -=1
else:
bleft -=1
if bleft <0 or aleft <0:
return -1
#if aleft
for i in range(n//2):
#print('in', i, aleft, bleft)
#if i == n-i-1 and s[i] == '?':
# if aleft == 1:
# s[i] = '0'
# elif bleft == 1:
# s[i] = '1'
# else:
# return -1
temp1 = s[i]
temp2 = s[-i-1]
if temp2 == temp1 == '?':
if aleft > 1:
s[-i-1] = '0'
s[i] = '0'
aleft -= 2
elif bleft > 1:
s[-i-1] = '1'
s[i] = '1'
bleft -= 2
else:
return -1
if aleft or bleft:
return -1
if '?' in s:
return -1
return ''.join(s)
for t in range(T):
print(solve())
``` | output | 1 | 1,892 | 0 | 3,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
def result(s,a,b):
x=[a,b]
for i in range(0,len(s)):
if s[i]=='?' and s[-i-1]=='?':
pass
elif s[i]=='?':
s[i]=s[-i-1]
x[int(s[i])]-=1
elif s[-i-1]=="?":
s[-i-1]=s[i]
x[int(s[i])]-=1
else:
if s[i]!=s[-1-i]:
return '-1'
else:
x[int(s[i])]-=1
if (a+b)%2==1:
if s[len(s)//2]=='?':
if x[0]%2==1:
s[len(s)//2]='0'
x[0]-=1
else:
s[len(s)//2]='1'
x[1]-=1
if x[0]<0 or x[1]<0 or x[0]%2==1:
return "-1"
for i in range(0,len(s)):
if s[i]=='?':
if x[0]>0:
s[i]='0'
s[-i-1]='0'
x[0]-=2
else:
s[i]='1'
s[-i-1]='1'
x[1]-=2
return ''.join(s)
for i in range(int(input())):
a,b=map(int,input().split())
s=list(input())
print(result(s,a,b))
``` | instruction | 0 | 1,893 | 0 | 3,786 |
Yes | output | 1 | 1,893 | 0 | 3,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import deque
def solve():
a, b = map(int, input().split())
s = list(input().rstrip())
a -= s.count("0")
b -= s.count("1")
n = len(s)
for i in range(n // 2):
if s[i] != s[-(i + 1)]:
z=s[i]+s[-(i+1)]
if "?" in z:
if s[i] == "0" or s[-(i + 1)] == "0":
s[i] = s[-(i + 1)] = "0"
a -= 1
else:
s[i] = s[-(i + 1)] = "1"
b -= 1
else:
print(-1)
return
if n % 2 and s[n // 2] == "?":
if a % 2:
s[n // 2] = "0"
a -= 1
else:
s[n // 2] = "1"
b -= 1
if a < 0 or b < 0 or a % 2 or b % 2:
print(-1)
else:
for i in range(n // 2):
if s[i] == s[-(i + 1)] and s[i] == "?":
if a:
s[i] = s[-(i + 1)] = "0"
a -= 2
else:
s[i] = s[-(i + 1)] = "1"
b -= 2
print("".join(s))
def main():
for _ in range(int(input())):
solve()
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 1,894 | 0 | 3,788 |
Yes | output | 1 | 1,894 | 0 | 3,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
for _ in range(int(input())):
flag=True
a,b=map(int,input().split())
s=[i for i in input()]
for i in range(a+b):
if s[i]=='?':
ch=s[-i-1]
if ch!='?':
s[i]=ch
a-=s.count('0')
b-=s.count('1')
if a<0 or b<0:
flag=False
n=a+b
if n%2==0:
if a%2==1:
flag=False
if flag:
if n%2==0:
new=[0 for i in range(a//2)]+[1 for i in range(b)]+[0 for i in range(a//2)]
else:
if a%2==0:
new=[0 for i in range(a//2)]+[1 for i in range(b)]+[0 for i in range(a//2)]
else:
new=[1 for i in range(b//2)]+[0 for i in range(a)]+[1 for i in range(b//2)]
if new:
for i in range(len(s)):
if s[i]=='?':
s[i]=new.pop()
if s!=s[::-1]:
print(-1)
else:
print(''.join(str(i) for i in s))
else:
print(-1)
``` | instruction | 0 | 1,895 | 0 | 3,790 |
Yes | output | 1 | 1,895 | 0 | 3,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
I=input
for _ in[0]*int(I()):
(c,a),(d,b)=sorted(zip('01',map(int,I().split())),key=lambda x:x[1]%2);s=[*I()];f=s.count;i=0
for x in s:
i-=1
if'1'<s[i]:s[i]=x
j=f('?');i=b-f(d);k=-1
while j>0:k=s.index('?',k+1);s[k]=s[~k]=(d,c)[j>i];j-=2
print((''.join(s),-1)[(f(d),s)!=(b,s[::-1])])
``` | instruction | 0 | 1,896 | 0 | 3,792 |
Yes | output | 1 | 1,896 | 0 | 3,793 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
def arrIn():
return list(map(int,input().split()))
def mapIn():
return map(int,input().split())
def check_palindrome(s):
n=len(s)
for i in range(n):
if s[i]!=s[n-i-1]:return False
return True
for ii in range(int(input())):
a,b=mapIn()
s=input()
n=len(s)
d={"0":0,"1":0,"?":0}
for i in range(n):
d[s[i]]+=1
l=list(s)
x=n//2
for i in range(x):
if l[i] == "?" and l[n - i - 1] != "?":
l[i] = l[n - 1 - i]
d[l[i]] += 1
d["?"] -= 1
elif l[i] != "?" and l[n - i - 1] == "?":
l[n - i - 1] = l[i]
d[l[i]] += 1
d["?"] -= 1
if check_palindrome(l)==False:
print(-1)
continue
flag=False
if n%2 and l[n//2]=="?":
flag=True
final =True
x = a - d["0"]
y = b - d["1"]
if flag==False:
if x%2 or y%2:
final=False
else:
if x%2==0 and y%2==0:
final=False
if x%2==1 and y%2==1:
final=False
if final==False:
# print("x1")
print(-1)
else:
tx=a-d["0"]
ty=b-d["1"]
# print(l)
# print(d)
# print(tx,ty)
ans=""
for i in range(n):
if l[i]=="?":
if ty>1:
l[i]="1"
l[n-i-1]="1"
ty-=2
elif tx>1:
l[i]="0"
l[n-i-1]="0"
tx-=2
if flag:
if ty:l[n//2]="1"
else:l[n//2]="0"
for i in range(n):
print(l[i],end="")
print()
``` | instruction | 0 | 1,897 | 0 | 3,794 |
No | output | 1 | 1,897 | 0 | 3,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
for __ in range(int(input())):
a, b = list(map(int, input().split()))
n = a + b
ar = list(input())
for i in range(n):
if ar[i] == '0':
a -= 1
elif ar[i] == '1':
b -= 1
else:
if ar[n - i - 1] == '0':
a -= 1
ar[i] = '0'
elif ar[n - i - 1] == '1':
b -= 1
ar[i] = '1'
for i in range(n):
if ar[i] == '?' and n % 2 == 1 and i == n // 2:
if a % 2 == 1:
ar[i] = '0'
a -= 1
else:
ar[i] = '1'
b -= 1
elif ar[i] == '?' and a > 0:
ar[i] = '0'
ar[n - i - 1] = '0'
a -= 2
elif ar[i] == '?':
ar[i] = '1'
ar[n - i - 1] = '1'
b -= 2
if a == 0 == b:
ans = True
for i in range(n):
if ar[i] != ar[n - i - 1]:
ans = False
if ans:
print(''.join(ar))
else:
print(-1)
else:
print(-1)
``` | instruction | 0 | 1,898 | 0 | 3,796 |
No | output | 1 | 1,898 | 0 | 3,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
t = int(input())
for _ in range(t):
a1, b1 = map(int, input().split())
a = 0
b = 0
S1 = input()
S = []
answer = 0
for h in S1:
S.append(h)
n = len(S) - 1
for i in range(len(S)//2):
if S[i] != "?" and S[n-i] != "?":
if S[i] != S[n-i]:
answer = -1
break
else:
if S[i] == '0':
a += 2
else:
b += 2
elif S[i] == "?" and S[n-i] != "?":
if S[n-i] == '0':
a += 2
S[i] = '0'
else:
b += 2
S[i] = '1'
elif S[i] != "?" and S[n-i] == "?":
if S[i] == '0':
a += 2
S[n-i] = '0'
else:
b += 2
S[n-i] = '1'
else:
if a + 2 <= a1 and (a1 - S.count("0")) > (b1 - S.count("1")):
S[i] = '0'
S[n-i] = '0'
a += 2
else:
S[i] = '1'
S[n - i] = '1'
b += 2
if a > a1 or b > b1:
answer = - 1
break
if len(S) % 2 != 0:
if S[len(S) // 2] == '0':
a += 1
elif S[len(S) // 2] == '1':
b += 1
elif S[len(S) // 2] == '?':
if a < a1:
a += 1
S[len(S) // 2] = '0'
else:
b += 1
S[len(S) // 2] = '1'
if answer == -1:
print(-1)
elif a1 != a or b1 != b:
print(-1)
else:
StrS = "".join(S)
print( StrS)
``` | instruction | 0 | 1,899 | 0 | 3,798 |
No | output | 1 | 1,899 | 0 | 3,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i] = t[n-i+1] is true for all i (1 ≤ i ≤ n).
For example, if s="01?????0", a=4 and b=4, then you can replace the characters '?' in the following ways:
* "01011010";
* "01100110".
For the given string s and the numbers a and b, replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow.
The first line of each test case contains two integers a and b (0 ≤ a, b ≤ 2 ⋅ 10^5, a + b ≥ 1).
The second line of each test case contains the string s of length a+b, consisting of the characters '0', '1', and '?'.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output:
* "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
* the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
Input
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
Submitted Solution:
```
def getStr(a,b):
ans=["0"]*(a+b)
if a>b:
ac="0"
bc="1"
else:
ac="1"
bc="0"
a,b=b,a
i=0
n=a+b
while i<(n//2) and a>=0 and b>=0:
if i%2==0 and a>0:
ans[i]=ac
ans[-(i+1)]=ac
a-=2
elif b>0:
ans[i]=bc
ans[-(i+1)]=bc
b-=2
else:
if a>0:
while i<(n//2):
ans[i],ans[-(i+1)]=ac,ac
i+=1
break
if b>0:
while i<(n//2):
ans[i],ans[-(i+1)]=bc,bc
i+=1
break
i+=1
return "".join(ans)
for _ in range(int(input())):
a,b=list(map(int,input().split()))
l=list(input())
cq=l.count("?")
if cq==(a+b):
if cq%2==0:
if a%2==0 and b%2==0:
print(getStr(a,b))
else:
print("-1")
else:
n=a+b
if a%2==0 and b%2!=0:
b-=1
s=getStr(a,b)[0:n//2]
print(s+"1"+s[::-1])
elif a%2!=0 and b%2==0:
a-=1
s=getStr(a,b)[0:n//2]
print(s+"0"+s[::-1])
else:
print("-1")
else:
n,sdf,qc=(a+b)//2,a+b,0
if (a+b)%2!=0:
if (a+b)==1 and l[0]=="?":
qc+=1
elif l[n]=="?":
qc+=1
elif l[n]=="1":
b-=1
elif l[n]=="0":
a-=1
dd=True
for i in range(n):
if l[i]==l[-(i+1)]:
if l[i]=="?":
qc+=2
elif l[i]=="0":
a-=2
else:
b-=2
else:
if (l[i]=="0" and l[-(i+1)]=="?") or (l[i]=="?" and l[-(i+1)]=="0"):
a-=2
l[i],l[-(i+1)]="0","0"
elif (l[i]=="1" and l[-(i+1)]=="?") or (l[i]=="?" and l[-(i+1)]=="1"):
b-=2
l[i],l[-(i+1)]="1","1"
else:
print("-1")
dd=False
break
if dd and a>=0 and b>=0:
if sdf%2!=0:
n+=1
if qc%2==0:
if a%2==0 and b%2==0:
d=getStr(a,b)
j=0
for i in range(n):
if l[i]==l[-(i+1)]:
if l[i]=="?":
l[i],l[-(i+1)]=d[j],d[j]
j+=1
print("".join(l))
# print(l,n)
else:
print("-1")
else:
if a%2==0 and b%2!=0:
b-=1
s=getStr(a,b)[0:n//2]
d=s+"1"+s[::-1]
j=0
for i in range(n):
if l[i]==l[-(i+1)]:
if l[i]=="?":
l[i],l[-(i+1)]=d[j],d[j]
j+=1
print("".join(l))
elif a%2!=0 and b%2==0:
a-=1
s=getStr(a,b)[0:n//2]
d=s+"0"+s[::-1]
j=0
for i in range(n):
if l[i]==l[-(i+1)]:
if l[i]=="?":
l[i],l[-(i+1)]=d[j],d[j]
j+=1
print("".join(l))
else:
print("-1")
else:
print("-1")
``` | instruction | 0 | 1,900 | 0 | 3,800 |
No | output | 1 | 1,900 | 0 | 3,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,005 | 0 | 4,010 |
Tags: constructive algorithms, strings
Correct Solution:
```
s, t = input(), input()
for i in range(len(s)):
if s[i] == 'z':
continue
c = chr(ord(s[i]) + 1)
str = s[0:i] + c + ('a' * (len(s) - i - 1));
if (str < t) :
print(str)
exit();
print("No such string")
``` | output | 1 | 2,005 | 0 | 4,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,006 | 0 | 4,012 |
Tags: constructive algorithms, strings
Correct Solution:
```
# In case of length of s and t equal 1
# Calculate difference between s and t
# If difference is larger than 1
# Print next alphabet character in ASCII table
# Else
# Print 'No such string'
# Else
# Create a new string which is a clone of s
# Loop from start to end of both string s and t
# Calculate absolute difference between s[i] and t[i]
# If difference is larger than or equal to 0
# Identify next alphabet character
# If s[i] equal to 'z'
# Replace current character of clone string with 'a'
# Else
# Replace current character of clone string with next alphabet character in ASCII table
# Compare clone string with s and t
# If clone string is larger than s and smaller than t
# Print clone string
# Exit from program
#
# If there is no such string is printed while looping
# Print 'No such string'
s = input()
t = input()
length = len(s)
if length == 1:
dist = abs(ord(t) - ord(s))
if dist > 1:
print(chr(ord(s) + 1))
else:
print('No such string')
else:
s_clone = list(s)
for i in range(length - 1, -1, -1):
dist = abs(ord(t[i]) - ord(s[i]))
if dist >= 0:
if s[i] == 'z':
s_clone[i] = 'a'
else:
s_clone[i] = chr(ord(s[i]) + 1)
if ''.join(s_clone) > s and ''.join(s_clone) < t:
print(''.join(s_clone))
exit()
print('No such string')
``` | output | 1 | 2,006 | 0 | 4,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,007 | 0 | 4,014 |
Tags: constructive algorithms, strings
Correct Solution:
```
s = input()
t = input()
s = [i for i in s]
t = [i for i in t]
i = len(s)-1
while(i>=0):
if(s[i]=='z'):
s[i] = 'a'
else:
a = ord(s[i])+1
s[i] = chr(a)
break
i -= 1
s = ''.join(s)
t = ''.join(t)
if(s>=t):
print('No such string')
else:
print(s)
``` | output | 1 | 2,007 | 0 | 4,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,008 | 0 | 4,016 |
Tags: constructive algorithms, strings
Correct Solution:
```
# http://codeforces.com/problemset/problem/169/A
# A. Chores
def read_input():
a = input()
b = input()
return (a, b)
def do_middle_string(a, b):
c = list(a)
for i in range(len(c) - 1, -1, -1):
# print(i)
if c[i] == 'z':
c[i] = 'a'
else:
c[i] = chr(ord(c[i]) + 1)
break
c = "".join(c)
if a >= c or b <= c:
return None
return c
def main():
a, b = read_input()
result = do_middle_string(a, b)
print(result if result else "No such string")
if __name__ == '__main__':
main()
``` | output | 1 | 2,008 | 0 | 4,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,009 | 0 | 4,018 |
Tags: constructive algorithms, strings
Correct Solution:
```
s = input()
t = input()
index = len(s)-1
y = ord("z")
while index >= 0:
x = ord(s[index])
if x < y:
s = s[0:index]+chr(x+1)+"a"*(len(t)-index-1)
break
index -=1
if s == t:
print("No such string")
else:
print(s)
``` | output | 1 | 2,009 | 0 | 4,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,010 | 0 | 4,020 |
Tags: constructive algorithms, strings
Correct Solution:
```
s = input()
t = input()
k = 1
stri = ""
for i in s[::-1]:
m= ord(i) + k
if m> 122:
k = 1
stri += "a"
else:
k= 0
stri += chr(m)
if stri[::-1] == t:
print("No such string")
else:
print(stri[::-1])
``` | output | 1 | 2,010 | 0 | 4,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,011 | 0 | 4,022 |
Tags: constructive algorithms, strings
Correct Solution:
```
s = list(input())
t = list(input())
for i in range(len(s) - 1, -1, -1):
if s[i] == 'z':
s[i] = 'a'
else:
s[i] = chr(ord(s[i]) + 1)
break
print(''.join(s) if s < t else "No such string")
``` | output | 1 | 2,011 | 0 | 4,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,012 | 0 | 4,024 |
Tags: constructive algorithms, strings
Correct Solution:
```
s = input().strip()
t = input().strip()
r = list(s)
for i in range(len(s)-1, -1, -1):
if s[i] == 'z':
r[i] = 'a'
else:
r[i] = chr(ord(s[i]) + 1)
break
r = ''.join(r)
if t == r:
print("No such string")
else:
print(r)
``` | output | 1 | 2,012 | 0 | 4,025 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti. | instruction | 0 | 2,013 | 0 | 4,026 |
Tags: constructive algorithms, strings
Correct Solution:
```
from __future__ import print_function
# python 3 default
def main():
s = list(raw_input())
t = list(raw_input())
# map to ascii code
s_num = map(ord, s)
t_num = map(ord, t)
s_plus1 = list(s_num)
# z = 122
for i in range(len(s)-1, -1, -1):
s_plus1[i] += 1
if s_plus1[i] == 123: # z -> a
s_plus1[i] = 97 # a
else:
break
# check if splus1 < t
for i in range(len(s)):
if s_plus1[i] < t_num[i]:
print(''.join(map(chr, s_plus1)))
return 0
print('No such string')
return 1
if __name__ == '__main__':
main()
``` | output | 1 | 2,013 | 0 | 4,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
Submitted Solution:
```
u = input()
v = input()
z, n = 1001, len(u)
for i in range(n):
if u[i] != v[i]:
z = i
break
if z > 1000:
print('No such string')
elif ord(v[z]) - ord(u[z]) < 2:
x, y = 1001, 0
for i in range(z+1,n):
if u[i] != 'z':
x, y = i, 1
break
if v[i] != 'a':
x, y = i, 2
break
if y < 1:
print('No such string')
else:
print(u[:z], end = '')
if y == 1:
print(u[z:x] + 'z' + u[x+1:n])
else:
print(v[z:x] + 'a' + v[x+1:n])
else:
print(u[:z] + chr(ord(u[z]) + 1) + u[z+1:n])
``` | instruction | 0 | 2,017 | 0 | 4,034 |
Yes | output | 1 | 2,017 | 0 | 4,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
Submitted Solution:
```
s = input()
t = input()
if len(s) == 1 and len(t) == 1:
dist = ord(t) - ord(s)
if dist <= 1:
print('No such string')
else:
print(chr(ord(s) + 1))
else:
s_arr = list(s)
t_arr = list(t)
result = ''
sum = 0
for i in range(len(s)):
dist = abs(ord(t[i]) - ord(s[i]))
sum += dist
if dist > 1:
next_letter = chr(ord(s[i]) + 1)
if next_letter > 'z':
next_letter = 'a'
result += ''.join(next_letter)
else:
result += ''.join(s[i])
if sum <= 1:
print('No such string')
else:
print(result)
``` | instruction | 0 | 2,018 | 0 | 4,036 |
No | output | 1 | 2,018 | 0 | 4,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.
During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
Output
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
Examples
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
Submitted Solution:
```
line0 = input()
line1 = input()
numData0 = []
numData1 = []
c = 0
def PlayNow(arg):
for i in range(len(arg)):
print(chr(arg[i]), end='')
for i in range(len(line0)):
numData0.append(ord(line0[i]))
numData1.append(ord(line1[i]))
for i in range(len(line0)):
if (numData1[i] - numData0[i]) >= 1:
if (numData1[i] - numData0[i]) >= 2:
numData0[i] += 1
PlayNow(numData0)
c = 1
break
else:
for x in range(i+1, len(line0)):
if numData0[x] < ord('z'):
numData0[x] += 1
PlayNow(numData0)
c = 1
break
if c is 1:
break
if c is 0:
print('No such string')
``` | instruction | 0 | 2,021 | 0 | 4,042 |
No | output | 1 | 2,021 | 0 | 4,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.
Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.
The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
Output
If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.
If there are multiple possible answers, print any of them.
Examples
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}. | instruction | 0 | 2,022 | 0 | 4,044 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
t = input()
if n > len(set(t)):
print('NO')
else:
pos = [0 for _ in range(len(t)+1)]
dis = set()
c = 0
for i in range(len(t)):
if t[i] not in dis:
dis.add(t[i])
pos[c] = i
c = c+1
pos[n] = len(t)
print('YES')
for i in range(n):
print(t[pos[i]:pos[i+1]])
``` | output | 1 | 2,022 | 0 | 4,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.
Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.
The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
Output
If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.
If there are multiple possible answers, print any of them.
Examples
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}. | instruction | 0 | 2,023 | 0 | 4,046 |
Tags: implementation, strings
Correct Solution:
```
import sys
k = int(input())
q = input()
if len(set(q)) < k:
print('NO')
else:
first = ''
s = ''
print('YES')
for i in range(len(q)):
if q[i] not in first:
if len(s) > 0:
print(s)
s =''
s = q[i]
first += q[i]
k -= 1
if k == 0:
s += q[i+1:len(q)]
print(s)
break
else:
s += q[i]
``` | output | 1 | 2,023 | 0 | 4,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.
Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.
The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
Output
If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.
If there are multiple possible answers, print any of them.
Examples
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}. | instruction | 0 | 2,024 | 0 | 4,048 |
Tags: implementation, strings
Correct Solution:
```
k = int(input())
q = input()
b = []
for x in range(len(q)):
if q[0: x + 1].count(q[x]) == 1:
b.append(q[x])
else:
b[-1] += q[x]
if len(b) >= k:
print("YES")
for x in range(k-1):
print(b[x])
for y in range(k, len(b)):
b[k-1] += b[y]
print(b[k-1])
if len(b) < k:
print("NO")
``` | output | 1 | 2,024 | 0 | 4,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.
Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.
The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
Output
If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.
If there are multiple possible answers, print any of them.
Examples
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}. | instruction | 0 | 2,025 | 0 | 4,050 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
s = list(str(input()))
dif = list(dict.fromkeys(s))
ans = []
if(n==1):
print('YES')
print(''.join(s))
else:
cur = 0
pnt = 0
lst = s[0]
cnt = 1
used = [s[0]]
for i in range(len(s)):
if(s[i] not in used):
if(i==len(s)-1 or cnt== n-1):
ans.append(''.join(s[cur:i]))
ans.append(''.join(s[i:]))
break
else:
ans.append(''.join(s[cur:i]))
cur = int(i)
cnt+=1
used.append(s[i])
if(len(ans)==n):
print('YES')
for i in range(n):
print(ans[i])
else:
print('NO')
``` | output | 1 | 2,025 | 0 | 4,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.
Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
Input
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence.
The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
Output
If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.
If there are multiple possible answers, print any of them.
Examples
Input
1
abca
Output
YES
abca
Input
2
aaacas
Output
YES
aaa
cas
Input
4
abc
Output
NO
Note
In the second sample there are two possible answers: {"aaaca", "s"} and {"aaa", "cas"}. | instruction | 0 | 2,026 | 0 | 4,052 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
s = input()
arr = []
for i in range(len(s)):
if s[i] not in s[:i]:
arr.append(i)
if (len(arr) < n):
print('NO')
exit()
print('YES')
for i in range(n - 1):
print(s[arr[i] : arr[i + 1]])
if len(arr) >= n:
print(s[arr[n - 1] : len(s)])
``` | output | 1 | 2,026 | 0 | 4,053 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.