text stringlengths 594 23.8k | conversation_id int64 97 109k | embedding list | cluster int64 0 0 |
|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Output
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
Submitted Solution:
```
k = int(input())
s = input(); n = len(s)
ans = 0
cnt = 0
freq = [0]*(n+1)
freq[0] = 1
for i in range(n):
if s[i]=='1': cnt += 1
if cnt-k>=0:
ans += freq[cnt-k]
freq[cnt] += 1
print(ans)
# Made By Mostafa_Khaled
```
Yes
| 63,413 | [
0.1539306640625,
-0.216064453125,
0.00830078125,
0.1044921875,
-0.44189453125,
-0.52783203125,
-0.08831787109375,
0.047393798828125,
0.07159423828125,
0.99072265625,
0.56103515625,
-0.587890625,
0.26171875,
-0.8896484375,
-0.46875,
-0.040985107421875,
-0.5458984375,
-0.8310546875,
... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Output
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
Submitted Solution:
```
k = int(input())
s = input()
n = len(s)
if(s.count('1') < k):
print(0)
exit(0)
i,j,ct = 0,0,0
for j in range(n):
if(s[j] == '1'):
ct += 1
if(ct == k):
break
i = s.find('1')
ans = 0
b = i
while(j < n):
j += 1
a = 0
while(j < n and s[j] == '0'):
a += 1
j += 1
ans += (b+1)*(a+1)
i += 1
b = 0
while(i < n and s[i] == '0'):
b += 1
i += 1
print(ans)
```
No
| 63,414 | [
0.20556640625,
-0.217529296875,
0.071044921875,
0.093505859375,
-0.56591796875,
-0.5673828125,
-0.03472900390625,
0.09759521484375,
0.03619384765625,
1.00390625,
0.57958984375,
-0.6171875,
0.287841796875,
-0.84326171875,
-0.368408203125,
-0.126708984375,
-0.53369140625,
-0.76171875... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Output
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
Submitted Solution:
```
def readln(): return tuple(map(int, input().split()))
k, = readln()
inp = '#' + input()
n = len(inp)
pref = [0] * n
for i in range(1, n):
pref[i] = pref[i - 1] + (inp[i] == '1')
cnt = [0] * (pref[n - 1] + 1)
for v in pref:
cnt[v] += 1
ans = 0
for i in range(pref[n - 1] - k + 1):
ans += cnt[i] * cnt[i + k]
if k == 0:
print(len([1 for _ in list(inp) if _ == '0']))
else:
print(ans)
```
No
| 63,415 | [
0.1995849609375,
-0.2490234375,
-0.00839996337890625,
0.06597900390625,
-0.60107421875,
-0.62109375,
-0.054534912109375,
0.00949859619140625,
0.02789306640625,
0.99560546875,
0.59619140625,
-0.6240234375,
0.424560546875,
-0.80078125,
-0.318603515625,
-0.0699462890625,
-0.5859375,
-... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Output
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
Submitted Solution:
```
from sys import stdin
from collections import defaultdict
n = int(stdin.buffer.readline())
line = stdin.buffer.readline()
d = defaultdict(int)
d[0] += 1
sm = 0
for c in line.decode().strip():
if c == '1':
sm += 1
d[sm] += 1
ans = 0
for i in range(n, sm + 1):
ans += d[i] * d[i - n]
print (ans)
```
No
| 63,416 | [
0.09405517578125,
-0.257080078125,
0.09405517578125,
0.1085205078125,
-0.57080078125,
-0.556640625,
-0.06707763671875,
0.058258056640625,
-0.041229248046875,
1.0341796875,
0.57861328125,
-0.69677734375,
0.280029296875,
-0.76708984375,
-0.391357421875,
-0.1151123046875,
-0.5673828125,... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Output
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
k=Int()
a=input()
n=len(a)
indexOf1=[]
neigbour=defaultdict(lambda : [0,0])
zeros=0
if(k==0):
i=0
tot=0
while(i<n):
if(a[i]=='0'):
c=1
i+=1
while(i<n and a[i]=='0'):
i+=1
c+=1
tot+=c*(c+1)//2
else:
i+=1
print(tot)
exit()
for i in range(n):
if(a[i]=='1'):
if(indexOf1==[]):
neigbour[i][0]=zeros
else:
neigbour[i][0]=zeros
neigbour[last][1]=zeros
last=i
indexOf1.append(i)
zeros=0
else:
zeros+=1
if(len(indexOf1)<k):
print(0)
exit()
neigbour[last][1]=zeros
# if k!=0 and atleast one 1 #
ans=0
start=indexOf1[0]
end=indexOf1[k-1]
#print(indexOf1)
#print(neigbour)
for i in range(0,len(indexOf1)-k+1):
first=indexOf1[i]
last=indexOf1[i+k-1]
#print(first,last,k)
k1=neigbour[first][0]+neigbour[last][1]
ans+=1+k1*(k1+1)//2
print(ans)
```
No
| 63,417 | [
0.11016845703125,
-0.265380859375,
0.060791015625,
0.1689453125,
-0.599609375,
-0.49609375,
-0.0848388671875,
0.07061767578125,
0.007472991943359375,
1.0498046875,
0.61767578125,
-0.62841796875,
0.2958984375,
-0.8369140625,
-0.345947265625,
0.0102996826171875,
-0.55322265625,
-0.79... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Tags: games, greedy
Correct Solution:
```
def main(a, b, l, r):
qL = (l - 1) // (2 * a + 2 * b)
rL = (l - 1) % (2 * a + 2 * b) + 1
qR = (r - 1) // (2 * a + 2 * b)
rR = (r - 1) % (2 * a + 2 * b) + 1
#print(qL, qR, rL, rR)
if qL == qR:
#In b segment
if a < rL <= a + b and a < rR <= a + b:
return 1
if 2 * a + b < rL and 2 * a + b < rR:
return 1
#In a segment
if 1 <= rL <= a and 1 <= rR <= a:
return rR - rL + 1
if a + b < rL <= 2 * a + b and a + b < rR <= 2 * a + b:
return rR - rL + 1
#In a + b segment
if 1 <= rL <= a + b and 1 <= rR <= a + b:
return a - rL + 1
#In abab segment
if a + b < rL and a + b < rR:
return (2 * a + b) - rL + 1
if a < rL <= a + b and a + b < rR <= 2 * a + b:
return 1 + rR - (a + b)
if a < rL <= a + b and 2 * a + b < rR:
return 1 + a
if 1 <= rL <= a and a + b < rR <= 2 * a + b:
ans = a - rL + 1 + max(rR - (a + b + b), 0) + min(b, rR) - max(min(rR, b) - rL + 1, 0)
return ans
if 1 <= rL <= a and 2 * a + b < rR:
return a - rL + 1 + a - max(b - rL + 1, 0)
elif qL == qR - 1:
#abababab
newL = qL * (2 * a + 2 * b) + 1
newR = (qR + 1) * (2 * a + 2 * b)
if 1 <= rL <= a + b and a + b + 1 <= rR:
return a + max(a - b, 0) + int(a <= b)
if a + b + 1 <= rL <= 2 * (a + b) and (2 * a + 2 * b) + 1 <= rR <= a + b:
return main(a, b, l - (a + b), r - (a + b))
if 1 <= rL <= a and 1 <= rR <= a:
return a + max(a - b, 0) + int(a <= b) + rR - max(rR - rL + 1, 0)
if 1 <= rL <= a and a + 1 <= rR <= a + b:
return a + max(a - b, 0) + int(a <= b)
if a + 1 <= rL <= a + b and 1 <= rR <= a:
return 1 + a
if a + 1 <= rL <= a + b and a + 1 <= rR <= a + b:
return 1 + a + max(a - b, 0)
return main(a, b, l - (a + b), r - (a + b))
else:
return a + max(a - b, 0) + int(a <= b) # + main(a, b, l, (qL + 1) * (2 * a + 2 * b)) + main(a, b, qR * (2 * a + 2 * b) + 1, r)
a, b, l, r = [int(item) for item in input().split()]
print(main(a, b, l, r))
```
| 63,616 | [
0.28955078125,
0.0809326171875,
0.274169921875,
0.458251953125,
-0.461181640625,
-0.64599609375,
-0.11236572265625,
0.209716796875,
-0.242431640625,
0.69677734375,
0.7900390625,
-0.159912109375,
-0.251953125,
-0.6982421875,
-0.90234375,
-0.1673583984375,
-0.74609375,
-0.52001953125... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Tags: games, greedy
Correct Solution:
```
a,b,l,r=map(int, input().split())
length=int(l/(a+b))
if a==3 and b==1 and l==4 and r==10:
print(4)
exit()
l-=length*(a+b)
r-=length*(a+b)
if r>=4*a+4*b:
r=4*a+4*b
if b>=a:
_A=[]
for i in range(a):
_A.append(i+1)
for i in range(b):
_A.append(a)
for i in range(a):
_A.append(i+1)
_A[2*a+b-1]+=1
for i in range(b):
_A.append(_A[2*a+b-1])
for i in range(2*a+2*b):
_A.append(_A[i])
_B=[]
for i in range(25):
_B.append(0)
cnt=0
for i in range(r-l+1):
if _B[_A[l+i-1]]==0:
cnt+=1
_B[_A[l+i-1]]=1
else:
_A=[]
for i in range(a):
_A.append(i+1)
for i in range(b):
_A.append(a)
for i in range(a):
if i+1<=b:
_A.append(i+1)
else:
_A.append(a+i-b+2)
for i in range(b):
_A.append(_A[2*a+b-1])
for i in range(2*a+2*b):
_A.append(_A[i])
_B=[]
for i in range(25):
_B.append(0)
cnt=0
for i in range(r-l+1):
if _B[_A[l+i-1]]==0:
cnt+=1
_B[_A[l+i-1]]=1
# print(_A)
print(cnt)
```
| 63,617 | [
0.28955078125,
0.0809326171875,
0.274169921875,
0.458251953125,
-0.461181640625,
-0.64599609375,
-0.11236572265625,
0.209716796875,
-0.242431640625,
0.69677734375,
0.7900390625,
-0.159912109375,
-0.251953125,
-0.6982421875,
-0.90234375,
-0.1673583984375,
-0.74609375,
-0.52001953125... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Tags: games, greedy
Correct Solution:
```
def main(a, b, l, r):
qL = (l - 1) // (2 * a + 2 * b)
rL = (l - 1) % (2 * a + 2 * b) + 1
qR = (r - 1) // (2 * a + 2 * b)
rR = (r - 1) % (2 * a + 2 * b) + 1
#print(qL, qR, rL, rR)
if qL == qR:
#In b segment
if a < rL <= a + b and a < rR <= a + b:
return 1
if 2 * a + b < rL and 2 * a + b < rR:
return 1
#In a segment
if 1 <= rL <= a and 1 <= rR <= a:
return rR - rL + 1
if a + b < rL <= 2 * a + b and a + b < rR <= 2 * a + b:
return rR - rL + 1
#In a + b segment
if 1 <= rL <= a + b and 1 <= rR <= a + b:
return a - rL + 1
if a + b < rL and a + b < rR:
return (2 * a + b) - rL + 1
if a < rL <= a + b and a + b < rR <= 2 * a + b:
return 1 + rR - (a + b)
if a < rL <= a + b and 2 * a + b < rR:
return 1 + a
if 1 <= rL <= a and a + b < rR <= 2 * a + b:
ans = a - rL + 1 + max(rR - (a + b + b), 0) + min(b, rR) - max(min(rR, b) - rL + 1, 0)
return ans
if 1 <= rL <= a and 2 * a + b < rR:
return a - rL + 1 + a - max(b - rL + 1, 0)
elif qL == qR - 1:
#abababab
newL = qL * (2 * a + 2 * b) + 1
newR = (qR + 1) * (2 * a + 2 * b)
if 1 <= rL <= a + b and a + b + 1 <= rR:
return a + max(a - b, 0) + int(a <= b)
if a + b + 1 <= rL <= 2 * (a + b) and (2 * a + 2 * b) + 1 <= rR <= a + b:
return main(a, b, l - (a + b), r - (a + b))
if 1 <= rL <= a and 1 <= rR <= a:
return a + max(a - b, 0) + int(a <= b) + rR - max(rR - rL + 1, 0)
if 1 <= rL <= a and a + 1 <= rR <= a + b:
return a + max(a - b, 0) + int(a <= b)
if a + 1 <= rL <= a + b and 1 <= rR <= a:
return 1 + a
if a + 1 <= rL <= a + b and a + 1 <= rR <= a + b:
return 1 + a + max(a - b, 0)
return main(a, b, l - (a + b), r - (a + b))
else:
return a + max(a - b, 0) + int(a <= b) # + main(a, b, l, (qL + 1) * (2 * a + 2 * b)) + main(a, b, qR * (2 * a + 2 * b) + 1, r)
a, b, l, r = [int(item) for item in input().split()]
print(main(a, b, l, r))
# Made By Mostafa_Khaled
```
| 63,618 | [
0.28955078125,
0.0809326171875,
0.274169921875,
0.458251953125,
-0.461181640625,
-0.64599609375,
-0.11236572265625,
0.209716796875,
-0.242431640625,
0.69677734375,
0.7900390625,
-0.159912109375,
-0.251953125,
-0.6982421875,
-0.90234375,
-0.1673583984375,
-0.74609375,
-0.52001953125... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Tags: games, greedy
Correct Solution:
```
def main(a, b, l, r):
qL = (l - 1) // (2 * a + 2 * b)
rL = (l - 1) % (2 * a + 2 * b) + 1
qR = (r - 1) // (2 * a + 2 * b)
rR = (r - 1) % (2 * a + 2 * b) + 1
#print(qL, qR, rL, rR)
if qL == qR:
#In b segment
if a < rL <= a + b and a < rR <= a + b:
return 1
if 2 * a + b < rL and 2 * a + b < rR:
return 1
#In a segment
if 1 <= rL <= a and 1 <= rR <= a:
return rR - rL + 1
if a + b < rL <= 2 * a + b and a + b < rR <= 2 * a + b:
return rR - rL + 1
#In a + b segment
if 1 <= rL <= a + b and 1 <= rR <= a + b:
return a - rL + 1
if a + b < rL and a + b < rR:
return (2 * a + b) - rL + 1
if a < rL <= a + b and a + b < rR <= 2 * a + b:
return 1 + rR - (a + b)
if a < rL <= a + b and 2 * a + b < rR:
return 1 + a
if 1 <= rL <= a and a + b < rR <= 2 * a + b:
ans = a - rL + 1 + max(rR - (a + b + b), 0) + min(b, rR) - max(min(rR, b) - rL + 1, 0)
return ans
if 1 <= rL <= a and 2 * a + b < rR:
return a - rL + 1 + a - max(b - rL + 1, 0)
elif qL == qR - 1:
#abababab
newL = qL * (2 * a + 2 * b) + 1
newR = (qR + 1) * (2 * a + 2 * b)
if 1 <= rL <= a + b and a + b + 1 <= rR:
return a + max(a - b, 0) + int(a <= b)
if a + b + 1 <= rL <= 2 * (a + b) and (2 * a + 2 * b) + 1 <= rR <= a + b:
return main(a, b, l - (a + b), r - (a + b))
if 1 <= rL <= a and 1 <= rR <= a:
return a + max(a - b, 0) + int(a <= b) + rR - max(rR - rL + 1, 0)
if 1 <= rL <= a and a + 1 <= rR <= a + b:
return a + max(a - b, 0) + int(a <= b)
if a + 1 <= rL <= a + b and 1 <= rR <= a:
return 1 + a
if a + 1 <= rL <= a + b and a + 1 <= rR <= a + b:
return 1 + a + max(a - b, 0)
return main(a, b, l - (a + b), r - (a + b))
else:
return a + max(a - b, 0) + int(a <= b) # + main(a, b, l, (qL + 1) * (2 * a + 2 * b)) + main(a, b, qR * (2 * a + 2 * b) + 1, r)
a, b, l, r = [int(item) for item in input().split()]
print(main(a, b, l, r))
```
| 63,619 | [
0.28955078125,
0.0809326171875,
0.274169921875,
0.458251953125,
-0.461181640625,
-0.64599609375,
-0.11236572265625,
0.209716796875,
-0.242431640625,
0.69677734375,
0.7900390625,
-0.159912109375,
-0.251953125,
-0.6982421875,
-0.90234375,
-0.1673583984375,
-0.74609375,
-0.52001953125... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Submitted Solution:
```
(a,b,l,r) = (int(i) for i in input().split())
alf = 'abcdefghijklmnopqrstuvwxyz'
tstr = alf[:a]
tstr+=tstr[-1]*b
genstr = ''
i = 0
zapr = tstr[-a:]
while len(genstr)<a:
if alf[i] not in zapr:
genstr+=alf[i]
i+=1
tstr+=genstr
tstr+=tstr[-1]*b
onelen = 2*(a+b)
y = r%(onelen)
x = onelen - l%(onelen) + 1
z = (1+r-l-x-y)/onelen
if z<1:
# print(x,y,z)
tstr = tstr[-x:] + tstr[:y-1+(y==0)]
# print(tstr)
if z==-1:
tstr = tstr[:r-l+1]
st = set()
for i in tstr:
st.add(i)
print(len(st))
```
No
| 63,620 | [
0.430908203125,
0.10638427734375,
0.1767578125,
0.441650390625,
-0.499267578125,
-0.52783203125,
-0.1663818359375,
0.2320556640625,
-0.284423828125,
0.66064453125,
0.75048828125,
-0.09521484375,
-0.300537109375,
-0.71923828125,
-0.802734375,
-0.1934814453125,
-0.67041015625,
-0.592... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Submitted Solution:
```
a, b, l, r = list(map(int, input().split()))
d = (l // (a + b)) * (a + b)
l -= d
r -= d
if r - l > a + b:
l = 1
r = 2 * (a + b)
k = a
m = b
s = [chr(ord('a') + i) for i in range(a)]
while k <= r:
if m == b:
k += b
for i in range(m):
s.append(s[-1])
m = -a
else:
k += a
m = -m
st = set(list(map(lambda x : ord(x) - ord('a'), s[-a:])))
i, j = 0, 0
while i < m:
while j in st:
j += 1
s.append(chr(ord('a') + j))
j += 1
i += 1
print(len(set(s[l - 1:r])))
```
No
| 63,621 | [
0.430908203125,
0.10638427734375,
0.1767578125,
0.441650390625,
-0.499267578125,
-0.52783203125,
-0.1663818359375,
0.2320556640625,
-0.284423828125,
0.66064453125,
0.75048828125,
-0.09521484375,
-0.300537109375,
-0.71923828125,
-0.802734375,
-0.1934814453125,
-0.67041015625,
-0.592... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Submitted Solution:
```
def fb(s, i):
c = chr(ord('a') + i)
for _ in range(b):
s += c
return s
def fa(s):
used = [False for _ in range(26)]
for i in range(a):
used[ord(s[-i - 1]) - ord('a')] = True
t = ''
for i in range(26):
if not used[i]:
t += chr(ord('a') + i)
if len(t) == a:
break
return s + t
a, b, l, r = map(int, input().split())
s = ''.join([chr(ord('a') + i) for i in range(a)])
ans = 26
for i in range(26):
t = s
for _ in range(52):
t = fb(t, i)
t = fa(t)
ln = 26 * (a + b)
il = l % ln
ir = r % ln
if ir < il:
ir += ln
ls = []
for i in range(il, ir + 1):
ls.append(t[i])
ans = min(ans, len(set(ls)))
print(ans)
```
No
| 63,622 | [
0.430908203125,
0.10638427734375,
0.1767578125,
0.441650390625,
-0.499267578125,
-0.52783203125,
-0.1663818359375,
0.2320556640625,
-0.284423828125,
0.66064453125,
0.75048828125,
-0.09521484375,
-0.300537109375,
-0.71923828125,
-0.802734375,
-0.1934814453125,
-0.67041015625,
-0.592... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.
All characters in this game are lowercase English letters. There are two players: Mister B and his competitor.
Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde").
The players take turns appending letters to string s. Mister B moves first.
Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.
Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s.
Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.
Input
First and only line contains four space-separated integers: a, b, l and r (1 ≤ a, b ≤ 12, 1 ≤ l ≤ r ≤ 109) — the numbers of letters each player appends and the bounds of the segment.
Output
Print one integer — the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.
Examples
Input
1 1 1 8
Output
2
Input
4 2 2 6
Output
3
Input
3 7 4 6
Output
1
Note
In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2.
In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3.
In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
Submitted Solution:
```
def main(a, b, l, r):
qL = (l - 1) // (2 * a + 2 * b)
rL = (l - 1) % (2 * a + 2 * b) + 1
qR = (r - 1) // (2 * a + 2 * b)
rR = (r - 1) % (2 * a + 2 * b) + 1
if qL == qR:
#In b segment
if a < rL <= a + b and a < rR <= a + b:
return 1
if 2 * a + b < rL and 2 * a + b < rR:
return 1
#In a segment
if 1 <= rL <= a and 1 <= rR <= a:
return rR - rL + 1
if a + b < rL <= 2 * a + b and a + b < rR <= 2 * a + b:
return rR - rL + 1
#In a + b segment
if 1 <= rL <= a + b and 1 <= rR <= a + b:
return a - rL + 1
if a + b < rL and a + b < rR:
return (2 * a + b) - rL + 1
if a < rL <= a + b and a + b < rR <= 2 * a + b:
return 1 + rR - (a + b)
if a < rL <= a + b and 2 * a + b < rR:
return 1 + a
if 1 <= rL <= a and a + b < rR <= 2 * a + b:
ans = a - rL + 1 + max(rR - (a + b + b), 0) + min(b, rR) - max(min(rR, b) - rL + 1, 0)
return ans
if 1 <= rL <= a and 2 * a + b < rR:
return a - rL + 1 + a - max(b - rL + 1, 0)
else:
return a + max(a - b, 0) + int(a == b) # + main(a, b, l, (qL + 1) * (2 * a + 2 * b)) + main(a, b, qR * (2 * a + 2 * b) + 1, r)
a, b, l, r = [int(item) for item in input().split()]
print(main(a, b, l, r))
```
No
| 63,623 | [
0.430908203125,
0.10638427734375,
0.1767578125,
0.441650390625,
-0.499267578125,
-0.52783203125,
-0.1663818359375,
0.2320556640625,
-0.284423828125,
0.66064453125,
0.75048828125,
-0.09521484375,
-0.300537109375,
-0.71923828125,
-0.802734375,
-0.1934814453125,
-0.67041015625,
-0.592... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
import copy
t = int(input())
for i in range(t):
d = {0:0, 1:0}
n = int(input())
arr = []
for j in range(n):
s = input()
for x in s:
if x == '1':
d[1] += 1
else:
d[0] += 1
arr += [len(s)]
# print(d)
# print(arr)
eve = []
odd = []
for x in arr:
if x%2 == 0:
eve += [x]
else:
odd += [x]
eve.sort()
odd.sort()
# print(eve, odd)
ansarr = []
ori = copy.deepcopy(d)
# for x in range(0, len(odd)+1):
# d = copy.deepcopy(ori)
# oddnum = x
# evenum = min(len(arr)-x, len(eve))
# # print(evenum, oddnum)
# count = 0
# # print("KK")
# print(d)
# for y in range(oddnum):
# if odd[y] <= d[0]:
# d[0] -= odd[y]
# count += 1
# elif odd[y] <= d[1] + d[0]:
# d[1] = d[1] - (odd[y]-d[0])
# d[0] = 0
# count += 1
# print(d)
# for y in range(evenum):
# if eve[y] <= d[0]:
# d[0] -= eve[y]
# count += 1
# elif eve[y] <= d[1] + d[0]:
# # num = 2*(d[0]//2)
# # rem = eve[y] - num
# # if rem <= eve[y]:
# # d[1] -= rem
# # count += 1
# if (d[0] == 1 or d[0] == 0) and eve[y] <= d[1]:
# d[1] -= eve[y]
# count += 1
# elif d[0] > 1:
# num = 2* (d[0]//2)
# d[0] -= num
# rem = eve[y]-num
# if rem <= d[1]:
# d[1] -= rem
# count += 1
# ansarr += [count]
# print(evenum, oddnum, count)
arr.sort()
count = 0
for x in arr:
num = 0
if x%2 == 0:
while num != x:
if d[0] <= 1 and d[1] <= 1:
break
if d[1] >= d[0] and d[1] > 1:
num += 2
d[1] -= 2
elif d[0] > d[1] and d[0] > 1:
num += 2
d[0] -= 2
if num == x:
count += 1
else:
if d[0]%2 == 1:
d[0] -= 1
num += 1
elif d[1]%2 == 1:
d[1] -= 1
num += 1
if num == 1:
while num != x:
if d[0] <= 1 and d[1] <= 1:
break
if d[1] >= d[0] and d[1] > 1:
num += 2
d[1] -= 2
elif d[0] > d[1] and d[0] > 1:
num += 2
d[0] -= 2
if num == x:
count += 1
else:
if d[0] != 0:
d[0] -= 1
num += 1
elif d[1] != 0:
d[1] -= 1
num += 1
while num != x:
if d[0] <= 1 and d[1] <= 1:
break
if d[1] >= d[0] and d[1] > 1:
num += 2
d[1] -= 2
elif d[0] > d[1] and d[0] > 1:
num += 2
d[0] -= 2
if num == x:
count += 1
print(count)
# print(ansarr)
# print(max(ansarr))
```
| 64,083 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
import math
for _ in range(int(input())):
n=int(input())
c=0
se=0
ec=0
for i in range(n):
s=list(input())
if len(s)%2==1:
c+=1
else:
if s.count('0')%2==1:
se+=1
else:
ec+=1
if se%2==1:
if c>0:
print(n)
else:
print(ec+2*(se//2))
else:
print(c+ec+2*(se//2))
```
| 64,084 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
from sys import stdin
input=stdin.readline
for _ in range(int(input())):
n=int(input());v=odd=even=0
for i in range(n):
x=input().rstrip('\n')
v+=x.count('1')
l=len(x)
if l%2:odd+=l
else:even+=l
if even:
if v%2:v-=min(v-1,even)
else:v-=min(v,even)
v-=min(v,odd)
print(n-v)
```
| 64,085 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
n = int(input())
for _ in range(n):
q= int(input())
S = []
for __ in range(q):
S.append(input())
res=0
cnt=0
for s in S:
c0=s.count('0')
c1=s.count('1')
if c0%2==1 and c1%2==1:
res-=1
cnt+=1
elif (c0+c1)%2==1:
res=0
break
if res>=0:
res=0
else:
res=-1
if cnt%2==0:
res=0
print(len(S)+res)
```
| 64,086 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
q=int(input())
for i in range(q):
n=int(input())
cnt1=cnt0=c0=c1=0
for j in range(n):
a=[int(x) for x in list(input())]
counter=0
for item in a:
counter+=1
if item==0:
cnt0+=1
else:
cnt1+=1
if counter%2==0:
c0+=1
else:
c1+=1
if c1%2==1:
if cnt1%2==cnt0%2:
print(n-1)
else:
print(n)
else:
if cnt1%2==cnt0%2==1 and c1!=0:
print(n)
elif cnt0%2==cnt1%2==0:
print(n)
else:
print(n-1)
```
| 64,087 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
# Author : raj1307 - Raj Singh
# Date : 24.10.19
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(100000000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()
from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import ceil,floor,log,sqrt,factorial
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *,threading
#from itertools import permutations
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[0]
def sort2(l):return sorted(l, key=getKey)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def main():
for _ in range(ii()):
n=ii()
s=[]
for i in range(n):
s.append(list(si()))
one=[]
sz=[]
for i in range(n):
cnt=0
for j in s[i]:
if j=='1':
cnt+=1
one.append(cnt)
sz.append(len(s[i]))
cnt=0
odd=[]
for i in range(n):
if sz[i]%2==0:
if one[i]%2==1:
cnt+=1
else:
odd.append(one[i])
#print(cnt,'i')
if cnt%2==0:
print(n)
else:
ans=n-1
for i in range(len(odd)):
ans=n
break
print(ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#dmain()
# Comment Read()
```
| 64,088 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().strip()
t = int(input())
for i in range(t):
n = int(input())
lens = []
cnts = {'0': 0, '1': 0}
for i in range(n):
s = input()
for i in s:
cnts[i]+=1
lens.append(len(s))
lens.sort()
ans = 0
cnts = list(cnts.values())
for length in lens:
for i in range(length//2):
if cnts[0]>=2:
cnts[0]-=2
elif cnts[1]>=2:
cnts[1]-=2
else: break
else:
if length%2:
if cnts[0]%2 and cnts[0]>=1:
cnts[0]-=1
elif cnts[1]%2 and cnts[1]>=1:
cnts[1]-=1
elif cnts[0]>=1:
cnts[0]-=1
elif cnts[1]>=1:
cnts[1]-=1
else: break
ans+=1
continue
break
print(ans)
```
| 64,089 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Tags: greedy, strings
Correct Solution:
```
t = int(input())
for _ in range(t):
ch1 = 0
ch0 = 0
cnp = 0
pp = 0
n = int(input())
for i in range(n):
s = input()
c1 = 0
c0 = 0
for j in range(len(s)):
if(s[j]=='1'):
c1 += 1
else:
c0 += 1
if(c1%2==0 and c0%2==0):
cnp += 1
elif(c1%2==0 and c0%2==1):
cnp += 1
ch0 = 1
elif(c1%2==1 and c0%2==0):
cnp += 1
ch1 = 1
elif(c1%2==1 and c0%2==1):
pp += 1
#print(pp,c1,c0)
cnp += pp//2*2
if(pp%2==1):
pp = 1
if(pp==1 and (ch0==1 or ch1==1)):
cnp += 1
print(cnp)
```
| 64,090 | [
0.1962890625,
-0.178955078125,
-0.5341796875,
-0.0260772705078125,
-0.4716796875,
-0.368408203125,
0.25244140625,
-0.07696533203125,
0.205322265625,
1.0185546875,
0.74169921875,
-0.29833984375,
0.1722412109375,
-1.0146484375,
-0.54638671875,
-0.1793212890625,
-0.607421875,
-0.82617... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
Q = int(input())
for _ in range(Q):
n = int(input())
zs = 0
os = 0
odd = 0
for k in range(n):
s = input()
zs += s.count('0')
os += s.count('1')
odd += len(s) % 2
#print(zs,os,odd)
if zs % 2 == 1 and os % 2 == 1 and odd == 0:
print(n-1)
else:
print(n)
```
Yes
| 64,091 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
for i in range(int(input())):
alleven = True
minus = False
m = int(input())
strings = []
for j in range(m):
strings.append(input())
for current in strings:
if len(current)%2:
alleven = False
print(m)
break
else:
if current.count("0")%2: minus = not minus
if alleven: print(m-minus)
```
Yes
| 64,092 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
from collections import defaultdict as dc
import math
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n=int(input())
e=0
o=0
b=0
c=0
for i in range(n):
s=input()[:-1]
if len(s)%2:
o+=1
c+=1
else:
x=0
y=0
for j in s:
if j=='0':
x+=1
else:
y+=1
if x%2 or y%2:
b+=1
else:
c+=1
#print(o,c,b)
x=min(o,b)
y=((b-x)//2)*2
m=c+x+y
x=(b//2)*2
y=min(o,b-x)
m=max(m,c+x+y)
print(m)
```
Yes
| 64,093 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
t=int(input())
for i in range(t):
S=[]
p=0
p1=0
p2=0
np=0
flag=0
n=int(input())
for j in range(n):
S.append(input())
for j in S:
c1=j.count('0')
c2=j.count('1')
if c1%2==0 or c2%2==0:
p=p+1
if c1%2==0 and c2%2==0 :
p1=p1+1
else:
np=np+1
while np>0 and np!=1 :
np=np-2
p2=p2+2
if(np==1 and p-p1>0):
p=p+1
print(p+p2)
```
Yes
| 64,094 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
for i in range(int(input())):
s = 0
total = int(input())
for j in range(total):
cs = input()
if len(cs)%2==0 and cs.count("0")%2: s += 1
print(total - (s%2))
```
No
| 64,095 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
q=int(input())
def create_lists(check_lists,n):
if len(check_lists)>0:
for l in check_lists:
if len(l) % 2 == 1:
return(n)
s_join=''.join(l)
result=check(s_join,n)
if result:
return result
new_check_lists=[]
for l in check_lists:
for ni in range(n):
new_element= l[0:ni] + l[ni+1:]
if len(new_element)>0:
new_check_lists.append(new_element)
check_lists=new_check_lists
return create_lists(check_lists, n-1)
else:
return 0
def check(string,n):
zero_count=0
one_count=0
for x in string:
if x=='0':
zero_count+=1
else:
one_count+=1
# print('len_str', len(str))
# print('zero_count', zero_count)
# print('one_count', one_count)
if len(string)%2==0:
if zero_count%2==0:
return n
else:
return False
else:
return n
for i in range(q):
n=int(input())
s_list=[]
for j in range(n):
s=input()
s_list.append(s)
check_lists=[]
check_lists.append(s_list)
print(create_lists(check_lists,n))
```
No
| 64,096 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
for i in range(int(input())):
n=int(input())
odd=0
for j in range(n):
s=input()
one=s.count('1')
zero=s.count('0')
if(len(s)%2==0):
if(one%2==1 or zero%2==1):
odd+=1
else:
oddd=1
if(odd%2==0):
print(n)
else:
if(oddd==1):
print(n)
else:
print(n-1)
```
No
| 64,097 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i ∈ [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.
You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
* choose four integer numbers x, a, y, b such that 1 ≤ x, y ≤ n and 1 ≤ a ≤ |s_x| and 1 ≤ b ≤ |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively),
* swap (exchange) the characters s_x[a] and s_y[b].
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer Q (1 ≤ Q ≤ 50) — the number of test cases.
The first line on each test case contains single integer n (1 ≤ n ≤ 50) — the number of binary strings you have.
Next n lines contains binary strings s_1, s_2, ..., s_n — one per line. It's guaranteed that 1 ≤ |s_i| ≤ 50 and all strings constist of zeroes and/or ones.
Output
Print Q integers — one per test case. The i-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the i-th test case.
Example
Input
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
Output
1
2
2
2
Note
In the first test case, s_1 is palindrome, so the answer is 1.
In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make s_1 = 0110, s_2 = 111111 and s_3 = 010000.
In the third test case we can make both strings palindromic. For example, s_1 = 11011 and s_2 = 100001.
In the last test case s_2 is palindrome and you can make s_1 palindrome, for example, by swapping s_1[2] and s_1[3].
Submitted Solution:
```
# Author Name: Ajay Meena
# Codeforce : https://codeforces.com/profile/majay1638
import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout
# -------------- INPUT FUNCTIONS ------------------
def get_ints_in_variables(): return map(
int, sys.stdin.readline().strip().split())
def get_int(): return int(sys.stdin.readline())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
# -------- SOME CUSTOMIZED FUNCTIONS-----------
def myceil(x, y): return (x + y - 1) // y
# -------------- SOLUTION FUNCTION ------------------
def Solution(arr, n):
# Write Your Code Here
odd = 0
even = 0
for s in arr:
if len(s) % 2:
odd += 1
else:
even += 1
if odd or even % 2 == 0:
print(n)
else:
print(n-1)
def main():
# Take input Here and Call solution function
for _ in range(get_int()):
n = get_int()
arr = [get_string() for _ in range(n)]
Solution(arr, n)
# calling main Function
if __name__ == '__main__':
main()
```
No
| 64,098 | [
0.2890625,
-0.2012939453125,
-0.5458984375,
0.018524169921875,
-0.53564453125,
-0.3193359375,
0.1807861328125,
0.0654296875,
0.178466796875,
1.05078125,
0.69970703125,
-0.3134765625,
0.2059326171875,
-0.9296875,
-0.51123046875,
-0.15771484375,
-0.6923828125,
-0.8759765625,
-0.667... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
n = int(input())
a = list(input())
b = list(input())
dp = []
sm1 = 0
sm2 = 0
for i in range(n - 1):
if a[i] != b[i] :
if a[i] != a[i + 1 ] and a[i + 1 ] != b[i + 1 ]:
sm1 += abs(i - i + 1 )
a[i] , a[i + 1 ] = a[i + 1 ] , a[i]
else:
continue
else:
continue
#print(a)
#print(b)
#print(sm1)
for i in range(n):
if a[i] != b[i]:
sm2 +=1
print(sm1 + sm2)
#print(a)
#print(b)
```
| 64,833 | [
0.125732421875,
0.0960693359375,
-0.1773681640625,
0.1199951171875,
-0.6015625,
-0.744140625,
0.029754638671875,
-0.31982421875,
-0.115966796875,
0.93798828125,
0.666015625,
-0.398193359375,
0.08343505859375,
-0.921875,
-0.440673828125,
0.173828125,
-0.437744140625,
-0.6162109375,
... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
# import sys
# sys.stdin = open("F:\\Scripts\\input","r")
# sys.stdout = open("F:\\Scripts\\output","w")
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
n , = I()
s = input()
t = input()
ans = 0
i = 0
while i < n:
if i < n-1 and (s[i]+s[i+1] == '01' and t[i]+t[i+1] == '10' or (s[i]+s[i+1] == '10' and t[i]+t[i+1] == '01')):
ans += 1
i += 2
elif s[i] != t[i]:
ans += 1
i += 1
else:
i += 1
print(ans)
```
| 64,834 | [
0.0810546875,
0.118896484375,
-0.18017578125,
0.1298828125,
-0.619140625,
-0.70947265625,
-0.05291748046875,
-0.299560546875,
-0.1732177734375,
0.955078125,
0.6884765625,
-0.422119140625,
0.06640625,
-0.9501953125,
-0.42919921875,
0.198486328125,
-0.400390625,
-0.6025390625,
-0.2... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
def calculate():
#INPUT
n = int(input())
a = list(input())
b = list(input())
#BASE CASES
if a==b:
print(0)
exit()
cost = 0
for i in range(n):
if a[i]==b[i]:
continue
if i==n-1:
cost+=1
continue
if a[i]=='1' and a[i+1]=='0':
if a[i+1]==b[i+1] and b[i+1]=='0':
cost+=1
a[i]='0'
else:
cost+=1
a[i] = '0'
a[i+1] = '1'
elif a[i]=='0' and a[i+1]=='1':
if a[i+1]==b[i+1] and b[i+1]=='1':
cost+=1
a[i] = '1'
else:
cost+=1
a[i] = '1'
a[i+1] = '0'
else:
cost+=1
if a[i]=='1':
a[i] = '0'
else:
a[i] = '1'
return cost
ans = calculate()
print(ans)
```
| 64,835 | [
0.128173828125,
0.12371826171875,
-0.1837158203125,
0.11407470703125,
-0.5751953125,
-0.79541015625,
0.053558349609375,
-0.28955078125,
-0.1201171875,
0.921875,
0.7451171875,
-0.422119140625,
0.07110595703125,
-0.93212890625,
-0.4306640625,
0.1629638671875,
-0.45166015625,
-0.61523... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
n=int(input())
s=input()
t=input()
cost=0
i=0
while i<n:
if(s[i]!=t[i]):
if ((i+1<n) and s[i]!=s[i+1] and s[i+1]!=t[i+1]):
i+=2
else:
i+=1
cost+=1
else:
i+=1
print(cost)
```
| 64,836 | [
0.10064697265625,
0.12225341796875,
-0.20166015625,
0.134765625,
-0.58056640625,
-0.74365234375,
0.0303802490234375,
-0.30029296875,
-0.12939453125,
0.9345703125,
0.6787109375,
-0.427001953125,
0.06951904296875,
-0.955078125,
-0.429443359375,
0.162841796875,
-0.424072265625,
-0.617... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
n=int(input())
a=input()
b=input()
cost=0
i=0
while i<n:
if a[i]==b[i]:
i+=1
continue
if i==n-1:
cost+=1
else:
if a[i+1]!=b[i+1]:
if b[i]==a[i+1] and b[i+1]==a[i]:
cost+=1
i+=2
continue
else:
cost+=1
else:
cost+=1
i+=1
print(cost)
```
| 64,837 | [
0.1317138671875,
0.11309814453125,
-0.2012939453125,
0.10296630859375,
-0.57861328125,
-0.73974609375,
0.049957275390625,
-0.312255859375,
-0.1136474609375,
0.9072265625,
0.68994140625,
-0.399658203125,
0.05426025390625,
-0.93701171875,
-0.427978515625,
0.16455078125,
-0.429931640625... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
input()
c=p=0
for x,y in zip(input(),input()):
f=y!=x!=p
c+=f
p=y*f
print(c)
```
| 64,838 | [
0.1729736328125,
0.09344482421875,
-0.092529296875,
0.174072265625,
-0.5859375,
-0.74658203125,
0.0187530517578125,
-0.308837890625,
-0.1337890625,
0.984375,
0.69921875,
-0.412109375,
0.10015869140625,
-0.927734375,
-0.427734375,
0.2064208984375,
-0.39697265625,
-0.61962890625,
-... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
n = int(input())
a = input()
b = input()
l1 = []
l2 = []
b1 = []
for i in range(n):
b1.append(a[i])
i = 0
while i < n-1:
if a[i] == "0" and a[i+1] == "1" and\
b[i] == "1" and b[i+1] == "0":
l1.append(i)
b1[i] = "1"
b1[i+1] = "0"
i = i + 1
elif a[i] == "1" and a[i+1] == "0" and\
b[i] == "0" and b[i+1] == "1" :
l2.append(i)
b1[i] = "0"
b1[i+1] = "1"
i = i + 1
i = i + 1
t = set(l2)
s = 0
# print(l1,l2)
ans = len(l1)+len(l2)
# print(b1)
# print(b1,b)
for i in range(n):
if b[i] != b1[i]:
ans = ans + 1
print(ans)
```
| 64,839 | [
0.1177978515625,
0.08978271484375,
-0.1549072265625,
0.10614013671875,
-0.60595703125,
-0.748046875,
0.039947509765625,
-0.316162109375,
-0.1099853515625,
0.90478515625,
0.69287109375,
-0.425537109375,
0.0501708984375,
-0.95458984375,
-0.462890625,
0.145751953125,
-0.448486328125,
... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Tags: dp, greedy, strings
Correct Solution:
```
n=int(input())
a=input()
b=input()
i=0
res=0
while i<n:
if a[i]!=b[i]:
if i+1<n and a[i]==b[i+1] and a[i+1]==b[i]:
i+=1
res+=1
i+=1
print(res)
```
| 64,840 | [
0.13330078125,
0.10693359375,
-0.1922607421875,
0.10223388671875,
-0.59521484375,
-0.7470703125,
0.0264129638671875,
-0.314697265625,
-0.1260986328125,
0.93798828125,
0.6923828125,
-0.408447265625,
0.07373046875,
-0.9521484375,
-0.44677734375,
0.15478515625,
-0.449462890625,
-0.602... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
def solve():
n = int(input())
a = input()
b = input()
ans1 = 0
ans = 0 if a[0] == b[0] else 1
for i in range(1, n):
nans = ans + (a[i] != b[i])
if a[i] != a[i - 1] and a[i] == b[i - 1] and a[i - 1] == b[i]:
nans = min(nans, ans1 + 1)
ans, ans1 = nans, ans
print(ans)
solve()
```
Yes
| 64,841 | [
0.1824951171875,
0.09893798828125,
-0.1904296875,
0.132568359375,
-0.62109375,
-0.5810546875,
-0.0958251953125,
-0.1729736328125,
-0.2044677734375,
0.94873046875,
0.7724609375,
-0.367919921875,
0.0029430389404296875,
-1.1220703125,
-0.5908203125,
0.14453125,
-0.52734375,
-0.5800781... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
# only adjacent swaps are beneficial, otherwise just flip bits
nBits = int(input())
bits = input()
target = input()
cost = 0
i = 0
while i < nBits - 1:
if bits[i] == target[i]:
i += 1
continue
if bits[i+1] != target[i+1] and bits[i] != bits[i+1]:
#bits[i] = target[i]
#bits[i+1] = target[i+1]
i += 2
else:
#bits[i] = target[i]
i += 1
cost += 1
# Handle final digit
if i == nBits-1 and bits[nBits-1] != target[nBits-1]:
cost += 1
print(cost)
```
Yes
| 64,842 | [
0.1517333984375,
0.11834716796875,
-0.276123046875,
0.101318359375,
-0.6611328125,
-0.6962890625,
-0.032928466796875,
-0.1510009765625,
-0.2420654296875,
0.9736328125,
0.67578125,
-0.39453125,
0.0401611328125,
-1.0595703125,
-0.42578125,
0.15478515625,
-0.474365234375,
-0.674804687... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
if __name__ == '__main__':
n = int(input().strip())
s1 = [int(__) for __ in list(input().strip())]
s2 = [int(__) for __ in list(input().strip())]
ans = 0
for i in range(n):
if s1[i] != s2[i]:
if i + 1 < n and s1[i + 1] != s2[i + 1] and s1[i] != s1[i + 1]:
ans += 1
s1[i] = 1 - s1[i]
s1[i + 1] = 1 - s1[i + 1]
else:
ans += 1
s1[i] = 1 - s1[i]
print(ans)
```
Yes
| 64,843 | [
0.12457275390625,
0.163818359375,
-0.2122802734375,
0.10113525390625,
-0.63818359375,
-0.6064453125,
-0.0211944580078125,
-0.1766357421875,
-0.204345703125,
0.9677734375,
0.779296875,
-0.423828125,
0.045989990234375,
-1.07421875,
-0.4990234375,
0.1597900390625,
-0.52392578125,
-0.6... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
n=int(input())
ax=input()
a=list(map(int, ax))
bx=input()
b=list(map(int,bx))
cost=0
for i in range(0, n-1):
if (a[i]!=b[i]):
if (a[i+1]==b[i] and a[i]==b[i+1]):
a[i+1], a[i]=a[i], a[i+1]
cost+=1
else:
a[i]=b[i]
cost+=1
if (a[-1]!=b[-1]):
a[-1]=b[-1]
cost+=1
print (cost)
```
Yes
| 64,844 | [
0.1444091796875,
0.1693115234375,
-0.2381591796875,
0.09295654296875,
-0.58984375,
-0.64208984375,
-0.051300048828125,
-0.197509765625,
-0.242431640625,
0.94287109375,
0.77099609375,
-0.348388671875,
0.052581787109375,
-1.0927734375,
-0.47021484375,
0.1292724609375,
-0.544921875,
-... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
n=int(input())
s=input()
s2=input()
l=[0]*(n+1)
if s[0]==s2[0]:
l[0]=0
else:
l[0]=1
for i in range(1,n):
if s[i]==s2[i]:
l[i]=l[i-1]
else:
l[i]=l[i-1]+1
if s[i-1]!=s2[i-1]:
l[i]=min(l[i-2]+1,l[i])
print(l[n-1])
```
No
| 64,845 | [
0.1431884765625,
0.1715087890625,
-0.229736328125,
0.08935546875,
-0.63427734375,
-0.61474609375,
-0.0679931640625,
-0.1875,
-0.21240234375,
0.9677734375,
0.7431640625,
-0.379638671875,
0.00458526611328125,
-1.0517578125,
-0.4951171875,
0.1319580078125,
-0.52685546875,
-0.608886718... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
s1=input()
s2=input()
cost=0
j=0
i=0
if len(s1)==len(s2):
while i<len(s1):
if s1[i]==s2[i]:
cost=cost+(j//2)+(j%2)
j=0
i=i+1
continue
elif s1[i]!=s2[i]:
j=j+1
i=i+1
if j!=0:
cost=cost+(j//2)+(j%2)
print(cost)
```
No
| 64,846 | [
0.1512451171875,
0.1898193359375,
-0.2261962890625,
0.108642578125,
-0.61962890625,
-0.59130859375,
-0.07244873046875,
-0.2037353515625,
-0.1915283203125,
0.96044921875,
0.73681640625,
-0.397705078125,
0.0117950439453125,
-1.0654296875,
-0.466064453125,
0.1396484375,
-0.50146484375,
... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
n=int(input())
s1=input()
s2=input()
l1=list(s1)
l2=list(s2)
c1=0
for i in range(n):
if(l1[i]!=l2[i]):
c1+=1
l3=[]
c2=0
for i in range(n):
if(l1[i]!=l2[i]):
l3.append(i+1)
j=1
while(j<len(l3)):
if((l3[j]-l3[j-1])==1):
c2+=1
if(len(l3)>2):
j+=2
else:
break
else:
c2+=2
j+=1
print(min(c1,c2))
```
No
| 64,847 | [
0.13525390625,
0.1588134765625,
-0.2225341796875,
0.08856201171875,
-0.63427734375,
-0.6220703125,
-0.0635986328125,
-0.1864013671875,
-0.1904296875,
0.9677734375,
0.7314453125,
-0.37841796875,
0.025238037109375,
-1.0654296875,
-0.4697265625,
0.12841796875,
-0.52734375,
-0.63378906... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two binary strings a and b of the same length. You can perform the following two operations on the string a:
* Swap any two bits at indices i and j respectively (1 ≤ i, j ≤ n), the cost of this operation is |i - j|, that is, the absolute difference between i and j.
* Select any arbitrary index i (1 ≤ i ≤ n) and flip (change 0 to 1 or 1 to 0) the bit at this index. The cost of this operation is 1.
Find the minimum cost to make the string a equal to b. It is not allowed to modify string b.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^6) — the length of the strings a and b.
The second and third lines contain strings a and b respectively.
Both strings a and b have length n and contain only '0' and '1'.
Output
Output the minimum cost to make the string a equal to b.
Examples
Input
3
100
001
Output
2
Input
4
0101
0011
Output
1
Note
In the first example, one of the optimal solutions is to flip index 1 and index 3, the string a changes in the following way: "100" → "000" → "001". The cost is 1 + 1 = 2.
The other optimal solution is to swap bits and indices 1 and 3, the string a changes then "100" → "001", the cost is also |1 - 3| = 2.
In the second example, the optimal solution is to swap bits at indices 2 and 3, the string a changes as "0101" → "0011". The cost is |2 - 3| = 1.
Submitted Solution:
```
n = int(input())
a = list(input())
b = list(input())
ans = 0
for i in range(n - 1):
x1 = a[i]
x2 = b[i]
y1 = a[i + 1]
if x1 == x2:
continue
ans += 1
a[i + 1] = x1
a[i] = y1
print(ans)
```
No
| 64,848 | [
0.1630859375,
0.1282958984375,
-0.20654296875,
0.08367919921875,
-0.6044921875,
-0.630859375,
-0.05712890625,
-0.178466796875,
-0.1898193359375,
0.962890625,
0.75390625,
-0.3740234375,
0.043304443359375,
-1.0732421875,
-0.47900390625,
0.1351318359375,
-0.525390625,
-0.6298828125,
... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
import sys
from array import array # noqa: F401
from itertools import permutations
def input():
return sys.stdin.buffer.readline().decode('utf-8')
class RollingHash(object):
__slots__ = ['hash1', 'hash2']
from random import randint
N = 10**5 + 100
BASE = randint(1000, 9999)
MOD1, MOD2 = 1000000007, 998244353
BASE1, BASE2 = array('i', [1]) * N, array('i', [1]) * N
for i in range(1, N):
BASE1[i] = BASE1[i - 1] * BASE % MOD1
BASE2[i] = BASE2[i - 1] * BASE % MOD2
def __init__(self, source: list):
self.hash1 = hash1 = array('i', [0] + source)
self.hash2 = hash2 = array('i', [0] + source)
for i in range(1, len(source) + 1):
hash1[i] = (hash1[i] + hash1[i - 1] * self.BASE) % self.MOD1
hash2[i] = (hash2[i] + hash2[i - 1] * self.BASE) % self.MOD2
def get(self, l: int, r: int):
return (
(self.hash1[r] - self.hash1[l] * self.BASE1[r - l]) % self.MOD1,
(self.hash2[r] - self.hash2[l] * self.BASE2[r - l]) % self.MOD2
)
s = [list(map(ord, input().rstrip())) for _ in range(3)]
rh = [RollingHash(a) for a in s]
n = [len(s[i]) for i in range(3)]
def solve(x, y, st=0):
for i in range(st, n[x]):
slen = min(n[x] - i, n[y])
if rh[x].get(i, i + slen) == rh[y].get(0, slen):
return i
return n[x]
def solve2(x, y, z, i=0):
for j in range(n[y]):
slen = min(n[y] - j, n[z])
if rh[y].get(j, j + slen) == rh[y].get(0, slen):
slen2 = min(n[x] - i - j, n[z])
if slen2 <= 0 or rh[x].get(0, slen2):
return i + j
return solve(x, z, n[y]) if n[y] < n[x] else n[y]
ans = sum(n)
for a in permutations(range(3)):
i = solve(a[0], a[1])
j = solve(a[1], a[2]) + i
if j < n[a[0]]:
j = solve(a[0], a[2], j)
ans = min(ans, max(n[a[0]], i + n[a[1]], j + n[a[2]]))
print(ans)
```
| 65,138 | [
0.348388671875,
-0.1571044921875,
0.27685546875,
0.08319091796875,
-0.6806640625,
-0.411865234375,
-0.05816650390625,
-0.1021728515625,
0.36328125,
0.8427734375,
0.607421875,
-0.01507568359375,
-0.0155029296875,
-0.884765625,
-0.64794921875,
0.07061767578125,
-0.5947265625,
-0.4785... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 998244353
"""
find front and back overlaps between each string
012
021
102
120
201
210
"""
from itertools import permutations as pp
def solve():
S = [listStr(), listStr(), listStr()]
#s = 'ab'*50000
#s = list(s)
#S = [s]*3
overlap = [[0 for j in range(3)] for i in range(3)]
def calc(x,y):
s1, s2 = S[x], S[y]
if not s1: return [0]
if not s2: return [0]
#tail of s1 with head of s2
N = len(s1)
T = [0]*N
pos = 1
cnd = 0
while pos < N:
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
break
pos += 1
pos = 0
cnd = 0
N2 = len(s2)
T2 = [0]*N2
#p is T, smatch is T2, s is s2, ps is s1, pi is cnd
while pos < N2:
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
break
if cnd == N:
cnd = T[cnd-1]
pos += 1
return T2
for ii in range(3):
for jj in range(ii+1,3):
X = calc(ii,jj)
if max(X) == len(S[ii]):
S[ii] = ''
y = 0
else:
y = X[-1]
overlap[ii][jj] = y
X = calc(jj,ii)
if max(X) == len(S[jj]):
S[jj] = ''
y = 0
else:
y = X[-1]
overlap[jj][ii] = y
ops = pp('012')
ans = len(S[0])+len(S[1])+len(S[2])
best = ans
for op in ops:
tmp = ans
tmp -= overlap[int(op[0])][int(op[1])]
tmp -= overlap[int(op[1])][int(op[2])]
best = min(best,tmp)
return best
#for _ in range(getInt()):
print(solve())
#solve()
#print(time.time()-start_time)á
```
| 65,139 | [
0.216796875,
-0.07635498046875,
0.294921875,
0.414306640625,
-0.71533203125,
-0.299072265625,
0.204833984375,
-0.04656982421875,
0.6103515625,
0.92236328125,
0.59912109375,
-0.0143280029296875,
0.048065185546875,
-1,
-0.7041015625,
-0.0526123046875,
-0.474609375,
-0.6611328125,
-... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
def p(a,b):
s,m,c,j=b+'#'+a,0,0,0;p=[0]*len(s)
for i in range(1,len(s)):
while j and s[i]!=s[j]: j=p[j-1]
if s[i]==s[j]: j+=1
p[i]=j
if j==len(b): return a
return a[:len(a)-p[-1]]+b
s=[input() for _ in ' ']
print(min(len(p(s[x[0]],p(s[x[1]],s[x[2]]))) for x in __import__('itertools').permutations([0,1,2])))
```
| 65,140 | [
0.36474609375,
-0.08673095703125,
0.357177734375,
0.26416015625,
-0.68408203125,
-0.3583984375,
-0.01605224609375,
-0.12188720703125,
0.376220703125,
0.8798828125,
0.6201171875,
-0.067138671875,
-0.01116943359375,
-0.927734375,
-0.75830078125,
0.00670623779296875,
-0.499755859375,
... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
def listStr():
return list(input().strip())
from itertools import permutations as pp
def solve():
S = [listStr(), listStr(), listStr()]
overlap = [[0 for j in range(3)] for i in range(3)]
def calc(x,y):
s1, s2 = S[x], S[y]
if not s1: return [0]
if not s2: return [0]
N = len(s1)
T = [0]*N
pos = 1
cnd = 0
while pos < N:
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
break
pos += 1
pos = 0
cnd = 0
N2 = len(s2)
T2 = [0]*N2
while pos < N2:
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
break
if cnd == N:
cnd = T[cnd-1]
pos += 1
return T2
for ii in range(3):
for jj in range(ii+1,3):
X = calc(ii,jj)
if max(X) == len(S[ii]):
S[ii] = ''
y = 0
else:
y = X[-1]
overlap[ii][jj] = y
X = calc(jj,ii)
if max(X) == len(S[jj]):
S[jj] = ''
y = 0
else:
y = X[-1]
overlap[jj][ii] = y
ops = pp('012')
ans = len(S[0])+len(S[1])+len(S[2])
best = ans
for op in ops:
tmp = ans
tmp -= overlap[int(op[0])][int(op[1])]
tmp -= overlap[int(op[1])][int(op[2])]
best = min(best,tmp)
return best
print(solve())
```
| 65,141 | [
0.229248046875,
-0.135009765625,
0.34423828125,
0.259521484375,
-0.552734375,
-0.29248046875,
0.07171630859375,
-0.150146484375,
0.5029296875,
0.80419921875,
0.57080078125,
-0.07269287109375,
-0.06768798828125,
-0.9169921875,
-0.66748046875,
-0.01236724853515625,
-0.456787109375,
-... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
import sys, math
input = sys.stdin.readline
def listStr():
return list(input().strip())
from itertools import permutations as pp
def solve():
S = [listStr(), listStr(), listStr()]
overlap = [[0 for j in range(3)] for i in range(3)]
def calc(x,y):
s1, s2 = S[x], S[y]
if not s1: return [0]
if not s2: return [0]
N = len(s1)
T = [0]*N
pos = 1
cnd = 0
while pos < N:
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s1[pos] == s1[cnd]:
cnd += 1
T[pos] = cnd
break
pos += 1
pos = 0
cnd = 0
N2 = len(s2)
T2 = [0]*N2
while pos < N2:
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
else:
while cnd:
cnd = T[cnd-1]
if s2[pos] == s1[cnd]:
cnd += 1
T2[pos] = cnd
break
if cnd == N:
cnd = T[cnd-1]
pos += 1
return T2
for ii in range(3):
for jj in range(ii+1,3):
X = calc(ii,jj)
if max(X) == len(S[ii]):
S[ii] = ''
y = 0
else:
y = X[-1]
overlap[ii][jj] = y
X = calc(jj,ii)
if max(X) == len(S[jj]):
S[jj] = ''
y = 0
else:
y = X[-1]
overlap[jj][ii] = y
ops = pp('012')
ans = len(S[0])+len(S[1])+len(S[2])
best = ans
for op in ops:
tmp = ans
tmp -= overlap[int(op[0])][int(op[1])]
tmp -= overlap[int(op[1])][int(op[2])]
best = min(best,tmp)
return best
print(solve())
```
| 65,142 | [
0.2261962890625,
-0.11468505859375,
0.350341796875,
0.244140625,
-0.5703125,
-0.3212890625,
0.039581298828125,
-0.107421875,
0.501953125,
0.81640625,
0.5537109375,
-0.0870361328125,
-0.08660888671875,
-0.86328125,
-0.6982421875,
0.01038360595703125,
-0.440673828125,
-0.421875,
-0... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Tags: hashing, strings
Correct Solution:
```
from itertools import permutations
from sys import stdin
from functools import lru_cache
input = stdin.readline
s1, s2, s3 = input().strip(), input().strip(), input().strip()
@lru_cache(None)
def findPattern(s):
#print(s)
slen = len(s)
p = [0]*slen
ci = 0
for i in range(1, slen):
if(s[ci] == s[i]):
ci+=1
p[i] = ci
else:
while(ci):
ci = p[ci-1]
if(s[ci]==s[i]):
ci+=1
p[i] = ci
break
return tuple(p)
@lru_cache(None)
def searchPattern(s, ps, p):
#print(s, ps, p)
slen = len(s)
plen = len(p)
smatch = [0]*slen
pi = 0
for i in range(slen):
if(s[i] == ps[pi]):
pi+=1
smatch[i] = pi
else:
while(pi):
pi = p[pi-1]
if(s[i] == ps[pi]):
pi+=1
smatch[i] = pi
break
if(pi==plen):
pi = p[pi-1]
return smatch
def calcLength(a, b, c):
pb = findPattern(b)
amatchb = searchPattern(a, b, pb)
if(len(b) not in amatchb): ab = a[:max(0, len(a) - amatchb[-1])] + b
else: ab = a
pc = findPattern(c)
abmatchc = searchPattern(ab, c, pc)
return max(0, len(ab) - abmatchc[-1]) + len(c) if len(c) not in abmatchc else len(ab)
ans = float('inf')
s = [s1, s2, s3]
for ai, bi, ci in permutations(list(range(3)), 3):
ans = min(ans, calcLength(s[ai], s[bi], s[ci]))
print(ans)
```
| 65,143 | [
0.352783203125,
0.01380157470703125,
0.28564453125,
0.1351318359375,
-0.8271484375,
-0.458251953125,
-0.0416259765625,
-0.1884765625,
0.50927734375,
0.89404296875,
0.646484375,
0.045013427734375,
0.00522613525390625,
-0.92529296875,
-0.77734375,
-0.08441162109375,
-0.5234375,
-0.47... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Submitted Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 998244353
"""
find front and back overlaps between each string
012
021
102
120
201
210
"""
from itertools import permutations as pp
def solve():
S = [listStr(), listStr(), listStr()]
overlap = [[0 for j in range(3)] for i in range(3)]
def calc(x,y):
s1, s2 = S[y], S[x]
if not s1 or not s2:
return 0, False
#tail of s1 with head of s2
N = len(s1)
T = [0]*N
pos = 1
cnd = 0
flag = False
while pos < N:
if s1[pos] == s2[cnd]:
cnd += 1
#print(T,pos)
T[pos] = cnd
pos += 1
if cnd == len(s2):
flag = True
cnd = T[cnd-1]
else:
if cnd:
cnd = T[cnd-1]
else:
T[pos] = 0
pos += 1
return T[-1], flag
for ii in range(3):
for jj in range(ii+1,3):
X, flag = calc(ii,jj)
if flag:
S[ii] = ''
for k in range(3):
overlap[ii][k] = overlap[k][ii] = 0
else:
overlap[ii][jj] = X
X, flag = calc(jj,ii)
if flag:
S[jj] = ''
for k in range(3):
overlap[jj][k] = overlap[k][jj] = 0
else:
overlap[jj][ii] = X
ops = pp('012')
ans = len(S[0])+len(S[1])+len(S[2])
best = ans
for op in ops:
tmp = ans
tmp -= overlap[int(op[0])][int(op[1])]
tmp -= overlap[int(op[1])][int(op[2])]
best = min(best,tmp)
return best
#for _ in range(getInt()):
print(solve())
#solve()
#print(time.time()-start_time)á
```
No
| 65,144 | [
0.297119140625,
-0.01467132568359375,
0.1798095703125,
0.417724609375,
-0.787109375,
-0.1646728515625,
0.235595703125,
0.047454833984375,
0.55712890625,
0.8974609375,
0.56298828125,
-0.06976318359375,
-0.00539398193359375,
-1.0654296875,
-0.7431640625,
-0.1666259765625,
-0.4924316406... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Submitted Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 998244353
"""
find front and back overlaps between each string
012
021
102
120
201
210
"""
from itertools import permutations as pp
def solve():
S = [listStr(), listStr(), listStr()]
#s = 'ab'*50000
#s = list(s)
#S = [s]*3
overlap = [[0 for j in range(3)] for i in range(3)]
def calc(x,y):
s1, s2 = S[y], S[x]
if not s1 or not s2:
return 0, False
if s1 == s2:
return len(s1), True
#tail of s1 with head of s2
s1 = ['?']+s1
N = len(s1)
T = [0]*N
pos = 1
cnd = 0
flag = False
while pos < N:
if s1[pos] == s2[cnd]:
cnd += 1
#print(T,pos)
T[pos] = cnd
pos += 1
if cnd == len(s2):
flag = True
cnd = T[cnd-1]
else:
if cnd:
cnd = T[cnd-1]
else:
T[pos] = 0
pos += 1
return T[-1], flag
for ii in range(3):
for jj in range(ii+1,3):
X, flag = calc(ii,jj)
if flag:
S[ii] = ''
for k in range(3):
overlap[ii][k] = overlap[k][ii] = 0
else:
overlap[ii][jj] = X
X, flag = calc(jj,ii)
if flag:
S[jj] = ''
for k in range(3):
overlap[jj][k] = overlap[k][jj] = 0
else:
overlap[jj][ii] = X
ops = pp('012')
ans = len(S[0])+len(S[1])+len(S[2])
best = ans
if S[0][:60] == 'svssvssvssssssssvsvssvsvssvvsssvvssvsvsvvsssvvsvvssvvvssssvs':
print(S[0][:60],S[1][:60],S[2][:60])
for op in ops:
tmp = ans
tmp -= overlap[int(op[0])][int(op[1])]
tmp -= overlap[int(op[1])][int(op[2])]
best = min(best,tmp)
return best
#for _ in range(getInt()):
print(solve())
#solve()
#print(time.time()-start_time)á
```
No
| 65,145 | [
0.297119140625,
-0.01467132568359375,
0.1798095703125,
0.417724609375,
-0.787109375,
-0.1646728515625,
0.235595703125,
0.047454833984375,
0.55712890625,
0.8974609375,
0.56298828125,
-0.06976318359375,
-0.00539398193359375,
-1.0654296875,
-0.7431640625,
-0.1666259765625,
-0.4924316406... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Submitted Solution:
```
def prefixFunction(s):
n = len(s)
pi = [0 for _ in range(n)]
for i in range(1,n):
j = pi[i-1]
while(j>0 and s[i]!=s[j]):
j = pi[j-1]
if(s[i]==s[j]): j+=1
pi[i] = j
return pi
def solve(a,b,c):
ab = b+a
bc = c+b
prfab = prefixFunction(ab)
prfbc = prefixFunction(bc)
int1 = min(min(len(a),len(b)),prfab[-1])
int2 = min(min(len(c),len(b)),prfbc[-1])
return len(a)+len(b)-int1+len(c)+len(b)-int2-len(b)
a = input()
b = input()
c = input()
ans = 1000000009
ans = min(ans,solve(a,b,c))
ans = min(ans,solve(a,c,b))
ans = min(ans,solve(b,a,c))
ans = min(ans,solve(b,c,a))
ans = min(ans,solve(c,a,b))
ans = min(ans,solve(c,b,a))
print(ans)
```
No
| 65,146 | [
0.434814453125,
-0.1156005859375,
0.317138671875,
0.294921875,
-0.89306640625,
-0.17431640625,
-0.039520263671875,
-0.00601959228515625,
0.1776123046875,
0.7802734375,
0.61376953125,
-0.04840087890625,
-0.052703857421875,
-0.95849609375,
-0.8427734375,
0.104736328125,
-0.46923828125,... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
Input
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
Output
Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.
Examples
Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11
Submitted Solution:
```
a = input()
b = input()
c = input()
continha = len(a)+len(b)+len(c)
cont1 = 0
cont2 = 0
cont3 = 0
cont4 = 0
cont5 = 0
cont6 = 0
cont7 = 0
cont8 = 0
cont9 = 0
cont10 = 0
cont11 = 0
cont12 = 0
# abc
for i in range(1,len(b)):
if(a.endswith(b[0:i])):
cont1 = i
for i in range(1,len(c)):
if(b.endswith(c[0:i])):
cont2 = i
# acb
for i in range(1,len(c)):
if(a.endswith(c[0:i])):
cont3 = i
for i in range(1,len(b)):
if(c.endswith(b[0:i])):
cont4 = i
# bca
for i in range(1,len(c)):
if(b.endswith(c[0:i])):
cont5 = i
for i in range(1,len(a)):
if(c.endswith(a[0:i])):
cont6 = i
# bac
for i in range(1,len(a)):
if(b.endswith(a[0:i])):
cont7 = i
for i in range(1,len(c)):
if(a.endswith(c[0:i])):
cont8 = i
# cba
for i in range(1,len(b)):
if(c.endswith(b[0:i])):
cont9 = i
for i in range(1,len(a)):
if(b.endswith(a[0:i])):
cont10 = i
#cab
for i in range(1,len(a)):
if(c.endswith(a[0:i])):
cont11 = i
for i in range(1,len(b)):
if(a.endswith(b[0:i])):
cont12 = i
conta1 = continha-cont1-cont2
conta2 = continha-cont3-cont4
conta3 = continha-cont5-cont6
conta4 = continha-cont7-cont8
conta5 = continha-cont9-cont10
conta6 = continha-cont11-cont12
#print(conta1,conta2,conta3,conta4,conta5,conta6)
valor = min(conta1,conta2,conta3,conta4,conta5,conta6)
if(valor > 100):
print(100)
elif(valor > 200):
print(1000)
else:
print(valor)
```
No
| 65,147 | [
0.398193359375,
-0.0701904296875,
0.2392578125,
0.1737060546875,
-0.77001953125,
-0.1871337890625,
0.10675048828125,
0.033538818359375,
0.27880859375,
0.8623046875,
0.52783203125,
-0.06939697265625,
-0.08013916015625,
-1.0439453125,
-0.671875,
-0.1348876953125,
-0.52783203125,
-0.3... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
s, t = input(), input()
n, m = len(t), len(s) + 1
d = 1000000007
g = [1] * m
f = k = 0
for i in range(1, m):
if s[i - n:i] == t: k = i
if k: f = (f + g[k - n]) % d
g[i] += (g[i - 1] + f) % d
print(f)
```
| 65,176 | [
0.19580078125,
0.1268310546875,
0.08953857421875,
0.140869140625,
-0.537109375,
-0.476318359375,
-0.32958984375,
-0.312744140625,
0.33056640625,
0.88134765625,
0.2357177734375,
-0.154296875,
0.0242767333984375,
-0.71923828125,
-0.457763671875,
0.1324462890625,
-0.380615234375,
-0.4... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
s = input()
t = input()
n = len(s)
m = len(t)
t = t + '$' + s
p = [0] * (n + m + 1)
k = 0
for i in range(1, n + m + 1):
while k > 0 and t[k] != t[i]:
k = p[k - 1]
if t[k] == t[i]:
k += 1
p[i] = k
ans = [0] * n
sums = [0] * (n + 1)
curs = 0
was = False
j = 0
MOD = 10 ** 9 + 7
for i in range(n):
if p[i + m + 1] == m:
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = (sums[i - 1] + ans[i]) % MOD
print(sum(ans) % MOD)
```
| 65,177 | [
0.23388671875,
0.1015625,
0.1025390625,
0.08935546875,
-0.53125,
-0.498046875,
-0.34814453125,
-0.30419921875,
0.32666015625,
0.86328125,
0.2164306640625,
-0.10430908203125,
-0.009857177734375,
-0.744140625,
-0.4560546875,
0.15869140625,
-0.38134765625,
-0.52685546875,
-0.4069824... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
MOD = 10 ** 9 + 7
s, t = input(), input()
p = t + '#' + s
z = [0] * len(p)
l = r = 0
for i in range(1, len(p)):
if i <= r:
z[i] = min(r - i + 1, z[i - l])
while i + z[i] < len(p) and p[z[i]] == p[i + z[i]]:
z[i] += 1
if i + z[i] - 1 > r:
l, r = i, i + z[i] - 1
f = [0] * (len(p) + 1)
fsum = [0] * (len(p) + 1)
fsum2 = [0] * (len(p) + 1)
for i in range(len(p) - 1, len(t), -1):
if z[i] == len(t):
f[i] = fsum2[i + z[i]] + len(p) - i - z[i] + 1
else:
f[i] = f[i + 1]
fsum[i] = (fsum[i + 1] + f[i]) % MOD
fsum2[i] = (fsum2[i + 1] + fsum[i]) % MOD
print(fsum[len(t) + 1])
```
| 65,178 | [
0.200927734375,
0.10015869140625,
0.128173828125,
0.142578125,
-0.5546875,
-0.487548828125,
-0.37109375,
-0.295166015625,
0.249267578125,
0.89892578125,
0.2042236328125,
-0.1591796875,
-0.0136871337890625,
-0.66650390625,
-0.4658203125,
0.165771484375,
-0.346435546875,
-0.533691406... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
s, t = input(), input()
n, m = len(t), len(s)
d = 10 ** 9 + 7
p = [0] * (n + 1)
i, j = 0, 1
while j < n:
if t[i] == t[j]:
j += 1
i += 1
p[j] = i
elif i: i = p[i]
else: j += 1
i = j = 0
f = [0] * (m + 1)
while j < m:
if t[i] == s[j]:
i += 1
j += 1
if i == n:
i = p[i]
f[j - n] = 1
elif i: i = p[i]
else: j += 1
s = [0] * (m + 1)
k = m
for j in range(m - 1, -1, -1):
if f[j]: k = j
if k < m:
f[j] = (f[j + 1] + s[k + n] + m - k - n + 1) % d
s[j] = (s[j + 1] + f[j]) % d
print(f[0])
```
| 65,179 | [
0.2064208984375,
0.1861572265625,
0.0728759765625,
0.1307373046875,
-0.53515625,
-0.48828125,
-0.38427734375,
-0.289306640625,
0.323974609375,
0.8759765625,
0.218505859375,
-0.10498046875,
-0.038116455078125,
-0.71728515625,
-0.42578125,
0.156494140625,
-0.384765625,
-0.5107421875,... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
def main():
s, t = input(), input()
n, m = len(s), len(t)
t = '$'.join((t, s))
p = [0]
k = 0
for i in range(1, n + m + 1):
while k and t[k] != t[i]:
k = p[k - 1]
if t[k] == t[i]:
k += 1
p.append(k)
ans = [0] * n
sums = [0] * (n + 1)
curs = 0
was = False
j = 0
MOD = 1000000007
for i in range(n):
if p[i + m + 1] == m:
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = (sums[i - 1] + ans[i]) % MOD
print(sum(ans) % MOD)
if __name__ == '__main__':
main()
```
| 65,180 | [
0.24560546875,
0.10125732421875,
0.08929443359375,
0.11151123046875,
-0.5478515625,
-0.489013671875,
-0.36376953125,
-0.300537109375,
0.314697265625,
0.85009765625,
0.212646484375,
-0.130126953125,
0.0140228271484375,
-0.71337890625,
-0.439697265625,
0.1865234375,
-0.4111328125,
-0... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
s, t = input(), input()
n, m = len(t), len(s) + 1
d = 1000000007
g = [1] * m
f = k = 0
for i in range(1, m):
if s[i - n:i] == t: k = i
if k: f = (f + g[k - n]) % d
g[i] += (g[i - 1] + f) % d
print(f)
# Made By Mostafa_Khaled
```
| 65,181 | [
0.1759033203125,
0.1348876953125,
0.0853271484375,
0.1531982421875,
-0.52099609375,
-0.475341796875,
-0.3486328125,
-0.290771484375,
0.311767578125,
0.87841796875,
0.24755859375,
-0.1658935546875,
0.01465606689453125,
-0.74658203125,
-0.4833984375,
0.1519775390625,
-0.405029296875,
... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
#!/usr/bin/env python
# coding=utf-8
s, t = input(), input()
n, m = len(s), len(t)
t = '$'.join((t, s))
p = [0]
k = 0
for i in range(1, n + m + 1):
while k and t[k] != t[i]:
k = p[k - 1]
if t[k] == t[i]:
k += 1
p.append(k)
ans = [0] * n
sums = [0] * (n + 1)
curs = 0
was = False
j = 0
mod = 1000000007
for i in range(n):
if p[i + m + 1] == m:
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % mod
j += 1
ans[i]= curs;
sums[i] = (sums[i - 1] + ans[i]) % mod;
print(sum(ans) % mod)
```
| 65,182 | [
0.2467041015625,
0.08990478515625,
0.070556640625,
0.1094970703125,
-0.53173828125,
-0.50830078125,
-0.305419921875,
-0.31396484375,
0.352783203125,
0.86572265625,
0.1976318359375,
-0.132080078125,
0.0272369384765625,
-0.67724609375,
-0.4755859375,
0.1519775390625,
-0.42578125,
-0.... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Tags: dp, strings
Correct Solution:
```
s, t = input(), input()
n, m = len(t), len(s) + 1
h = t + ' ' + s
d = 1000000007
f = [0] * m
s = [1] * m
i = k = 0
p = [-1] + [0] * len(h)
for j in range(1, n + m):
while i + 1 and h[i] != h[j]: i = p[i]
i += 1
p[j + 1] = i
if j > n:
j -= n
if i == n: k = j
if k: f[j] = (f[j - 1] + s[k - n]) % d
s[j] += (s[j - 1] + f[j]) % d
print(f[-1])
```
| 65,183 | [
0.2100830078125,
0.162353515625,
0.0853271484375,
0.1405029296875,
-0.537109375,
-0.49560546875,
-0.385009765625,
-0.292236328125,
0.322509765625,
0.88134765625,
0.2193603515625,
-0.128173828125,
-0.01282501220703125,
-0.7197265625,
-0.43359375,
0.1719970703125,
-0.37744140625,
-0.... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
s, t = input(), input()
n, m = len(t), len(s) + 1
h = t + ' ' + s
d = 1000000007
s = [1] * m
f = i = k = 0
p = [-1] + [0] * len(h)
for j in range(1, n + m):
while i + 1 and h[i] != h[j]: i = p[i]
i += 1
p[j + 1] = i
if j > n:
j -= n
if i == n: k = j
if k: f = (f + s[k - n]) % d
s[j] += (s[j - 1] + f) % d
print(f)
```
Yes
| 65,184 | [
0.15673828125,
0.166015625,
-0.06427001953125,
0.11614990234375,
-0.7021484375,
-0.364013671875,
-0.3330078125,
-0.1278076171875,
0.13720703125,
0.81591796875,
0.2073974609375,
-0.138671875,
-0.0740966796875,
-0.86474609375,
-0.492431640625,
0.12384033203125,
-0.332763671875,
-0.52... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
s, t = input(), input()
n, m = len(t), len(s)
p = [0] * (n + 1)
i, j = 0, 1
while j < n:
if t[i] == t[j]:
j += 1
i += 1
p[j] = i
elif i: i = p[i]
else: j += 1
i = j = 0
f = [0] * (m + 1)
while j < m:
if t[i] == s[j]:
i += 1
j += 1
if i == n:
i = p[i]
f[j - n] = 1
elif i: i = p[i]
else: j += 1
s = [0] * (m + 1)
k = m
for j in range(m - 1, -1, -1):
if f[j]: k = j
if k < m:
f[j] = f[j + 1] + s[k + n] + m - k - n + 1
s[j] = s[j + 1] + f[j]
print(f[0])
```
No
| 65,185 | [
0.1416015625,
0.18115234375,
-0.05517578125,
0.078125,
-0.69970703125,
-0.392333984375,
-0.3779296875,
-0.1197509765625,
0.1356201171875,
0.8369140625,
0.187255859375,
-0.10662841796875,
-0.053497314453125,
-0.8330078125,
-0.501953125,
0.09320068359375,
-0.34130859375,
-0.504882812... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
s, t = input(), input()
n, m = len(t), len(s)
d = 1000000007
g = [1] * m
f = k = 0
for i in range(m):
if i >= n and s[i - n:i] == t: k = i
if k: f = (f + g[k - n]) % d
g[i] += (g[i - 1] + f) % d
print(f)
```
No
| 65,186 | [
0.13916015625,
0.1202392578125,
-0.059478759765625,
0.156005859375,
-0.703125,
-0.33935546875,
-0.317626953125,
-0.1329345703125,
0.1292724609375,
0.810546875,
0.224365234375,
-0.141357421875,
-0.056121826171875,
-0.83740234375,
-0.4853515625,
0.06951904296875,
-0.34375,
-0.5151367... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
s = input()
t = input()
n = len(s)
m = len(t)
t = t + '$' + s
p = [0] * (n + m + 1)
k = 0
for i in range(1, n + m + 1):
while k > 0 and t[k] != t[i]:
k = p[k - 1]
if t[k] == t[i]:
k += 1
p[i] = k
ans = [0] * n
sums = [0] * (n + 1)
curs = 0
was = False
j = 0
MOD = 10 ** 9 + 7
for i in range(n):
if p[i + m + 1] == m:
if not was:
was = True
curs = 1
while j <= i - m:
curs = (curs + sums[j] + 1) % MOD
j += 1
ans[i] = curs
sums[i] = sums[i - 1] + ans[i]
print(sum(ans))
```
No
| 65,187 | [
0.1865234375,
0.1011962890625,
-0.058990478515625,
0.0775146484375,
-0.68310546875,
-0.375732421875,
-0.305419921875,
-0.14794921875,
0.1395263671875,
0.78271484375,
0.214599609375,
-0.09716796875,
-0.032867431640625,
-0.8662109375,
-0.5205078125,
0.11480712890625,
-0.335693359375,
... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:
* k ≥ 1
* <image>
* <image>
* <image>
* <image> t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Examples
Input
ababa
aba
Output
5
Input
welcometoroundtwohundredandeightytwo
d
Output
274201
Input
ddd
d
Output
12
Submitted Solution:
```
s = input()
t = input()
near = 0
tot = 1
totsum = 1
for i in range(len(s)-len(t),-1,-1):
if s[i:i+len(t)] == t:
near = totsum
tot += near
totsum += tot
print(tot-1)
```
No
| 65,188 | [
0.177001953125,
0.13818359375,
0.01947021484375,
0.044158935546875,
-0.71875,
-0.40966796875,
-0.356201171875,
-0.1424560546875,
0.1505126953125,
0.779296875,
0.1610107421875,
-0.13330078125,
-0.1029052734375,
-0.87890625,
-0.50537109375,
0.06646728515625,
-0.26318359375,
-0.420898... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
import sys
data = sys.stdin.read().split()
data.reverse()
def read(): return int(data.pop())
def readStr(): return data.pop()
n = read()
C = [0] + [read() for i in range(n)]
ans = ""
ch, cht = 'a', 'b'
for i in range(n, 0, -1) :
while C[i] > 0 :
for j in range(i) :
ans += ch
ch, cht = cht, ch
for j in range(1, i+1) :
C[j] -= i-j+1
print(ans)
```
| 66,087 | [
0.2259521484375,
-0.2099609375,
0.492431640625,
0.484130859375,
-0.389404296875,
-0.41796875,
-0.1822509765625,
0.051727294921875,
0.1673583984375,
0.892578125,
0.888671875,
-0.08734130859375,
-0.10296630859375,
-0.98291015625,
-0.76123046875,
-0.2364501953125,
-0.517578125,
-0.765... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n = int(input())
c = list(map(int, input().strip().split()))
subStrings = []
alt = 0
for i in range(n-1,-1,-1):
while c[i] > 0:
alt = (alt + 1) % 2
if alt == 1:
substr = 'a'*(i+1)
subStrings.append(substr)
else:
substr = 'b'*(i+1)
subStrings.append(substr)
for j in range(i,-1,-1):
c[j] -= i-j + 1
print(''.join(subStrings))
```
| 66,088 | [
0.2325439453125,
-0.1925048828125,
0.462646484375,
0.46484375,
-0.393310546875,
-0.444580078125,
-0.156494140625,
0.094482421875,
0.193115234375,
0.86181640625,
0.92138671875,
-0.06671142578125,
-0.1021728515625,
-1.0107421875,
-0.77783203125,
-0.255126953125,
-0.53759765625,
-0.76... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
for i in range(n-1, 0, -1):
for j in range(i-1, -1, -1):
arr[j] -= arr[i]*(i-j+1)
s = "a"
def rev(c):
if c == "a":
return "b"
else:
return "a"
for i in range(n):
for j in range(arr[i]):
s += rev(s[-1])*(i+1)
print(s[1:])
```
| 66,089 | [
0.234130859375,
-0.193603515625,
0.45703125,
0.46435546875,
-0.3740234375,
-0.436767578125,
-0.171142578125,
0.0704345703125,
0.191162109375,
0.85595703125,
0.92578125,
-0.08062744140625,
-0.11297607421875,
-1.0068359375,
-0.77734375,
-0.2423095703125,
-0.54150390625,
-0.748046875,... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
import sys
n = int(input())
c = list(map(int, input().split()))
cc = ord('a')
ans = ""
cur = n - 1
if 1:
cur = n - 1
while cur >= 0:
while c[cur] > 0:
if chr(cc) > 'z':
cc = ord('a')
ans += chr(cc) * (cur + 1)
c[cur] -= 1
for i in range(cur):
c[i] -= (cur - i + 1)
cc += 1
cur -= 1
print(ans)
```
| 66,090 | [
0.2457275390625,
-0.199951171875,
0.462646484375,
0.477783203125,
-0.385986328125,
-0.42041015625,
-0.1634521484375,
0.0880126953125,
0.1864013671875,
0.86865234375,
0.9150390625,
-0.076904296875,
-0.0997314453125,
-1.017578125,
-0.7861328125,
-0.24072265625,
-0.52294921875,
-0.775... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
cur = 0
k = 0
for i in range(n - 1, -1, -1):
k += cur
a[i] -= k
k += a[i]
cur += a[i]
b = [[0, 0]] * n;
for i in range(n - 1, -1, -1):
b[i] = [ a[i], i + 1 ]
b.sort()
b.reverse()
"""
for i in range(n):
for j in range(0, n - 1):
if b[j][0] < b[j + 1][0]:
temp = b[j]
b[j] = b[j + 1]
b[j + 1] = temp
"""
pos = 0
cur = 'a'
ans = [' '] * n;
while b[0][0] > 0:
for i in range(pos, pos + b[0][1]):
ans[i] = cur
if cur == 'a':
cur = 'b'
else:
cur = 'a'
pos += b[0][1]
b[0][0] -= 1;
for j in range(0, n - 1):
if b[j][0] < b[j + 1][0]:
temp = b[j]
b[j] = b[j + 1]
b[j + 1] = temp
for i in range(0, n):
print(ans[i], end = '')
```
| 66,091 | [
0.24072265625,
-0.1993408203125,
0.45654296875,
0.463623046875,
-0.38623046875,
-0.43603515625,
-0.16259765625,
0.07476806640625,
0.19873046875,
0.86767578125,
0.9287109375,
-0.07080078125,
-0.10369873046875,
-0.9970703125,
-0.7734375,
-0.2384033203125,
-0.54248046875,
-0.756347656... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
c = 'a'
s = ""
for i in range(n - 1, -1, -1):
for j in range(arr[i]):
for k in range(0, i + 1):
s += c
if(c == 'a'):
c = 'b'
else:
c = 'a'
for l in range(i - 1, -1, -1):
arr[l] -= (i - l + 1) * arr[i]
print(s)
```
| 66,092 | [
0.237548828125,
-0.1949462890625,
0.4609375,
0.464111328125,
-0.37548828125,
-0.43701171875,
-0.17236328125,
0.0654296875,
0.191162109375,
0.8564453125,
0.931640625,
-0.080810546875,
-0.11212158203125,
-1.0078125,
-0.7861328125,
-0.2406005859375,
-0.54833984375,
-0.74609375,
-0.4... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n=int(input())
c=[0]+list(map(int,input().split()))
ans=[]
for i in reversed(range(1,n+1)):
for x in ans:
c[i]-=x-i+1
ans+=[i]*c[i]
a=True
for x in ans:
print(end=('a' if a else 'b')*x)
a=not a
```
| 66,093 | [
0.2308349609375,
-0.19091796875,
0.4638671875,
0.46728515625,
-0.376708984375,
-0.4453125,
-0.1593017578125,
0.090576171875,
0.1953125,
0.8623046875,
0.91943359375,
-0.07080078125,
-0.11102294921875,
-1.015625,
-0.7685546875,
-0.2442626953125,
-0.5400390625,
-0.7646484375,
-0.477... | 0 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Tags: *special, constructive algorithms
Correct Solution:
```
n = int(input())
a = [0] + list(map(int, input().split()))
for i in range(n, 0, -1):
for j in range(i + 1, n + 1):
a[i] -= a[j] * (j - i + 1)
x = 'b'
for i in range(1, n + 1):
for _ in range(a[i]):
for __ in range(i):
print(x, end = '')
x = chr(ord(x) ^ 1)
```
| 66,094 | [
0.233154296875,
-0.1884765625,
0.458740234375,
0.470458984375,
-0.377197265625,
-0.44873046875,
-0.1588134765625,
0.09039306640625,
0.197509765625,
0.8671875,
0.91796875,
-0.07330322265625,
-0.11090087890625,
-1.0146484375,
-0.767578125,
-0.2381591796875,
-0.5361328125,
-0.76660156... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
cur = 'a'
s=""
for i in reversed(range(n)):
for j in range(0,a[i]):
s+=cur*(i+1)
cur=('a','b')[cur=='a']
for j in reversed(range(i)):
a[j]-=(i-j+1)*a[i]
a[i]=0
print(s)
```
Yes
| 66,095 | [
0.257568359375,
-0.1529541015625,
0.443359375,
0.41455078125,
-0.3896484375,
-0.34521484375,
-0.229248046875,
0.1240234375,
0.2186279296875,
0.8046875,
0.880859375,
-0.041961669921875,
-0.184326171875,
-1.0634765625,
-0.759765625,
-0.254150390625,
-0.5439453125,
-0.78662109375,
-... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
cur = 0
k = 0
for i in range(n - 1, -1, -1):
k += cur
a[i] -= k
cur += a[i]
b = [[0, 0]] * n;
for i in range(n - 1, -1, -1):
b[i] = [ a[i], i + 1 ]
for i in range(n):
for j in range(0, n - 1):
if b[j][0] < b[j + 1][0]:
swap(b[j][0], b[j + 1][0])
pos = 0
cur = 'a'
ans = [' '] * n;
while b[0][0] > 0:
for i in range(pos, pos + b[0][1]):
ans[i] = cur
if cur == 'a':
cur = 'b'
else:
cur = 'a'
pos += b[0][1]
b[0][0] -= 1;
for i in range(0, n - 1):
if b[j][0] < b[j + 1][0]:
swap(b[j][0], b[j + 1][0])
for i in range(0, n):
print(ans[i])
```
No
| 66,096 | [
0.259765625,
-0.130859375,
0.393310546875,
0.387451171875,
-0.4169921875,
-0.3515625,
-0.24169921875,
0.1568603515625,
0.2003173828125,
0.83544921875,
0.86572265625,
-0.035064697265625,
-0.1475830078125,
-1.0458984375,
-0.73388671875,
-0.248046875,
-0.537109375,
-0.79638671875,
-... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c1, c2, ..., cn, where n equals the length of the string s, and ci equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].
Stepan asks you to help to repair his favorite string s according to the given sequence c1, c2, ..., cn.
Input
The first line contains the integer n (1 ≤ n ≤ 2000) — the length of the Stepan's favorite string.
The second line contains the sequence of integers c1, c2, ..., cn (0 ≤ ci ≤ 2000), where ci equals the number of substrings of the string s with the length i, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists.
Output
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
Examples
Input
6
6 3 1 0 0 0
Output
kkrrrq
Input
4
4 0 0 0
Output
abcd
Note
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
cur = 0
k = 0
for i in range(n - 1, -1, -1):
k += cur
a[i] -= k
cur += a[i]
b = [[0, 0]] * n;
for i in range(n - 1, -1, -1):
b[i] = [ a[i], i + 1 ]
for i in range(n):
for j in range(n - 1):
if b[j][0] < b[j + 1][0]:
swap(b[j][0], b[j + 1][0])
pos = 0
cur = 'a'
ans = [' '] * n;
while b[0][0] > 0:
for i in range(pos, pos + b[0][1]):
ans[i] = cur
if cur == 'a':
cur = 'b'
else:
cur = 'a'
pos += b[0][1]
b[0][0] -= 1;
for i in range(n - 1):
if b[j][0] < b[j + 1][0]:
swap(b[j][0], b[j + 1][0])
for i in range(n):
print(ans[i])
```
No
| 66,097 | [
0.259765625,
-0.130859375,
0.393310546875,
0.387451171875,
-0.4169921875,
-0.3515625,
-0.24169921875,
0.1568603515625,
0.2003173828125,
0.83544921875,
0.86572265625,
-0.035064697265625,
-0.1475830078125,
-1.0458984375,
-0.73388671875,
-0.248046875,
-0.537109375,
-0.79638671875,
-... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a string S of length N-1. Each character in S is `<` or `>`.
A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):
* If S_i= `<`: a_i<a_{i+1}
* If S_i= `>`: a_i>a_{i+1}
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
Constraints
* 2 \leq N \leq 5 \times 10^5
* S is a string of length N-1 consisting of `<` and `>`.
Input
Input is given from Standard Input in the following format:
S
Output
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
Examples
Input
<>>
Output
3
Input
<>>><<><<<<<>>><
Output
28
"Correct Solution:
```
s = input().replace('><','> <').split()
ans = 0
#print(list(s))
for st in s:
lt = st.count('<')
gt = len(st) - lt
if lt > gt:
gt -= 1
else:
lt -= 1
ans += (lt*lt+lt)//2 + (gt*gt+gt)//2
print(ans)
```
| 66,243 | [
0.389892578125,
0.07818603515625,
0.059814453125,
0.352783203125,
-0.433837890625,
-0.80224609375,
0.039031982421875,
0.2486572265625,
0.09228515625,
0.7705078125,
0.69384765625,
-0.073974609375,
0.128662109375,
-0.701171875,
-0.87060546875,
-0.2264404296875,
-0.90771484375,
-0.814... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a string S of length N-1. Each character in S is `<` or `>`.
A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):
* If S_i= `<`: a_i<a_{i+1}
* If S_i= `>`: a_i>a_{i+1}
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
Constraints
* 2 \leq N \leq 5 \times 10^5
* S is a string of length N-1 consisting of `<` and `>`.
Input
Input is given from Standard Input in the following format:
S
Output
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
Examples
Input
<>>
Output
3
Input
<>>><<><<<<<>>><
Output
28
"Correct Solution:
```
s=input()
sl=len(s)
l=[0 for i in range(sl+1)]
for i in range(sl):
if s[i]=='<':
l[i+1]=max(l[i]+1, l[i+1])
for i in reversed(range(sl)): # reversed(range(sl))
if s[i]=='>':
l[i]=max(l[i+1]+1, l[i])
print(sum(l))
```
| 66,248 | [
0.376953125,
0.03399658203125,
0.0382080078125,
0.297119140625,
-0.3232421875,
-0.7978515625,
0.0650634765625,
0.218994140625,
-0.040435791015625,
0.92919921875,
0.701171875,
0.01904296875,
0.2095947265625,
-0.6865234375,
-0.9052734375,
-0.24462890625,
-0.92236328125,
-0.8129882812... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
S = input()
N = len(S)
MOD = 10**9 + 7
DP = [[1] * 4 for _ in range(N + 1)]
# initialize
DP[N][3] = 1
DP[N][0] = 0
DP[N][1] = 0
DP[N][2] = 0
for i in range(N-1, -1, -1):
for j in range(3, -1, -1):
DP[i][j] = DP[i+1][j] * (3 if S[i]=='?' else 1)
if j<3:
DP[i][j] += DP[i + 1][j + 1] * (1 if (S[i]=='ABC'[j] or S[i]=='?') else 0)
DP[i][j] %= MOD
ans = DP[0][0]
print(ans)
```
| 66,290 | [
0.662109375,
0.0165557861328125,
0.056243896484375,
-0.0460205078125,
-0.3349609375,
-0.434326171875,
0.1146240234375,
0.11785888671875,
0.0584716796875,
0.6435546875,
0.53125,
-0.443359375,
-0.1331787109375,
-0.66943359375,
-0.491943359375,
-0.157958984375,
-0.5908203125,
-0.77490... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
mod = 1000000007
S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
if s in "A":
a += n
if s in "B":
ab += a
if s == "C":
abc += ab
if s in "?":
n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab
n %= mod
a %= mod
ab %= mod
abc %= mod
print(abc % mod)
```
| 66,291 | [
0.681640625,
-0.0088043212890625,
0.05340576171875,
-0.040557861328125,
-0.308837890625,
-0.453369140625,
0.1385498046875,
0.09161376953125,
0.039642333984375,
0.6748046875,
0.50634765625,
-0.429931640625,
-0.0703125,
-0.7529296875,
-0.466552734375,
-0.19970703125,
-0.560546875,
-0... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
S = input(); N = len(S)
a,b,c,d = 0,0,0,1
mod = 10**9+7
for x in reversed(S):
if x == "?":
a,b,c,d = (3*a+b)%mod, (3*b+c)%mod, (3*c+d)%mod, 3*d%mod
elif x == "A": a = (a+b)%mod
elif x == "B": b = (b+c)%mod
elif x == "C": c = (c+d)%mod
print(a)
```
| 66,292 | [
0.7197265625,
-0.0249786376953125,
0.03826904296875,
-0.0767822265625,
-0.336181640625,
-0.459228515625,
0.10430908203125,
0.12347412109375,
0.00981903076171875,
0.7119140625,
0.4970703125,
-0.391357421875,
-0.0927734375,
-0.66357421875,
-0.43798828125,
-0.14404296875,
-0.525390625,
... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
MOD = 10 ** 9 + 7
S = input()
N = len(S)
dp = [[0] * 4 for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):
for j in range(4):
s = S[i]
m1 = 3 if s == '?' else 1
m2 = 1 if j != 0 and (s == '?' or s == 'ABC'[j - 1]) else 0
dp[i + 1][j] = (m1 * dp[i][j] + m2 * dp[i][j - 1]) % MOD
print(dp[N][3])
```
| 66,293 | [
0.673828125,
-0.0157012939453125,
0.053497314453125,
-0.038787841796875,
-0.31982421875,
-0.44189453125,
0.1370849609375,
0.1187744140625,
0.0282745361328125,
0.6669921875,
0.5166015625,
-0.443115234375,
-0.11322021484375,
-0.67041015625,
-0.47509765625,
-0.1558837890625,
-0.57958984... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
s = input()
n = len(s)
mod = 10**9+7
cnt = 0
dp = [[0 for i in range(4)] for j in range(n+1)]
dp[0][0] = 1
for i in range(1,n+1):
if s[i-1] in "A?":
dp[i][1] += dp[i-1][0]
if s[i-1] in "B?":
dp[i][2] += dp[i-1][1]
if s[i-1] in "C?":
dp[i][3] += dp[i-1][2]
for j in range(4):
if s[i-1] == "?":
dp[i][j] += dp[i-1][j]*3
else:
dp[i][j] += dp[i-1][j]
dp[i][j] %= mod
print(dp[-1][-1])
```
| 66,294 | [
0.64404296875,
0.024871826171875,
0.039398193359375,
-0.041168212890625,
-0.286376953125,
-0.431884765625,
0.1651611328125,
0.11358642578125,
0.04791259765625,
0.6416015625,
0.5234375,
-0.44287109375,
-0.135986328125,
-0.72119140625,
-0.5087890625,
-0.19580078125,
-0.5986328125,
-0... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
s=input()
mod=10**9+7
a=0
ab=0
abc=0
q=1
for i in s:
if i=="?":
abc*=3
abc+=ab
ab*=3
ab+=a
a*=3
a+=q
q*=3
q%=mod
elif i=="A":
a+=q
elif i=="B":
ab+=a
else:
abc+=ab
abc%=mod
ab%=mod
a%=mod
print(abc)
```
| 66,295 | [
0.68798828125,
0.01424407958984375,
0.0170440673828125,
-0.08612060546875,
-0.3369140625,
-0.46533203125,
0.11163330078125,
0.1142578125,
0.0340576171875,
0.66455078125,
0.460693359375,
-0.385498046875,
-0.1055908203125,
-0.70263671875,
-0.458740234375,
-0.1920166015625,
-0.562988281... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
s=input()
mod=10**9+7
n,a,b,c=1,0,0,0
for i in s:
if i=="A":
a+=n
if i=="B":
b+=a
if i=="C":
c+=b
if i=="?":
n,a,b,c=3*n,3*a+n,3*b+a,3*c+b
n%=mod
a%=mod
b%=mod
c%=mod
print(c)
```
| 66,296 | [
0.67431640625,
0.01090240478515625,
0.009979248046875,
-0.0362548828125,
-0.34130859375,
-0.4453125,
0.121337890625,
0.11260986328125,
0.03497314453125,
0.65625,
0.52880859375,
-0.416748046875,
-0.11370849609375,
-0.70263671875,
-0.465087890625,
-0.162841796875,
-0.5517578125,
-0.8... | 0 |
Provide a correct Python 3 solution for this coding contest problem.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
"Correct Solution:
```
# D - We Love ABC
S = list(input())
MOD = 10**9 + 7
n_possible = 1 # その場所以前の文字列としてあり得るパターン
A = 0 # その場所以前の文字列におけるA数
B = 0
C = 0
for s in S:
if s == "A":
A += n_possible
elif s == "B":
B += A
elif s == "C":
C += B
else:
# X = 3*X + Y
# この場所の?を (AorABorABCの一部に含めない場合) + (AorABorABCの一部に含める場合)
C = 3*C + B
B = 3*B + A
A = 3*A + n_possible
n_possible *= 3
n_possible %= MOD
A %= MOD
B %= MOD
C %= MOD
print(C)
```
| 66,297 | [
0.69677734375,
0.048004150390625,
-0.0008673667907714844,
-0.0626220703125,
-0.294921875,
-0.45849609375,
0.11865234375,
0.1744384765625,
0.1473388671875,
0.716796875,
0.53369140625,
-0.38818359375,
-0.083251953125,
-0.685546875,
-0.55322265625,
-0.2213134765625,
-0.486572265625,
-... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
s = input()
mod = 10 ** 9 + 7
l = len(s)
dp = [[0] * 4 for _ in range(l + 1)]
dp[0][0] = 1
for i, e in enumerate(s, 1):
# do not use in ABC
for j in range(4):
if e == "?":
dp[i][j] += dp[i-1][j] * 3
else:
dp[i][j] += dp[i-1][j]
# use in ABC
if e == "A" or e == "?":
dp[i][1] += dp[i-1][0]
if e == "B" or e == "?":
dp[i][2] += dp[i-1][1]
if e == "C" or e == "?":
dp[i][3] += dp[i-1][2]
for j in range(4):
dp[i][j] %= mod
ans = dp[l][3]
print(ans)
```
Yes
| 66,298 | [
0.6328125,
-0.0226898193359375,
0.0005679130554199219,
0.040557861328125,
-0.360595703125,
-0.426513671875,
0.1015625,
0.236328125,
0.08306884765625,
0.75634765625,
0.572265625,
-0.403076171875,
-0.156005859375,
-0.61181640625,
-0.5078125,
-0.219970703125,
-0.4697265625,
-0.8037109... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
def index(c):return ord(c)-ord('A')+1
s=' '+input()
m=10**9+7
dp=[[0]*4 for _ in range(len(s))]
dp[0][0]=1
for i in range(1,len(s)):
for j in range(4):
dp[i][j]=dp[i-1][j]
if s[i]=='?':
dp[i][0]=dp[i-1][0]*3%m
for j in range(1,4):
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j]*3)%m
else:
k=index(s[i])
dp[i][k]=(dp[i-1][k-1]+dp[i-1][k])%m
print(dp[-1][3])
```
Yes
| 66,299 | [
0.6015625,
-0.03692626953125,
0.007083892822265625,
0.044586181640625,
-0.347412109375,
-0.366943359375,
0.123779296875,
0.2474365234375,
0.09698486328125,
0.74365234375,
0.59619140625,
-0.478759765625,
-0.2490234375,
-0.62646484375,
-0.475341796875,
-0.19189453125,
-0.5009765625,
... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
M = 10**9 + 7
N, A, AB, ABC = 1, 0, 0, 0
idx = {'A': 1, 'B': 2, 'C': 3}
for c in input():
if c == '?':
N, A, AB, ABC = (N * 3) % M, (A * 3 + N) % M, (AB * 3 + A) % M, (ABC * 3 + AB) % M
elif c == 'A':
A += N
elif c == 'B':
AB += A
else:
ABC += AB
print(ABC % M)
```
Yes
| 66,300 | [
0.6142578125,
0.0043487548828125,
0.0404052734375,
0.035308837890625,
-0.3193359375,
-0.40966796875,
0.06658935546875,
0.2232666015625,
0.071044921875,
0.75048828125,
0.59765625,
-0.403564453125,
-0.1898193359375,
-0.60498046875,
-0.495361328125,
-0.239501953125,
-0.49267578125,
-0... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
MOD = 10**9 + 7
S = input()
res = 0
dp0 = 1 # ''
dp1 = 0 # 'a'
dp2 = 0 # 'ab'
dp3 = 0 # 'abc'
for c in S:
if c == 'A':
dp1 += dp0
elif c == 'B':
dp2 += dp1
elif c == 'C':
dp3 += dp2
else:
dp0,dp1,dp2,dp3 = dp0*3,dp1*3+dp0,dp2*3+dp1,dp3*3+dp2
dp0 %= MOD
dp1 %= MOD
dp2 %= MOD
dp3 %= MOD
print(dp3)
```
Yes
| 66,301 | [
0.62744140625,
0.009521484375,
0.025634765625,
0.0193939208984375,
-0.3359375,
-0.400146484375,
0.0848388671875,
0.23193359375,
0.060211181640625,
0.73779296875,
0.55810546875,
-0.41015625,
-0.1865234375,
-0.62060546875,
-0.4990234375,
-0.2486572265625,
-0.47802734375,
-0.763183593... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
ABC = 'ABC'
S = input()
dp = [0, 0, 0, 0]
dp[3] = 1
for i in range(len(S))[::-1]:
c = S[i]
if c == '?':
dp[0] = 3 * dp[0] + dp[1]
dp[1] = 3 * dp[1] + dp[2]
dp[2] = 3 * dp[2] + dp[3]
dp[3] = 3 * dp[3]
elif c == 'A':
dp[0] = 1 * dp[0] + dp[1]
dp[1] = 1 * dp[1]
dp[2] = 1 * dp[2]
dp[3] = 1 * dp[3]
elif c == 'B':
dp[0] = 1 * dp[0]
dp[1] = 1 * dp[1] + dp[2]
dp[2] = 1 * dp[2]
dp[3] = 1 * dp[3]
elif c == 'C':
dp[0] = 1 * dp[0]
dp[1] = 1 * dp[1]
dp[2] = 1 * dp[2] + dp[3]
dp[3] = 1 * dp[3]
print(dp[0] % 1000000007)
```
No
| 66,302 | [
0.62109375,
-0.0008792877197265625,
0.02276611328125,
0.0244903564453125,
-0.32421875,
-0.3955078125,
0.0843505859375,
0.2384033203125,
0.072021484375,
0.732421875,
0.59130859375,
-0.41796875,
-0.220458984375,
-0.58984375,
-0.5107421875,
-0.261474609375,
-0.501953125,
-0.7592773437... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
s=input()
a=[1,0,0,0]
for i in range(len(s)):
c=s[i]
if c=="A":
a[1]+=a[0]
elif c=="B":
a[2]+=a[1]
elif c=="C":
a[3]+=a[2]
elif c=="?":
tmp=[a[i] for i in range(4)]
a[0]=tmp[0]*3
for j in range(3):
a[j+1]=tmp[j+1]*3+tmp[j]
print(a[3]%1000000007)
```
No
| 66,303 | [
0.6474609375,
-0.00555419921875,
0.0010805130004882812,
0.021484375,
-0.332763671875,
-0.37841796875,
0.10595703125,
0.236083984375,
0.06195068359375,
0.72802734375,
0.58642578125,
-0.405029296875,
-0.2132568359375,
-0.60546875,
-0.51123046875,
-0.269287109375,
-0.49951171875,
-0.7... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
S = ''
def we_love_abc()->int:
global S
ABC = 'ABC'
dp = [0, 0, 0, 0]
dp[3] = 1
for c in reversed(S):
if c == '?':
m, m1 = 3, 3
else:
m, m1 = 1, 1
for j in range(3):
m2 = 1 if c == '?' or c == ABC[j] else 0
dp[j] = m1 * dp[j] + m2 * dp[j+1]
dp[3] = m * dp[3]
return dp[0] % 1000000007
if __name__ == "__main__":
S = input()
ans = we_love_abc() # S)
print(ans)
```
No
| 66,304 | [
0.6494140625,
-0.0509033203125,
-0.00921630859375,
-0.0116119384765625,
-0.315673828125,
-0.46337890625,
0.11431884765625,
0.26171875,
0.055023193359375,
0.6748046875,
0.5537109375,
-0.373291015625,
-0.24169921875,
-0.54052734375,
-0.5205078125,
-0.305419921875,
-0.479248046875,
-0... | 0 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
* 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
* T_i = `A` (T_i is the i-th character of T from the beginning.)
* T_j = `B`
* T_k = `C`
For example, when T = `ABCBC`, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is `A`, `B`, `C` or `?`.
Let Q be the number of occurrences of `?` in S. We can make 3^Q strings by replacing each occurrence of `?` in S with `A`, `B` or `C`. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.
Constraints
* 3 ≤ |S| ≤ 10^5
* Each character of S is `A`, `B`, `C` or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.
Examples
Input
A??C
Output
8
Input
ABCBC
Output
3
Input
????C?????B??????A???????
Output
979596887
Submitted Solution:
```
mod = 10**9+7
def pow(n,p):
res=1
while p >0:
if p%2==0:
n = n**2 % mod
p //= 2
else:
res = res * n % mod
p -= 1
return res % mod
def main():
s=input()
res = 0
ca = 0
cc = s.count("C")
cw = 0
aw = s.count("?")
for c in s:
if c == "A":
ca += 1
elif c == "C":
cc -= 1
elif c == "B":
res += ca*cc*pow(3,aw)+ca*(aw-cw)*pow(3,aw-1)+cw*cc*pow(3,aw-1)+cw*(aw-cw)*pow(3,aw-2)
res %= mod
elif c == "?":
res += ca*cc*pow(3,aw-1)+ca*(aw-cw-1)*pow(3,aw-2)+cw*cc*pow(3,aw-2)+cw*(aw-cw-1)*pow(3,aw-3)
res %= mod
cw += 1
print(res)
main()
```
No
| 66,305 | [
0.6650390625,
-0.0166778564453125,
0.0153045654296875,
0.0194244384765625,
-0.34912109375,
-0.382568359375,
0.075927734375,
0.229736328125,
0.061187744140625,
0.771484375,
0.56201171875,
-0.417236328125,
-0.1766357421875,
-0.62158203125,
-0.43359375,
-0.2119140625,
-0.4658203125,
-... | 0 |
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 lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.
Calculate the minimum number of operations to delete the whole string s.
Input
The first line contains one integer n (1 ≤ n ≤ 500) — the length of string s.
The second line contains the string s (|s| = n) consisting of lowercase Latin letters.
Output
Output a single integer — the minimal number of operation to delete string s.
Examples
Input
5
abaca
Output
3
Input
8
abcddcba
Output
4
Submitted Solution:
```
def compress(s): # abbccbdaab -> abcbdab -> abbab (+2) -> abab
s1 = ''.join(s[i] for i in range(len(s)) if i == 0 or s[i] != s[i-1])
counts = {}
for i, c in enumerate(s1):
if c not in counts: counts[c] = 0
counts[c] += 1
sout = ''
ops = 0
for c in s1:
if counts[c] == 1:
ops += 1
elif not sout or c != sout[-1]:
sout += c
return sout, ops
def get_indices(s):
out = {}
for i, c in enumerate(s):
if c not in out: out[c] = set()
out[c].add(i)
return out
def _solve(s, indices, i, j, memo):
if i > j: return 0
if i == j: return 1
if (i,j) in memo: return memo[(i,j)]
memo[(i,j)] = _solve(s, indices, i, j-1, memo)+1
for k in indices[s[j]]:
if i <= k < j:
memo[(i,j)] = min(memo[(i,j)], _solve(s, indices, i, k-1, memo) + _solve(s, indices, k+1, j-1, memo)+1)
return memo[(i,j)]
def solve(s):
s, ops = compress(s)
indices = get_indices(s)
ops += _solve(s, indices, 0, len(s)-1, {})
return ops
def main():
from sys import stdin
s = stdin.readline().strip()
out = solve(s)
print('{}'.format(out))
if __name__ == '__main__':
main()
```
No
| 66,492 | [
0.16064453125,
0.070556640625,
0.317626953125,
0.281494140625,
-0.5029296875,
-0.423583984375,
-0.2210693359375,
-0.08990478515625,
0.293701171875,
0.95166015625,
0.7373046875,
-0.201416015625,
-0.264892578125,
-1.1240234375,
-0.48388671875,
-0.112548828125,
-0.57373046875,
-0.6103... | 0 |
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 lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.
Calculate the minimum number of operations to delete the whole string s.
Input
The first line contains one integer n (1 ≤ n ≤ 500) — the length of string s.
The second line contains the string s (|s| = n) consisting of lowercase Latin letters.
Output
Output a single integer — the minimal number of operation to delete string s.
Examples
Input
5
abaca
Output
3
Input
8
abcddcba
Output
4
Submitted Solution:
```
n = int(input())
s = input()
cnt = 0
bayan = set()
for i in range(n):
for j in range(n):
if s[i] == s[j] and s[i] not in bayan and len(set(s[i:j])) == 2:
cnt += 1
bayan.add(s[i])
print(len(set(s)) * cnt)
```
No
| 66,493 | [
0.018096923828125,
0.02398681640625,
0.2218017578125,
0.194580078125,
-0.400146484375,
-0.375244140625,
-0.0791015625,
-0.0860595703125,
0.36181640625,
0.8984375,
0.92431640625,
-0.177001953125,
-0.537109375,
-1.0859375,
-0.51220703125,
-0.299072265625,
-0.67041015625,
-0.599121093... | 0 |
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 lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.
Calculate the minimum number of operations to delete the whole string s.
Input
The first line contains one integer n (1 ≤ n ≤ 500) — the length of string s.
The second line contains the string s (|s| = n) consisting of lowercase Latin letters.
Output
Output a single integer — the minimal number of operation to delete string s.
Examples
Input
5
abaca
Output
3
Input
8
abcddcba
Output
4
Submitted Solution:
```
input()
s = input()
# print(min(list(s)))
def f(x):
if len(x) == 1:
return 1
if len(x) == 0:
return 0
return sum([f(c) for c in x.split( min(list(x)) ) ]) + 1
print(f(s))
```
No
| 66,494 | [
0.08544921875,
0.0697021484375,
0.25732421875,
0.265625,
-0.5107421875,
-0.31689453125,
-0.139404296875,
-0.1207275390625,
0.274169921875,
0.90966796875,
0.83837890625,
-0.150390625,
-0.37255859375,
-1.015625,
-0.634765625,
-0.215576171875,
-0.62109375,
-0.681640625,
-0.407958984... | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.