message stringlengths 2 23.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 97 109k | cluster float64 0 0 | __index_level_0__ int64 194 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ k ≤ 17) — the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
from collections import deque
import bisect
n, k = map(int,input().split())
s = input()
n=200000
k=15
s = "?"*200000
effectj = [[] for i in range(1<<k)]
for state in range(1<<k):
for j in range(k):
if (1<<j) & state>0: effectj[state].append(j)
maxconseq = [[0 for j in range(k)] for i in range(n+1)]
for i in range(n):
if s[i]=='?':
for j in range(k):
maxconseq[i][j] = maxconseq[i-1][j] + 1
else:
j = ord(s[i])-97
maxconseq[i][j] = maxconseq[i-1][j] + 1
#print(maxconseq)
def judge(needed):
minstate = [2147483647]*(1<<k)
minstate[0] = -1
effect = [[] for i in range(k)]
for i in range(n):
for j in range(k):
if maxconseq[i][j]>=needed: effect[j].append(i)
for state in range(1<<k):
for j in effectj[state]:
if effect[j] and minstate[state-(1<<j)] + needed > effect[j][-1]: continue
loc = bisect.bisect_left(effect[j], minstate[state-(1<<j)] + needed)
minstate[state] = min(minstate[state], effect[j][loc])
if minstate[-1]<n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,182 | 0 | 182,364 |
No | output | 1 | 91,182 | 0 | 182,365 |
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. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ k ≤ 17) — the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
from collections import deque
import bisect
#n, k = map(int,input().split())
#s = input()
n=200000
k=12
s = "?"*200000
maxconseq = [[0 for j in range(k)] for i in range(n+1)]
for i in range(n):
if s[i]=='?':
for j in range(k):
maxconseq[i][j] = maxconseq[i-1][j] + 1
else:
j = ord(s[i])-97
maxconseq[i][j] = maxconseq[i-1][j] + 1
#print(maxconseq)
def judge(needed):
minstate = [2147483647]*(1<<k)
minstate[0] = -1
effect = [[] for i in range(k)]
for i in range(n):
for j in range(k):
if maxconseq[i][j]>=needed: effect[j].append(i)
for state in range(1<<k):
for j in range(k):
if (1<<j) & state==0: continue
loc = bisect.bisect_left(effect[j], minstate[state-(1<<j)] + needed)
if loc == len(effect[j]): continue
minstate[state] = min(minstate[state], effect[j][loc])
if minstate[-1]<n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,183 | 0 | 182,366 |
No | output | 1 | 91,183 | 0 | 182,367 |
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. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ k ≤ 17) — the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 1
if accu>=needed:
index = i + needed
effect[j][i] = index
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,184 | 0 | 182,368 |
No | output | 1 | 91,184 | 0 | 182,369 |
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. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ k ≤ 17) — the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
from collections import deque
import bisect
#n, k = map(int,input().split())
#s = input()
n=200000
k=10
s = "?"*200000
maxconseq = [[0 for j in range(k)] for i in range(n+1)]
for i in range(n):
if s[i]=='?':
for j in range(k):
maxconseq[i][j] = maxconseq[i-1][j] + 1
else:
j = ord(s[i])-97
maxconseq[i][j] = maxconseq[i-1][j] + 1
#print(maxconseq)
def judge(needed):
minstate = [2147483647]*(1<<k)
minstate[0] = -1
effect = [[] for i in range(k)]
for i in range(n):
for j in range(k):
if maxconseq[i][j]>=needed: effect[j].append(i)
for state in range(1<<k):
for j in range(k):
if (1<<j) & state==0: continue
loc = bisect.bisect_left(effect[j], minstate[state-(1<<j)] + needed)
if loc == len(effect[j]): continue
minstate[state] = min(minstate[state], effect[j][loc])
if minstate[-1]<n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,185 | 0 | 182,370 |
No | output | 1 | 91,185 | 0 | 182,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,311 | 0 | 182,622 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
l=sorted([int(i) for i in input().split()],reverse=True)
l1=[0]*n
l1[0]=l[0]
s=l[0]
for i in range(1,n):
k=min(l[i],l1[i-1]-1)
s+=k
l1[i]=k
if(k==0):
break
print(s)
``` | output | 1 | 91,311 | 0 | 182,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,312 | 0 | 182,624 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
l=[int(x) for x in input().split()]
l.sort()
ans=0
for i in range(n):
val=l[i]
while (val>0) and val in l[:i]:
val-=1
l[i]=val
ans+=val
print(ans)
``` | output | 1 | 91,312 | 0 | 182,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,313 | 0 | 182,626 |
Tags: greedy, sortings
Correct Solution:
```
"""
B. Making a String
codeforces.com
"""
_, nums = input(), [int(x) for x in input().split(' ')]
nums.sort()
nums.reverse()
result = nums[0]
prev = nums[0]
for n in nums[1:]:
n = prev-1 if n >= prev else n
result += n
prev = n
if n == 0:
break
print (result)
``` | output | 1 | 91,313 | 0 | 182,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,314 | 0 | 182,628 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
l = list(map(int,input().split()))
l.sort(reverse = 1)
for i in range(1, n):
if l[i] >= l[i-1]:
l[i] = l[i-1]-1
if l[i] < 0: l[i] = 0
print(sum(l))
``` | output | 1 | 91,314 | 0 | 182,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,315 | 0 | 182,630 |
Tags: greedy, sortings
Correct Solution:
```
def main():
n = int(input())
a = [int(x) for x in input().split()]
print(solver(a))
def solver(L):
L.sort(reverse = True)
total = 0
current = L[0] + 1
for i in range(len(L)):
if L[i] >= current:
current -= 1
if current <= 0:
break
else:
current = L[i]
total += current
return total
#L1 = [2, 5, 5]
#print(solver(L1))
#L1 = [1, 1, 2, 2, 2]
#print(solver(L1))
main()
``` | output | 1 | 91,315 | 0 | 182,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,316 | 0 | 182,632 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
ai = list(map(int,input().split()))
ai.sort()
ai.reverse()
last_num = ai[0]+1
ans = 0
for num in ai:
if last_num == 0:
break
if num >= last_num:
last_num -= 1
else:
last_num = num
ans += last_num
print(ans)
``` | output | 1 | 91,316 | 0 | 182,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,317 | 0 | 182,634 |
Tags: greedy, sortings
Correct Solution:
```
N = int(input())
ans = 0
used = set()
for c in sorted([int(x) for x in input().split()], reverse=True):
while c > 0 and c in used:
c -= 1
ans += c
used.add(c)
print(ans)
``` | output | 1 | 91,317 | 0 | 182,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc". | instruction | 0 | 91,318 | 0 | 182,636 |
Tags: greedy, sortings
Correct Solution:
```
def make_s(lst):
while True:
lst.sort()
for i in range(len(lst) - 1):
if lst[i] == lst[i + 1]:
lst[i] -= 1
break
else:
break
return sum([x for x in lst if x > 0])
n = int(input())
a = [int(i) for i in input().split()]
print(make_s(a))
``` | output | 1 | 91,318 | 0 | 182,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
n = int(input())
data = list(map(int, input().split()))
answer = 0
for i in range(n):
if data.count(data[i]) == 1:
answer += data[i]
else:
while data[i] > 0:
if data.count(data[i]) == 1:
answer += data[i]
break
else:
data[i] -= 1
print(answer)
``` | instruction | 0 | 91,319 | 0 | 182,638 |
Yes | output | 1 | 91,319 | 0 | 182,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = [A[0]]
for i in range(1, n):
if A[i] < ans[-1]:
ans.append(A[i])
else:
ans.append(max(0, ans[-1] - 1))
print(sum(ans))
``` | instruction | 0 | 91,320 | 0 | 182,640 |
Yes | output | 1 | 91,320 | 0 | 182,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)[-1::-1]
for i in range(1, n):
if l[i] >= l[i-1]:
l[i] = l[i-1] - 1
if l[i] < 0:
l[i] = 0
print(sum(l))
``` | instruction | 0 | 91,321 | 0 | 182,642 |
Yes | output | 1 | 91,321 | 0 | 182,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
def main():
n = int(input())
l = list(map(int, input().split()))
l.sort()
for i in range(1, n):
while l.index(l[i]) != i:
l[i] -= 1
answer = 0
for e in l:
if e > 0:
answer += e
print(answer)
if __name__ == "__main__":
main()
``` | instruction | 0 | 91,322 | 0 | 182,644 |
Yes | output | 1 | 91,322 | 0 | 182,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2;
if a[mid] < x:
lo = mid+1;
else:
hi = mid;
return lo;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
from bisect import bisect_left
def solve():
n = int(input())
a = sorted(list(map(int,input().split())), reverse = True)
for i in range(1, n):
if a[i] == a[i - 1]:
a[i] = a[i - 1] - 1
print(sum(a))
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | instruction | 0 | 91,323 | 0 | 182,646 |
No | output | 1 | 91,323 | 0 | 182,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
l = int(input())
text = input().split()
occ = []
text = list(map(int, text))
for item in text:
if item in occ:
occ.append(item - 1)
if item not in occ:
occ.append(item)
sum = 0
for item in occ:
sum += item
print(sum)
# 1496342017597
``` | instruction | 0 | 91,324 | 0 | 182,648 |
No | output | 1 | 91,324 | 0 | 182,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
ans = a[0]
b = a[:]
for i in range(1, n):
if a[i] == a[i - 1]:
b[i] = b[i - 1] - 1
ans += b[i]
print(ans)
``` | instruction | 0 | 91,325 | 0 | 182,650 |
No | output | 1 | 91,325 | 0 | 182,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Output
Print a single integer — the maximum length of the string that meets all the requirements.
Examples
Input
3
2 5 5
Output
11
Input
3
1 1 2
Output
3
Note
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
Submitted Solution:
```
import math
def is_prime(n):
if n==1:
return False
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
return False
return True
def inp(n):
ls=list(map(int,input().split()))
return ls
def ans(ls):
flag = [0] * t
for i in range(t):
k = 0
for j in range(t):
if i != j and ls[i] == ls[j] and flag[i] == 0:
flag[j] += 1 + k
k += 1
#print(flag)
for i in range(t):
ls[i] = ls[i] - flag[i]
t=int(input())
ls=inp(t)
while(len(ls)!=len(set(ls))):
ans(ls)
print(sum(ls))
``` | instruction | 0 | 91,326 | 0 | 182,652 |
No | output | 1 | 91,326 | 0 | 182,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,777 | 0 | 183,554 |
Tags: greedy
Correct Solution:
```
n = int(input())
st = input().rstrip()
newSt = [st[0]]
p = 1
dele = 0
cmp = True
brk = False
while True:
if p+1 > len(st):
break
if cmp:
while newSt[-1] == st[p]:
p += 1
dele += 1
if p+1 > len(st):
brk = True
break
if brk:
break
newSt.append(st[p])
p += 1
cmp = False
else:
newSt.append(st[p])
p += 1
cmp = True
if len(newSt)%2 == 1:
newSt.pop()
dele += 1
print(dele)
print(*newSt, sep='')
``` | output | 1 | 91,777 | 0 | 183,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,778 | 0 | 183,556 |
Tags: greedy
Correct Solution:
```
n=int(input())
s=input()
l=[]
for i in range(0,n):
if(len(l)%2==0 or s[i]!=l[-1]):
l.append(s[i])
#print(l)
if(len(l)%2!=0):
l.pop()
print(n-len(l))
print("".join(l))
else:
print(n-len(l))
print("".join(l))
``` | output | 1 | 91,778 | 0 | 183,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,779 | 0 | 183,558 |
Tags: greedy
Correct Solution:
```
N = int(input())
S = input()
ans = 0
res = ""
i = 0
while i < N:
if i < N - 1 and S[i] != S[i + 1]:
res += S[i]
res += S[i + 1]
i += 2
elif i < N - 2 and S[i + 1] != S[i + 2]:
res += S[i + 1]
res += S[i + 2]
ans += 1
i += 3
else:
ans += 1
i += 1
print(ans)
print(res)
``` | output | 1 | 91,779 | 0 | 183,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,780 | 0 | 183,560 |
Tags: greedy
Correct Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
n, = I()
s = input()
l = []
l.append(s[0])
f = 0
m = [s[-1]]
for i in s[1:]:
if f:
f = 0
l.append(i)
elif l[-1] != i:
l.append(i)
f = 1
f = 0
for i in s[::-1][1:]:
if f:
f = 0
m.append(i)
elif m[-1] != i:
m.append(i)
f = 1
a = len(l)
b = len(m)
m = list(reversed(m))
if a & 1:
a -= 1
l = l[:-1]
if b & 1:
b -= 1
m = m[:-1]
a = len(l)
b = len(m)
if a >= b:
print(n - a)
print(''.join(l))
else:
print(n - b)
print(''.join(m))
# v = [0]*n
# u = [0]*n
``` | output | 1 | 91,780 | 0 | 183,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,781 | 0 | 183,562 |
Tags: greedy
Correct Solution:
```
n = int(input())
s = input()
ans = []
ansn = 0
for i in range(len(s)):
if not ansn % 2 or s[i] != s[i - 1]:
ans.append(s[i])
ansn += 1
if ansn % 2:
ansn -= 1
ans.pop()
print(n - ansn)
print(''.join(ans))
``` | output | 1 | 91,781 | 0 | 183,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,782 | 0 | 183,564 |
Tags: greedy
Correct Solution:
```
n=int(input())
s=[]
i=0
j=1
a=input()
while j<n:
if a[i]==a[j]:
j+=1
else:
s+=a[i]+a[j]
i=j+1
j+=2
print(n-len(s))
print("".join(s))
``` | output | 1 | 91,782 | 0 | 183,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,783 | 0 | 183,566 |
Tags: greedy
Correct Solution:
```
num=int(input())
st=input()
res=[]
for i in range(len(st)):
if len(res)%2==0 or res[-1]!=st[i]:
res.append(st[i])
if len(res)%2==1:
res.pop()
print(num-len(res))
print(''.join(map(str,res)))
``` | output | 1 | 91,783 | 0 | 183,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3 | instruction | 0 | 91,784 | 0 | 183,568 |
Tags: greedy
Correct Solution:
```
"""
NTC here
"""
from sys import setcheckinterval,stdin
setcheckinterval(1000)
#print("Case #{}: {} {}".format(i, n + m, n * m))
iin=lambda :int(stdin.readline())
lin=lambda :list(map(int,stdin.readline().split()))
n=iin()
a=input()
ch=0
ans=[]
dlt=0
i=0
while i<n:
if ch%2==0:
#print(ch,a[i],i)
try:
if a[i]!=a[i+1]:
ch+=1
ans.append(a[i])
else:
dlt+=1
except:
if ch and ans[-1]!=a[i]:ans.append(a[i])
else:dlt+=1
else:
ch+=1
ans.append(a[i])
i+=1
if len(ans)%2:
dlt+=1
ans.pop()
print(dlt)
print(''.join(ans))
``` | output | 1 | 91,784 | 0 | 183,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
def go():
n = int(input())
a = [i for i in input()]
if n == 1:
return '1\n'
i = 1
while i < n:
if a[i] == a[i - 1]:
a[i - 1] = None
i += 1
else:
i += 2
a = ''.join(i for i in a if i is not None)
if len(a) % 2 == 1:
a = a[:-1]
return '{}\n{}'.format(n - len(a), a)
print(go())
``` | instruction | 0 | 91,785 | 0 | 183,570 |
Yes | output | 1 | 91,785 | 0 | 183,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
n = int(input())
i=0
a = input()
minus = 0
ans=''
while(i<n-1):
if a[i]==a[i+1]:
i+=1
minus+=1
else:
# print(i, a[i], a[i+1])
ans += (a[i] + a[i + 1])
i += 2
# print('--->',ans)
if i ==n-1:
ans+=a[-1]
if (n-minus)%2==1:
minus+=1
ans=ans[:-1]
if minus!=n:
print(minus)
print(ans)
else:print(minus)
``` | instruction | 0 | 91,786 | 0 | 183,572 |
Yes | output | 1 | 91,786 | 0 | 183,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
from collections import defaultdict as ddic, Counter, deque
from itertools import permutations
def ipt():
return int(input())
def iptl():
return list(map(int, input().split()))
MOD = 10 ** 9 + 7
def main():
N = ipt()
S = input()
ans = []
for x in S:
if not ans or len(ans) % 2 == 0 or ans[-1] != x:
ans.append(x)
if len(ans) % 2:
ans.pop()
ans1 = N - len(ans), "".join(ans)
print(ans1[0])
print(ans1[1])
if __name__ == '__main__':
main()
``` | instruction | 0 | 91,787 | 0 | 183,574 |
Yes | output | 1 | 91,787 | 0 | 183,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
n = int(input())
a = list(input())
i = 0
k = 0
b = []
while 2*i+k<n-1:
if a[2*i+k] == a[2*i+k+1]:
k+=1
else:
b.append(a[2*i+k])
b.append(a[2*i+k+1])
i+=1
print(k+((n-k)%2))
print(*b, sep='')
``` | instruction | 0 | 91,788 | 0 | 183,576 |
Yes | output | 1 | 91,788 | 0 | 183,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
n=int(input())
s=input().rstrip()
x=list(s)
D=0;
if len(x)%2==0:
if len(x)%2==0:
l=[]
q=[]
i=0;
while(i<len(x)-1):
if x[i]==x[i+1]:
i+=1;
D+=1;
else:
l.append(x[i])
l.append(x[i+1])
q.append(i)
q.append(i+1)
i+=2;
if len(q)==0 or q[len(q)-1]!=(n-1):
l.append(x[len(x)-1])
if len(l)>1 and l[len(l)-1]==l[len(l)-2]:
del(l[len(l)-1])
D+=1;
if len(l)%2!=0:
D+=1;
del(l[len(l)-1])
print(D)
if len(l)!=0:
print(''.join(l))
else:
print()
else:
if n==1:
print(0)
print(s)
else:
D=0;
l=[]
q=[]
i=0;
while(i<len(x)-1):
if x[i]==x[i+1]:
i+=1;
D+=1;
else:
l.append(x[i])
l.append(x[i+1])
q.append(i)
q.append(i+1)
i+=2;
if len(q)==0 or q[len(q)-1]!=(n-1):
l.append(x[len(x)-1])
if len(l)>1 and l[len(l)-1]==l[len(l)-2]:
del(l[len(l)-1])
D+=1;
if len(l)%2!=0:
D+=1;
del(l[len(l)-1])
print(D)
if len(l)!=0:
print(''.join(l))
else:
print()
``` | instruction | 0 | 91,789 | 0 | 183,578 |
No | output | 1 | 91,789 | 0 | 183,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
import sys
n = int(sys.stdin.readline())
arr = list(sys.stdin.readline().rstrip())
i = 0
check = [True] * n
while i < n:
if i % 2 == 0:
j = i + 1
while j < n:
if arr[j] != arr[i]:
break
check[j] = False
j += 1
i = j
else:
i += 1
cnt = 0
for i in range(n):
if check[i]:
cnt += 1
if cnt % 2 == 0:
temp = 0
for i in range(n):
if not check[i]:
temp += 1
ans = []
for i in range(n):
if check[i]:
ans.append(arr[i])
print(temp)
print(''.join(ans))
else:
for i in range(n):
if check[i]:
check[i] = False
break
temp = 0
for i in range(n):
if not check[i]:
temp += 1
ans = []
for i in range(n):
if check[i]:
ans.append(arr[i])
print(temp)
print(''.join(ans))
``` | instruction | 0 | 91,790 | 0 | 183,580 |
No | output | 1 | 91,790 | 0 | 183,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
n=int(input())
string=list(input())
m=n
ex=0
if(n%2!=0):
m-=1
ex+=1
i=0
flag=0
while i<(m-1):
if(string[i]==string[i+1]):
string[i]='-'
flag=1
else:
i+=1
if(flag==1):
flag=0
m-=1
ex+=1
i+=1
count=0
newstr=[]
for i in range(n-ex):
if(string[i]!='-'):
newstr.append(string[i])
count+=1
print(n-count)
print(''.join(newstr))
``` | instruction | 0 | 91,791 | 0 | 183,582 |
No | output | 1 | 91,791 | 0 | 183,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of characters in s.
The second line contains the string s, consisting of exactly n lowercase Latin letters.
Output
In the first line, print one integer k (0 ≤ k ≤ n) — the minimum number of characters you have to delete from s to make it good.
In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.
Examples
Input
4
good
Output
0
good
Input
4
aabc
Output
2
ab
Input
3
aaa
Output
3
Submitted Solution:
```
n = int(input())
s = input()
def cal(s):
n = len(s)
ps1 = [0] * (n)
ps2 = [0] * (n)
cnt1, cnt2 = 0, 0
for i in range(1, n):
if i & 1:
cnt1 += s[i] == s[i - 1]
else:
cnt2 += s[i] == s[i - 1]
ps1[i] = cnt1
ps2[i] = cnt2
p, mn = -1, 1e9
for i in range(n):
tmp = 0
if i < n - 1:
tmp += ps2[n - 1] - ps2[i + 1]
if i:
tmp += ps1[i - 1]
if i & 1:
tmp += s[i - 1] == s[i + 1]
if tmp < mn:
p = i
mn = tmp
ns = ""
for i in range(n):
if i != p:
ns += s[i]
return ns
def cal2(s):
n = len(s)
ns = ""
for i in range(0, n, 2):
if s[i] != s[i + 1]:
ns += s[i] + s[i + 1]
return ns
if n & 1:
s = cal(s)
s = cal2(s)
print(n - len(s))
print(s)
``` | instruction | 0 | 91,792 | 0 | 183,584 |
No | output | 1 | 91,792 | 0 | 183,585 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string t and n strings s_1, s_2, ..., s_n. All strings consist of lowercase Latin letters.
Let f(t, s) be the number of occurences of string s in string t. For example, f('aaabacaa', 'aa') = 3, and f('ababa', 'aba') = 2.
Calculate the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j), where s + t is the concatenation of strings s and t. Note that if there are two pairs i_1, j_1 and i_2, j_2 such that s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}, you should include both f(t, s_{i_1} + s_{j_1}) and f(t, s_{i_2} + s_{j_2}) in answer.
Input
The first line contains string t (1 ≤ |t| ≤ 2 ⋅ 10^5).
The second line contains integer n (1 ≤ n ≤ 2 ⋅ 10^5).
Each of next n lines contains string s_i (1 ≤ |s_i| ≤ 2 ⋅ 10^5).
It is guaranteed that ∑_{i=1}^{n} |s_i| ≤ 2 ⋅ 10^5. All strings consist of lowercase English letters.
Output
Print one integer — the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j).
Examples
Input
aaabacaa
2
a
aa
Output
5
Input
aaabacaa
4
a
a
a
b
Output
33 | instruction | 0 | 91,794 | 0 | 183,588 |
Tags: brute force, string suffix structures, strings
Correct Solution:
```
class Node(object):
def __init__(self):
super(Node, self).__init__()
self.next = [-1] * 26
self.trans = []
self.matches = 0
self.leaf = 0
self.link = 0
class AhoCorasick(object):
def __init__(self):
super(AhoCorasick, self).__init__()
self.T = [Node()]
self.T[0].link = 0
def insert_trie(self, s):
v = 0
for i in range(len(s)):
c = ord(s[i]) - ord('a')
if(self.T[v].next[c] == -1):
self.T[v].trans.append(c)
self.T[v].next[c] = len(self.T)
self.T.append(Node())
v = self.T[v].next[c]
self.T[v].leaf += 1
self.T[v].matches += 1
def set_suffix_link(self, S):
Q = []
for j in range(len(S)):
Q.append((j, 0, 0, 0))
#string index, index in string, state, suff state,
i = 0
while(i < len(Q)):
j,ind,v,suff = Q[i]
i += 1
c = ord(S[j][ind]) - ord('a')
if(ind>0):
while(suff>0 and self.T[suff].next[c]==-1):
suff = self.T[suff].link
if(self.T[suff].next[c] != -1):
suff = self.T[suff].next[c]
v = self.T[v].next[c]
self.T[v].link = suff
if(ind+1 < len(S[j])):
Q.append((j,ind+1,v,suff))
def set_matches(self):
i = 0
Q = [0]
while(i < len(Q)):
v = Q[i]
self.T[v].matches = self.T[v].leaf + self.T[self.T[v].link].matches
for c in self.T[v].trans:
Q.append(self.T[v].next[c])
i += 1
def build(self, S):
for i in range(len(S)):
self.insert_trie(S[i])
self.set_suffix_link(S)
#self.printTree()
self.set_matches()
def get(self, s):
v = 0
matches = []
for i in range(len(s)):
c = ord(s[i]) - ord('a')
while(v>0 and self.T[v].next[c] == -1):
v = self.T[v].link
if(self.T[v].next[c] != -1):
v = self.T[v].next[c]
matches.append(self.T[v].matches)
return matches
def printTree(self):
for i in range(len(self.T)):
print(str(i)+" leaf:"+str(self.T[i].leaf)+" link:"+str(self.T[i].link)+" matches:"+str(self.T[i].matches)+" : " , end='')
for j in range(26):
print(" "+str(chr(j+ord('a')))+"-"+(str(self.T[i].next[j]) if (self.T[i].next[j]!=-1) else "_")+" ", end='')
print()
t = input()
n = int(input())
patterns = []
patterns_rev = []
for i in range(n):
s = input()
patterns.append(s)
patterns_rev.append(s[::-1])
t1 = AhoCorasick()
t2 = AhoCorasick()
t1.build(patterns)
t2.build(patterns_rev)
x1 = t1.get(t)
x2 = t2.get(t[::-1])[::-1]
#print(x1)
#print(x2)
ans = 0
for i in range(len(x1)-1):
ans += x1[i] * x2[i+1]
print(ans)
``` | output | 1 | 91,794 | 0 | 183,589 |
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 t and n strings s_1, s_2, ..., s_n. All strings consist of lowercase Latin letters.
Let f(t, s) be the number of occurences of string s in string t. For example, f('aaabacaa', 'aa') = 3, and f('ababa', 'aba') = 2.
Calculate the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j), where s + t is the concatenation of strings s and t. Note that if there are two pairs i_1, j_1 and i_2, j_2 such that s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}, you should include both f(t, s_{i_1} + s_{j_1}) and f(t, s_{i_2} + s_{j_2}) in answer.
Input
The first line contains string t (1 ≤ |t| ≤ 2 ⋅ 10^5).
The second line contains integer n (1 ≤ n ≤ 2 ⋅ 10^5).
Each of next n lines contains string s_i (1 ≤ |s_i| ≤ 2 ⋅ 10^5).
It is guaranteed that ∑_{i=1}^{n} |s_i| ≤ 2 ⋅ 10^5. All strings consist of lowercase English letters.
Output
Print one integer — the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j).
Examples
Input
aaabacaa
2
a
aa
Output
5
Input
aaabacaa
4
a
a
a
b
Output
33
Submitted Solution:
```
class Node(object):
def __init__(self):
super(Node, self).__init__()
self.next = [-1] * 26
self.matches = 0
self.leaf = 0
self.link = 0
class AhoCorasick(object):
def __init__(self):
super(AhoCorasick, self).__init__()
self.T = [Node()]
self.T[0].link = 0
def insert_trie(self, s):
v = 0
for i in range(len(s)):
c = ord(s[i]) - ord('a')
if(self.T[v].next[c] == -1):
self.T[v].next[c] = len(self.T)
self.T.append(Node())
v = self.T[v].next[c]
self.T[v].leaf += 1
self.T[v].matches += 1
def set_suffix_link(self, s):
v = 0
suff = 0
for i in range(len(s)):
c = ord(s[i])-ord('a')
if(i>0):
while(suff > 0 and self.T[suff].next[c] == -1):
suff = self.T[suff].link
if(self.T[suff].next[c] != -1):
suff = self.T[suff].next[c]
v = self.T[v].next[c]
self.T[v].link = suff
self.T[v].matches = self.T[v].leaf + self.T[suff].matches
def build(self, S):
for i in range(len(S)):
self.insert_trie(S[i])
for i in range(len(S)):
self.set_suffix_link(S[i])
def get(self, s):
v = 0
matches = []
for i in range(len(s)):
c = ord(s[i]) - ord('a')
while(v>0 and self.T[v].next[c] == -1):
v = self.T[v].link
if(self.T[v].next[c] != -1):
v = self.T[v].next[c]
#print(str(i) + " : " + str(v) + " : " + str(self.T[v].matches))
matches.append(self.T[v].matches)
return matches
def printTree(self):
for i in range(len(self.T)):
print(str(i)+" leaf:"+str(self.T[i].leaf)+" link:"+str(self.T[i].link)+" matches:"+str(self.T[i].matches)+" : " , end='')
for j in range(26):
print(" "+str(chr(j+ord('a')))+"-"+(str(self.T[i].next[j]) if (self.T[i].next[j]!=-1) else "_")+" ", end='')
print()
t = input()
n = int(input())
patterns = []
patterns_rev = []
for i in range(n):
s = input()
patterns.append(s)
patterns_rev.append(s[::-1])
t1 = AhoCorasick()
t2 = AhoCorasick()
t1.build(patterns)
t2.build(patterns_rev)
x1 = t1.get(t)
x2 = t2.get(t[::-1])[::-1]
ans = 0
for i in range(len(x1)-1):
ans += x1[i] * x2[i+1]
print(ans)
``` | instruction | 0 | 91,795 | 0 | 183,590 |
No | output | 1 | 91,795 | 0 | 183,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,959 | 0 | 183,918 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
import sys, math
import heapq
from collections import deque
input = sys.stdin.readline
#input
def ip(): return int(input())
def sp(): return str(input().rstrip())
def mip(): return map(int, input().split())
def msp(): return map(str, input().split())
def lmip(): return list(map(int, input().split()))
def lmsp(): return list(map(str, input().split()))
#gcd, lcm
def gcd(x, y):
while y:
x, y = y, x % y
return x
def lcm(x, y):
return x * y // gcd(x, y)
#prime
def isPrime(x):
if x==1: return False
for i in range(2, int(x**0.5)+1):
if x%i==0:
return False
return True
# Union Find
# p = {i:i for i in range(1, n+1)}
def find(x):
if x == p[x]:
return x
q = find(p[x])
p[x] = q
return q
def union(x, y):
global n
x = find(x)
y = find(y)
if x != y:
p[y] = x
def getPow(a, x):
ret =1
while x:
if x&1:
ret = (ret*a) % MOD
a = (a*a)%MOD
x>>=1
return ret
def getInv(x):
return getPow(x, MOD-2)
def lowerBound(start, end, key):
while start < end:
mid = (start + end) // 2
if lst[mid] == key:
end = mid
elif key < lst[mid]:
end = mid
elif lst[mid] < key:
start = mid + 1
return end
def upperBound(start, end, key):
while start < end:
mid = (start + end) // 2
if lst[mid] == key:
start = mid + 1
elif lst[mid] < key:
start = mid + 1
elif key < lst[mid]:
end = mid
if end == len(lst):
return end-1
return end
############### Main! ###############
t = ip()
while t:
t -= 1
n=ip()
a=sp()
b=sp()
c=sp()
i,j,k=0,0,0
ans=[]
while True:
if i==2*n:
if j>=k:
for m in range (j,2*n):
ans.append(b[m])
else:
for m in range (k,2*n):
ans.append(c[m])
break
elif j==2*n:
if i>=k:
for m in range (i,2*n):
ans.append(a[m])
else:
for m in range (k,2*n):
ans.append(c[m])
break
elif k==2*n:
if j>=i:
for m in range (j,2*n):
ans.append(b[m])
else:
for m in range (i,2*n):
ans.append(a[m])
break
if a[i]==b[j]:
ans.append(a[i])
i+=1
j+=1
elif b[j]==c[k]:
ans.append(b[j])
j+=1
k+=1
else:
ans.append(a[i])
i+=1
k+=1
print(*ans,sep='')
######## Priest W_NotFoundGD ########
``` | output | 1 | 91,959 | 0 | 183,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,960 | 0 | 183,920 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
def solve(n,ls):
x,y = [i.count('0') for i in ls],[i.count('1')for i in ls]
for i in range(3):
for j in range(3):
if i == j:
continue
if x[i] >= n and x[j] >= n:
ans,b = [],0
for a in ls[i]:
if a == '1':
ans.append('1')
continue
while b != 2*n and ls[j][b] != '0':
ans.append('1')
b += 1
ans.append('0')
if b != 2*n:
b += 1
while b != 2*n:
ans.append(ls[j][b])
b += 1
return ''.join(ans)
if y[i] >= n and y[j] >= n:
ans,b = [],0
for a in ls[i]:
if a == '0':
ans.append('0')
continue
while b != 2*n and ls[j][b] != '1':
ans.append('0')
b += 1
ans.append('1')
if b != 2*n:
b += 1
while b != 2*n:
ans.append(ls[j][b])
b += 1
return ''.join(ans)
def main():
for _ in range(int(input())):
n = int(input())
ls = [input().strip() for _ in range(3)]
print(solve(n,ls))
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
``` | output | 1 | 91,960 | 0 | 183,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,961 | 0 | 183,922 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = int(input())
a = []
a.append(input())
a.append(input())
a.append(input())
zeroDominant = []
oneDominant = []
for k in range(3):
cnt = 0
for i in range(2 * n):
if a[k][i] == "1":
cnt += 1
if cnt >= n:
oneDominant.append(a[k])
else:
zeroDominant.append(a[k])
if len(oneDominant) >= 2:
n *= 2
ans = []
fIndex = 0
lIndex = 0
while fIndex < n or lIndex < n:
if fIndex == n and lIndex < n:
ans.append(oneDominant[1][lIndex])
lIndex += 1
elif lIndex == n and fIndex < n:
ans.append(oneDominant[0][fIndex])
fIndex += 1
else:
if oneDominant[0][fIndex] == oneDominant[1][lIndex]:
ans.append(oneDominant[0][fIndex])
fIndex += 1
lIndex += 1
else:
ans.append("0")
if oneDominant[0][fIndex] == "0":
fIndex += 1
else:
lIndex += 1
print("".join(ans))
else:
n *= 2
ans = []
fIndex = 0
lIndex = 0
while fIndex < n or lIndex < n:
if fIndex == n and lIndex < n:
ans.append(zeroDominant[1][lIndex])
lIndex += 1
elif lIndex == n and fIndex < n:
ans.append(zeroDominant[0][fIndex])
fIndex += 1
else:
if zeroDominant[0][fIndex] == zeroDominant[1][lIndex]:
ans.append(zeroDominant[0][fIndex])
fIndex += 1
lIndex += 1
else:
ans.append("1")
if zeroDominant[0][fIndex] == "1":
fIndex += 1
else:
lIndex += 1
print("".join(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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 91,961 | 0 | 183,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,962 | 0 | 183,924 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
from operator import add,mul,sub,xor,truediv,floordiv
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def A(n):return [0]*n
def AI(n,x): return [x]*n
def A2(n,m): return [[0]*m for i in range(n)]
def G(n): return [[] for i in range(n)]
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
mod=10**9+7
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
fact(x,mod)
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa.reverse()
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def floorsum(a,b,c,n):#sum((a*i+b)//c for i in range(n+1))
if a==0:return b//c*(n+1)
if a>=c or b>=c: return floorsum(a%c,b%c,c,n)+b//c*(n+1)+a//c*n*(n+1)//2
m=(a*n+b)//c
return n*m-floorsum(c,c-b-1,a,m-1)
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
def lowbit(n):
return n&-n
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class ST:
def __init__(self,arr):#n!=0
n=len(arr)
mx=n.bit_length()#取不到
self.st=[[0]*mx for i in range(n)]
for i in range(n):
self.st[i][0]=arr[i]
for j in range(1,mx):
for i in range(n-(1<<j)+1):
self.st[i][j]=max(self.st[i][j-1],self.st[i+(1<<j-1)][j-1])
def query(self,l,r):
if l>r:return -inf
s=(r+1-l).bit_length()-1
return max(self.st[l][s],self.st[r-(1<<s)+1][s])
'''
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]>self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]'''
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
seen.add(u)
for v,w in graph[u]:
if v not in d or d[v]>d[u]+w:
d[v]=d[u]+w
heappush(heap,(d[v],v))
return d
def bell(s,g):#bellman-Ford
dis=AI(n,inf)
dis[s]=0
for i in range(n-1):
for u,v,w in edge:
if dis[v]>dis[u]+w:
dis[v]=dis[u]+w
change=A(n)
for i in range(n):
for u,v,w in edge:
if dis[v]>dis[u]+w:
dis[v]=dis[u]+w
change[v]=1
return dis
def lcm(a,b): return a*b//gcd(a,b)
def lis(nums):
res=[]
for k in nums:
i=bisect.bisect_left(res,k)
if i==len(res):
res.append(k)
else:
res[i]=k
return len(res)
def RP(nums):#逆序对
n = len(nums)
s=set(nums)
d={}
for i,k in enumerate(sorted(s),1):
d[k]=i
bi=BIT([0]*(len(s)+1))
ans=0
for i in range(n-1,-1,-1):
ans+=bi.query(d[nums[i]]-1)
bi.update(d[nums[i]],1)
return ans
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
def nb(i,j,n,m):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
def topo(n):
q=deque()
res=[]
for i in range(1,n+1):
if ind[i]==0:
q.append(i)
res.append(i)
while q:
u=q.popleft()
for v in g[u]:
ind[v]-=1
if ind[v]==0:
q.append(v)
res.append(v)
return res
@bootstrap
def gdfs(r,p):
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
def cal(x):
ans=0
for i in range(2*n):
ans+=x[i]=='1'
return ans
def f(x,y,ch):
ans=[]
i=j=0
c=0
while True:
while i<2*n and x[i]!=ch:
ans.append(x[i])
i+=1
while j<2*n and y[j]!=ch:
ans.append(y[j])
j+=1
ans.append(x[i])
i+=1
j+=1
c+=1
if c>=n:
break
ans=ans+x[i:]+y[j:]
return ''.join(ans)
t=N()
for i in range(t):
n=N()
a=list(input())
b=list(input())
c=list(input())
ca=cal(a)
cb=cal(b)
cc=cal(c)
res=[[a,ca],[b,cb],[c,cc]]
res.sort(key=lambda x:x[1])
[a,ca],[b,cb],[c,cc]=res
if cb>=n:
ans=f(b,c,'1')
else:
ans=f(a,b,'0')
print(ans)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thr
ead(target=main)
t.start()
t.join()
'''
``` | output | 1 | 91,962 | 0 | 183,925 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,963 | 0 | 183,926 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
ans=[]
s1=input()
s2=input()
s3=input()
fincount=0
count10=0
for i in range(2*n):
if s1[i]=='0':
count10+=1
if count10>=n:
fincount+=1
count20=0
for i in range(2*n):
if s2[i]=='0':
count20+=1
if count20>=n:
fincount+=1
count30=0
for i in range(2*n):
if s3[i]=='0':
count30+=1
if count30>=n:
fincount+=1
if fincount>=2:
mode='0'
unmode='1'
else:
mode='1'
unmode='0'
if mode=='0':
if count10>=n and count20>=n:
st1=s1
st2=s2
elif count10>=n and count30>=n:
st1=s1
st2=s3
elif count20>=n and count30>=n:
st1=s2
st2=s3
else:
if count10<=n and count20<=n:
st1=s1
st2=s2
elif count10<=n and count30<=n:
st1=s1
st2=s3
elif count20<=n and count30<=n:
st1=s2
st2=s3
pointer1=0
pointer2=0
while pointer1<2*n or pointer2<2*n:
while pointer1<2*n and st1[pointer1]==unmode:
pointer1+=1
ans.append(unmode)
while pointer2<2*n and st2[pointer2]==unmode:
pointer2+=1
ans.append(unmode)
if pointer1>=2*n and pointer2>=2*n:
break
ans.append(mode)
pointer1+=1
pointer2+=1
print(''.join(ans))
``` | output | 1 | 91,963 | 0 | 183,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,964 | 0 | 183,928 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
import sys,os
from io import BytesIO,IOBase
mod = 10**9+7; Mod = 998244353; INF = float('inf')
# input = lambda: sys.stdin.readline().rstrip("\r\n")
# inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
# region fastio
'''
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# endregion'''
#______________________________________________________________________________________________________
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
# ______________________________________________________________________________________________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# from itertools import groupby
# sys.setrecursionlimit(2000+10) #this is must for dfs
# ______________________________________________________________________________________________________
# segment tree for range minimum query
# n = int(input())
# a = list(map(int,input().split()))
# st = [float('inf') for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return float('inf')
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod = 10**9+7):
# if r>n: return 0
# if r>n-r: r = n-r
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# def C(n,r):
# if r>n:
# return 0
# if r>n-r:
# r = n-r
# ans = 1
# for i in range(r):
# ans = (ans*(n-i))//(i+1)
# return ans
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 2*10**5+10
# spf = [i for i in range(M)]
# def spfs(M):
# for i in range(2,M):
# if spf[i]==i:
# for j in range(i*i,M,i):
# if spf[j]==j:
# spf[j] = i
# return
# spfs(M)
# ______________________________________________________________________________________________________
# def gtc(p):
# print('Case #'+str(p)+': ',end='')
# ______________________________________________________________________________________________________
tc = 1
tc = int(input())
for test in range(1,tc+1):
n = int(input())
s1 = str(input())
s2 = str(input())
s3 = str(input())
o1 = s1.count('1');z1 = 2*n-o1
o2 = s2.count('1');z2 = 2*n-o2
o3 = s3.count('1');z3 = 2*n-o3
def solve(s1,s2,k):
ans = []
i = 0
j = 0
n = len(s1)
while(i<n and j<n):
if s1[i]==s2[j]:
ans.append(s1[i])
i+=1
j+=1
else:
if s1[i]==k:
ans.append(s2[j])
j+=1
else:
ans.append(s1[i])
i+=1
while(i<n):
ans.append(s1[i])
i+=1
while(j<n):
ans.append(s2[j])
j+=1
# print(s1,s2,ans)
return ''.join(ans)+'0'*((n//2)*3-len(ans))
if o1>=n and o2>=n:
ans = solve(s1,s2,'1')
elif o2>=n and o3>=n:
ans = solve(s2,s3,'1')
elif o1>=n and o3>=n:
ans = solve(s1,s3,'1')
elif z1>=n and z2>=n:
ans = solve(s1,s2,'0')
elif z2>=n and z3>=n:
ans = solve(s2,s3,'0')
else:
ans = solve(s1,s3,'0')
print(ans)
``` | output | 1 | 91,964 | 0 | 183,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,965 | 0 | 183,930 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
import sys
from sys import stdin
def solve(a,b,X):
#print (a,b,X,file=sys.stderr)
ans = []
ca = [a[i] for i in range(len(a))]
cb = [b[i] for i in range(len(b))]
while len(ca) > 0 or len(cb) > 0:
if len(cb) == 0:
ans.append(ca[-1])
del ca[-1]
elif len(ca) == 0:
ans.append(cb[-1])
del cb[-1]
elif ca[-1] == cb[-1]:
ans.append(ca[-1])
del ca[-1]
del cb[-1]
else:
ans.append(X)
if ca[-1] == X:
del ca[-1]
else:
del cb[-1]
while len(ans) < 3*n:
ans.append("0")
ans.reverse()
return "".join(ans)
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
s = [stdin.readline()[:-1] for i in range(3)]
lis = []
for i in range(3):
one = 0
zero = 0
for j in s[i]:
if j == "0":
zero += 1
else:
one += 1
if one < zero:
lis.append("1")
else:
lis.append("0")
flag = True
for i in range(3):
for j in range(i):
if lis[i] == lis[j] and flag:
print (solve(s[i],s[j],lis[i]))
flag = False
``` | output | 1 | 91,965 | 0 | 183,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | instruction | 0 | 91,966 | 0 | 183,932 |
Tags: constructive algorithms, greedy, implementation, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
def main():
n = int(input())
S1 = input().strip()
S2 = input().strip()
S3 = input().strip()
for s1 in (S1, S2, S3):
for s2 in (S1, S2, S3):
if s1 == s2:
continue
z1 = s1.count("0")
o1 = s1.count("1")
z2 = s2.count("0")
o2 = s2.count("1")
mz = max(z1, z2)
mo = max(o1, o2)
if mz > n and mo > n:
continue
if mz <= n:
cnt1 = []
cnt2 = []
bef = "0"
cnt = 0
for s in s1:
if s == "1":
cnt1.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt1.append(cnt)
bef = "0"
cnt = 0
for s in s2:
if s == "1":
cnt2.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt2.append(cnt)
if len(cnt1) > len(cnt2):
cnt2 += [0] * (len(cnt1) - len(cnt2))
elif len(cnt1) < len(cnt2):
cnt1 += [0] * (len(cnt2) - len(cnt1))
ans = []
for c1, c2 in zip(cnt1, cnt2):
ans += ["0"] * max(c1, c2)
ans += ["1"]
ans.pop()
if len(ans) < 3 * n:
ans += ["1"] * (3 * n - len(ans))
print("".join(ans))
return
elif mo <= n:
cnt1 = []
cnt2 = []
bef = "1"
cnt = 0
for s in s1:
if s == "0":
cnt1.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt1.append(cnt)
bef = "1"
cnt = 0
for s in s2:
if s == "0":
cnt2.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt2.append(cnt)
if len(cnt1) > len(cnt2):
cnt2 += [0] * (len(cnt1) - len(cnt2))
elif len(cnt1) < len(cnt2):
cnt1 += [0] * (len(cnt2) - len(cnt1))
ans = []
for c1, c2 in zip(cnt1, cnt2):
ans += ["1"] * max(c1, c2)
ans += ["0"]
ans.pop()
if len(ans) < 3 * n:
ans += ["0"] * (3 * n - len(ans))
print("".join(ans))
return
for _ in range(int(input())):
main()
``` | output | 1 | 91,966 | 0 | 183,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
def some_random_function():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function5():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
import os,sys
from io import BytesIO,IOBase
def solve(n,ls):
x,y = [i.count('0') for i in ls],[i.count('1')for i in ls]
for i in range(3):
for j in range(3):
if i == j:
continue
if x[i] >= n and x[j] >= n:
ans,b = [],0
for a in ls[i]:
if a == '1':
ans.append('1')
continue
while b != 2*n and ls[j][b] != '0':
ans.append('1')
b += 1
ans.append('0')
if b != 2*n:
b += 1
while b != 2*n:
ans.append(ls[j][b])
b += 1
return ''.join(ans)
if y[i] >= n and y[j] >= n:
ans,b = [],0
for a in ls[i]:
if a == '0':
ans.append('0')
continue
while b != 2*n and ls[j][b] != '1':
ans.append('0')
b += 1
ans.append('1')
if b != 2*n:
b += 1
while b != 2*n:
ans.append(ls[j][b])
b += 1
return ''.join(ans)
def main():
for _ in range(int(input())):
n = int(input())
ls = [input().strip() for _ in range(3)]
print(solve(n,ls))
#Fast IO Region
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 some_random_function1():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function2():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function3():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function4():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function6():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function7():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function8():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am making
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
if __name__ == '__main__':
main()
``` | instruction | 0 | 91,967 | 0 | 183,934 |
Yes | output | 1 | 91,967 | 0 | 183,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Submitted Solution:
```
import sys
input = sys.stdin.readline
for f in range(int(input())):
n=int(input())
s=[]
for i in range(3):
s.append(input())
s1=[]
s0=[]
for i in range(3):
c0=0
for x in s[i]:
if x=="0":c0+=1
s0.append(c0)
s1.append(2*n-s0[-1])
for i in range(2):
for j in range(i+1,3):
if s0[i]>=n and s0[j]>=n:
c1=i
c2=j
g="0"
b="1"
if s1[i]>=n and s1[j]>=n:
c1=i
c2=j
g="1"
b="0"
i=j=0
while i<2*n or j<2*n:
if i<2*n and s[c1][i]==b:
i+=1
print(end=b)
else:
if j<2*n and s[c2][j]==b:
j+=1
print(end=b)
else:
print(end=g)
i+=1
j+=1
print()
``` | instruction | 0 | 91,968 | 0 | 183,936 |
Yes | output | 1 | 91,968 | 0 | 183,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Submitted Solution:
```
tc = int(input())
for i in range(tc):
n = int(input())
s = [input() for _ in range(3)]
p = [0]*3
ans = ""
while True:
ones = []
oc = 0
zeroes = []
for i in range(3):
if s[i][p[i]] == '1':
ones.append(i)
oc += 1
else:
zeroes.append(i)
if oc >= 2:
print('1', end="")
for i in ones:
p[i] += 1
else:
print('0', end="")
for i in zeroes:
p[i] += 1
if (p[0] == 2*n) or (p[1] == 2*n) or (p[2] == 2*n):
break
#pick the smallest left omg
x = sorted(enumerate(p), key=lambda i:i[1])
print(s[x[1][0]][x[1][1]:])
#3 pointer trick in comments
``` | instruction | 0 | 91,969 | 0 | 183,938 |
Yes | output | 1 | 91,969 | 0 | 183,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of n across all test cases does not exceed 10^5.
Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
Example
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Submitted Solution:
```
from sys import *
input = stdin.readline
def superstring(x,y,fx,n):
ans = ""
i,j = 0,0
while (i<2*n) and (j<2*n):
if x[i]==y[j]:
ans+=x[i]
i+=1
j+=1
elif x[i]!=fx:
ans+=x[i]
i+=1
else:
ans+=y[j]
j+=1
return ans+x[i:]+y[j:]
for _ in range(int(input())):
n = int(input())
a,b,c = input().strip(),input().strip(),input().strip()
one,zero = [],[]
for i in [a,b,c]:
if i.count("0")>=n:
zero.append(i)
else:
one.append(i)
currans = ""
if len(zero)>len(one):
currans = superstring(zero[0],zero[1],'0',n)
else:
currans = superstring(one[0],one[1],'1',n)
print(currans.rjust(3*n,'0'))
``` | instruction | 0 | 91,970 | 0 | 183,940 |
Yes | output | 1 | 91,970 | 0 | 183,941 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.