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.
A non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, "abcba", "a", and "abba" are palindromes, while "abab" and "xy" are not.
A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, "abc", "ab", and "c" are substrings of the string "abc", while "ac" and "d" are not.
Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string "aaa" is 6 because all its substrings are palindromes, and the palindromic count of the string "abc" is 3 because only its substrings of length 1 are palindromes.
You are given a string s. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count.
Input
The first line contains an integer n (1 β€ n β€ 100 000) β the length of string s.
The second line contains string s that consists of exactly n lowercase characters of Latin alphabet.
Output
Print string t, which consists of the same set of characters (and each characters appears exactly the same number of times) as string s. Moreover, t should have the maximum possible value of palindromic count among all such strings strings.
If there are multiple such strings, print any of them.
Examples
Input
5
oolol
Output
ololo
Input
16
gagadbcgghhchbdf
Output
abccbaghghghgdfd
Note
In the first example, string "ololo" has 9 palindromic substrings: "o", "l", "o", "l", "o", "olo", "lol", "olo", "ololo". Note, that even though some substrings coincide, they are counted as many times as they appear in the resulting string.
In the second example, the palindromic count of string "abccbaghghghgdfd" is 29.
Submitted Solution:
```
import sys
from collections import Counter
n=int(input())
s=sys.stdin.readline().strip()
d=Counter(s)
p=''
q=[]
l=0
for v in d:
p=p+v*(d[v]//2)
l=l+d[v]//2
d[v]=d[v]%2
if d[v]==1:
q.append(v)
k=0
a=''
for idx,v in enumerate(q):
if k<l:
a=a+p[k]+v+p[k]
k=k+1
a=a+p[k:]+p[k:][::-1]+''.join(q[k:])
print(a)
``` | instruction | 0 | 21,212 | 0 | 42,424 |
No | output | 1 | 21,212 | 0 | 42,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | instruction | 0 | 21,677 | 0 | 43,354 |
Tags: data structures, greedy, sortings, strings
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
L = 0
t, x = [], []
for i in range(n):
v = input().split()
t.append(v[0])
pos = list(map(lambda x: int(x) - 1, v[2:]))
x.append(pos)
L = max(L, pos[-1] + len(t[i]))
nxt = list(range(L + 1))
ans = ['a'] * L
def find(j):
jcopy = j
while nxt[j] != j:
j = nxt[j]
j, jcopy = jcopy, j
while nxt[j] != j:
tmp = nxt[j]
nxt[j] = jcopy
j = tmp
return j
for i in range(n):
for j in x[i]:
l = j
j = find(nxt[j])
while j < l + len(t[i]):
ans[j] = t[i][j - l]
nxt[j] = j + 1
j = find(nxt[j])
print(*ans, sep='')
``` | output | 1 | 21,677 | 0 | 43,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | instruction | 0 | 21,678 | 0 | 43,356 |
Tags: data structures, greedy, sortings, strings
Correct Solution:
```
import math as mt
import sys,string
input=sys.stdin.readline
print=sys.stdout.write
import random
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
t=I()
d=defaultdict(str)
ml=0
p=0
for _ in range(t):
s=input().split()
w=s[0]
x=list(map(int,s[1::]))
for i in range(1,len(x)):
r=d[x[i]-1]
if(len(r)<len(w)):
d[x[i]-1]=w
ml=max(ml,len(w)+x[-1]-1)
ans="a"*ml
ans=list(ans)
#print(d)
p=-1
z=sorted(list(d.keys()))
for i in z:
if(i+len(d[i])>p):
if(i>=p):
for j in range(i,i+len(d[i])):
ans[j]=d[i][j-i]
else:
leave=p-i
f=max(i,p)
#print(ans,"@",d[i],p,d[i][leave::])
for j in range(leave,len(d[i])):
ans[f]=d[i][j]
f+=1
#print(ans,"*")
p=i+len(d[i])
for i in ans:
print(i)
``` | output | 1 | 21,678 | 0 | 43,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | instruction | 0 | 21,679 | 0 | 43,358 |
Tags: data structures, greedy, sortings, strings
Correct Solution:
```
from sys import stdin, stdout
sze = 10 ** 6 + 1
n = int(stdin.readline())
challengers = []
strings = []
sze = 10 ** 6 + 1
cnt = [0 for i in range(sze)]
for i in range(n):
s = stdin.readline().strip().split()
num = int(s[1])
values = list(map(int, s[2:]))
strings.append(s[0])
for j in range(num):
if not cnt[values[j]]:
cnt[values[j]] = (i, len(s[0]))
elif cnt[values[j]][1] < len(s[0]):
cnt[values[j]] = (i, len(s[0]))
previous = 1
for i in range(sze):
if not cnt[i]:
continue
ind, s = i, cnt[i][0]
s = strings[s]
if previous < ind:
stdout.write(str('a') * (ind - previous))
previous = ind
if previous > ind + len(s) - 1:
continue
else:
stdout.write(s[previous - ind: len(s)])
previous = ind + len(s)
``` | output | 1 | 21,679 | 0 | 43,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | instruction | 0 | 21,680 | 0 | 43,360 |
Tags: data structures, greedy, sortings, strings
Correct Solution:
```
from sys import stdin, stdout
sze = 10 ** 6 + 1
n = int(stdin.readline())
challengers = []
strings = []
sze = 10 ** 6 + 1
cnt = [[] for i in range(sze)]
for i in range(n):
s = stdin.readline().strip().split()
num = int(s[1])
values = list(map(int, s[2:]))
strings.append(s[0])
for j in range(num):
cnt[values[j]].append(i)
previous = 1
for i in range(sze):
if not cnt[i]:
continue
ind, s = i, max(cnt[i], key = lambda x: len(strings[x]))
s = strings[s]
if previous < ind:
stdout.write(str('a') * (ind - previous))
previous = ind
if previous > ind + len(s) - 1:
continue
else:
stdout.write(s[previous - ind: len(s)])
previous = ind + len(s)
``` | output | 1 | 21,680 | 0 | 43,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab
Submitted Solution:
```
# import re
# import sys
# n = int(sys.stdin.readline())
# dp = {}
# max = 0
# d = []
# for i in range(n):
# d.append(sys.stdin.readline().split())
# for r in d:
# cl = len(r[0])
# for j in range(int(r[1])):
# if not dp.get(int(r[j+2]) - 1):
# dp[int(r[j+2]) - 1] = ("", 0)
# if cl > dp[int(r[j+2]) - 1][1]:
# dp[int(r[j+2]) - 1] = (r[0], cl)
# if max < cl + int(r[j+2]):
# max = cl + int(r[j+2])
# ans = "a"*(max - 1)
# for each in dp:
# ans = ans[:each] + dp[each][0] + ans[each+dp[each][1]:]
# print(ans)
import sys
n = int(sys.stdin.readline())
dp = {}
max = 0
for i in range(n):
r = tuple(sys.stdin.readline().split())
cl = len(r[0])
for j in range(int(r[1])):
if not dp.get(int(r[j+2]) - 1):
dp[int(r[j+2]) - 1] = ("", 0)
if cl > dp[int(r[j+2]) - 1][1]:
dp[int(r[j+2]) - 1] = (r[0], cl)
if max < cl + int(r[j+2]):
max = cl + int(r[j+2])
ans = "a"*(max - 1)
# for each in dp:
# ans = ans[:each] + dp[each][0] + ans[each+dp[each][1]:]
print(ans)
``` | instruction | 0 | 21,681 | 0 | 43,362 |
No | output | 1 | 21,681 | 0 | 43,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab
Submitted Solution:
```
import heapq
x = []
heapq.heapify(x)
for _ in range(int(input())):
arr = list(input().split())
char = arr[0]
for i in range(int(arr[1])):
heapq.heappush(x,[arr[2+i],char])
ind = 1
s = ""
k = x[:]
while x:
while x and ind < int(x[0][0]):
ind += 1
s += "a"
pos,char = heapq.heappop(x)
while x and int(x[0][0]) == int(pos):
char = heapq.heappop(x)[1]
s = s[:int(pos)-1] + char
ind = int(pos) + len(char)
for i in s:
print(i,end="")
``` | instruction | 0 | 21,682 | 0 | 43,364 |
No | output | 1 | 21,682 | 0 | 43,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab
Submitted Solution:
```
#!/usr/bin/env python3
def solve():
return None
_testcases = """
""".strip()
# ======================= B O I L E R P L A T E ======================= #
# Practicality beats purity
import re
import sys
import math
import heapq
from heapq import heapify, heappop, heappush
import bisect
from bisect import bisect_left, bisect_right
import operator
from operator import itemgetter, attrgetter
import itertools
import collections
inf = float('inf')
sys.setrecursionlimit(10000)
def tree():
return collections.defaultdict(tree)
def cache(func): # Decorator for memoizing a function
cache_dict = {}
def _cached_func(*args, _get_cache=False):
if _get_cache:
return cache_dict
if args in cache_dict:
return cache_dict[args]
cache_dict[args] = func(*args)
return cache_dict[args]
return _cached_func
def equal(x, y, epsilon=1e-6):
# https://code.google.com/codejam/kickstart/resources/faq#real-number-behavior
if -epsilon <= x - y <= epsilon:
return True
if -epsilon <= x <= epsilon or -epsilon <= y <= epsilon:
return False
return (-epsilon <= (x - y) / x <= epsilon or -epsilon <= (x - y) / y <= epsilon)
def get(_type): # For easy input
if type(_type) == list:
if len(_type) == 1:
_type = _type[0]
return list(map(_type, input().strip().split()))
else:
return [_type[i](inp) for i, inp in enumerate(input().strip().split())]
else:
return _type(input().strip())
if __name__ == '__main__':
print(solve())
``` | instruction | 0 | 21,683 | 0 | 43,366 |
No | output | 1 | 21,683 | 0 | 43,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 β€ n β€ 105) β the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order β positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 β€ xi, j β€ 106, 1 β€ ki β€ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab
Submitted Solution:
```
from sys import stdin, stdout
sze = 10 ** 6 + 1
n = int(stdin.readline())
challengers = []
for i in range(n):
s = stdin.readline().strip().split()
num = int(s[1])
values = list(map(int, s[2:]))
s = s[0]
for j in range(num):
challengers.append((values[j], s))
challengers.sort(key = lambda x: x[0])
previous = 1
for i in range(len(challengers)):
ind, s = challengers[i]
if previous < ind:
stdout.write(str('a') * (ind - previous))
ptevious = ind
if previous > ind + len(s) - 1:
continue
else:
stdout.write(s[previous - ind: len(s)])
previous = ind + len(s)
``` | instruction | 0 | 21,684 | 0 | 43,368 |
No | output | 1 | 21,684 | 0 | 43,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,085 | 0 | 44,170 |
Tags: brute force, two pointers
Correct Solution:
```
def parse(c, n):
l = [-1] * n
for x in c:
se = set(x)
for i in range(len(x) - 1):
for j in range(i + 1, len(x)):
k = x[j] - x[i]
if x[i] + k + k >= n:
break
if x[i] + k + k in se:
l[x[i] + k + k] = x[i]
break
res = 0
prex = -1
# print(l)
for i in range(n):
if l[i] <= prex:
continue
res = res + (l[i] - prex) * (n - i);
# print(prex + 1, l[i], i + 1, n)
prex = l[i]
return res
if __name__ == '__main__':
s = input()
one = [i for i in range(len(s)) if s[i] == '1']
zero = [i for i in range(len(s)) if s[i] == '0']
# print(one)
# print(zero)
ans = parse((one, zero), len(s))
print(ans)
``` | output | 1 | 22,085 | 0 | 44,171 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R | instruction | 0 | 22,480 | 0 | 44,960 |
Tags: dp
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
s, t = input().rstrip(), input().rstrip()
n, m = len(s), len(t)
dp = [[10**9] * (m + 2) for _ in range(n + 2)]
dp[n][m] = 0
prev = [[(-1, -1)] * (m + 2) for _ in range(n + 2)]
for i in range(n, -1, -1):
for j in range(m, -1, -1):
if i < n and j < m and dp[i][j] > dp[i + 1][j + 1] + (0 if s[i] == t[j] else 1):
dp[i][j] = dp[i + 1][j + 1] + (0 if s[i] == t[j] else 1)
prev[i][j] = (i + 1, j + 1)
if i < n and dp[i][j] > dp[i + 1][j] + 1:
dp[i][j] = dp[i + 1][j] + 1
prev[i][j] = (i + 1, j)
if j < m and dp[i][j] > dp[i][j + 1] + 1:
dp[i][j] = dp[i][j + 1] + 1
prev[i][j] = (i, j + 1)
i, j = 0, 0
ans = []
while i < n or j < m:
ti, tj = prev[i][j]
if i + 1 == ti and j + 1 == tj:
if s[i] != t[j]:
ans.append(f'REPLACE {j+1} {t[j]}')
elif i + 1 == ti:
ans.append(f'DELETE {j+1}')
elif j + 1 == tj:
ans.append(f'INSERT {j+1} {t[j]}')
i, j = ti, tj
sys.stdout.buffer.write((str(len(ans)) + '\n' + '\n'.join(ans)).encode('utf-8'))
``` | output | 1 | 22,480 | 0 | 44,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R | instruction | 0 | 22,481 | 0 | 44,962 |
Tags: dp
Correct Solution:
```
s,t="$"+input(),"$"+input()
n,m=len(s)-1,len(t)-1
ar=[[0]*(m+1) for i in range(n+1)]
for i in range(1,n+1):ar[i][0]=i
for j in range(1,m+1):ar[0][j]=j
for i in range(1,n+1):
for j in range(1,m+1):
if(s[i]==t[j]):ar[i][j]=ar[i-1][j-1]
else:ar[i][j]=min(ar[i-1][j],ar[i-1][j-1],ar[i][j-1])+1
x,y=n,m
res=[]
while(x+y):
if(s[x]==t[y]):
x-=1
y-=1
elif(x and y and ar[x-1][y-1]+1==ar[x][y]):
res.append(["REPLACE",x,t[y]])
x-=1
y-=1
elif(x and ar[x-1][y]+1==ar[x][y]):
res.append(["DELETE",x])
x-=1
else:
res.append(["INSERT",x+1,t[y]])
y-=1
#res=res[::-1]
print(len(res))
for e in res:
print(*e)
``` | output | 1 | 22,481 | 0 | 44,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R | instruction | 0 | 22,482 | 0 | 44,964 |
Tags: dp
Correct Solution:
```
# https://www.youtube.com/watch?v=MiqoA-yF-0M&feature=youtu.be
s, t = input(), input()
n, m = len(s), len(t)
M, camino = [], []
for _ in range(n+1):
M.append([0]*(m+1))
camino.append([None] * (m + 1))
for i in range(1, n+1):
M[i][0] = i
for j in range(1, m+1):
M[0][j] = j
for i in range(1, n+1):
for j in range(1, m+1):
distintos = s[i-1] != t[j-1]
m1, m2, m3 = M[i-1][j-1], M[i-1][j], M[i][j-1]
if not distintos:
M[i][j] = m1
camino[i][j] = (1, distintos)
continue
minimo = min(m1, m2, m3)
M[i][j] = minimo + 1
if minimo == m3:
camino[i][j] = (3, distintos)
elif minimo == m1:
camino[i][j] = (1, distintos)
else:
camino[i][j] = (2, distintos)
print(M[n][m])
instrucciones = []
i, j = n, m
while i*j != 0:
if camino[i][j][0] == 1:
if camino[i][j][1]:
instrucciones.append("REPLACE " + str(j) + " " + t[j-1])
i, j = i-1, j-1
elif camino[i][j][0] == 2:
if camino[i][j][1]:
instrucciones.append("DELETE " + str(j+1))
i -= 1
else:
if camino[i][j][1]:
instrucciones.append("INSERT " + str(j) + " " + t[j-1])
j -= 1
if i == 0:
for k in range(j, 0, -1):
instrucciones.append("INSERT " + str(k) + " " + t[k-1])
else:
for _ in range(i):
instrucciones.append("DELETE " + str(1))
for instruccion in instrucciones[::-1]:
print(instruccion)
``` | output | 1 | 22,482 | 0 | 44,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R
Submitted Solution:
```
s,t="$"+input(),"$"+input()
n,m=len(s)-1,len(t)-1
ar=[[0]*(m+1) for i in range(n+1)]
for i in range(1,n+1):ar[i][0]=i
for j in range(1,m+1):ar[0][j]=i
for i in range(1,n+1):
for j in range(1,m+1):
if(s[i]==t[j]):ar[i][j]=ar[i-1][j-1]
else:ar[i][j]=min(ar[i-1][j],ar[i-1][j-1],ar[i][j-1])+1
print(ar[n][m])
x,y=n,m
while(x+y):
if(s[x]==t[y]):
x-=1
y-=1
elif(x and y and ar[x-1][y-1]+1==ar[x][y]):
print("REPLACE",m-y+1,t[y])
x-=1
y-=1
elif(x and ar[x-1][y]+1==ar[x][y]):
print("DELETE",n-x+1)
x-=1
else:
print("INSERT",m-y+1,t[y])
y-=1
``` | instruction | 0 | 22,483 | 0 | 44,966 |
No | output | 1 | 22,483 | 0 | 44,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R
Submitted Solution:
```
s,t="$"+input(),"$"+input()
n,m=len(s)-1,len(t)-1
ar=[[0]*(m+1) for i in range(n+1)]
for i in range(1,n+1):ar[i][0]=i
for j in range(1,m+1):ar[0][j]=i
for i in range(1,n+1):
for j in range(1,m+1):
if(s[i]==t[j]):ar[i][j]=ar[i-1][j-1]
else:ar[i][j]=min(ar[i-1][j],ar[i-1][j-1],ar[i][j-1])+1
x,y=n,m
res=[]
while(x+y):
if(s[x]==t[y]):
x-=1
y-=1
elif(x and y and ar[x-1][y-1]+1==ar[x][y]):
res.append(["REPLACE",y,t[y]])
x-=1
y-=1
elif(x and ar[x-1][y]+1==ar[x][y]):
res.append(["DELETE",x])
x-=1
else:
res.append(["INSERT",y,t[y]])
y-=1
res=res[::-1]
print(len(res))
for e in res:
print(*e)
``` | instruction | 0 | 22,484 | 0 | 44,968 |
No | output | 1 | 22,484 | 0 | 44,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R
Submitted Solution:
```
s,t="$"+input(),"$"+input()
n,m=len(s)-1,len(t)-1
ar=[[0]*(m+1) for i in range(n+1)]
for i in range(1,n+1):ar[i][0]=i
for j in range(1,m+1):ar[0][j]=i
for i in range(1,n+1):
for j in range(1,m+1):
if(s[i]==t[j]):ar[i][j]=ar[i-1][j-1]
else:ar[i][j]=min(ar[i-1][j],ar[i-1][j-1],ar[i][j-1])+1
print(ar[n][m])
x,y=n,m
res=[]
while(x+y):
if(s[x]==t[y]):
x-=1
y-=1
elif(x and y and ar[x-1][y-1]+1==ar[x][y]):
res.append(["REPLACE",y,t[y]])
x-=1
y-=1
elif(x and ar[x-1][y]+1==ar[x][y]):
res.append(["DELETE",x])
x-=1
else:
res.append(["INSERT",y,t[y]])
y-=1
res=res[::-1]
for e in res:
print(*e)
``` | instruction | 0 | 22,485 | 0 | 44,970 |
No | output | 1 | 22,485 | 0 | 44,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a string s, consisting of capital Latin letters. Let's denote its current length as |s|. During one move it is allowed to apply one of the following operations to it:
* INSERT pos ch β insert a letter ch in the string s in the position pos (1 β€ pos β€ |s| + 1, A β€ ch β€ Z). The letter ch becomes the pos-th symbol of the string s, at that the letters shift aside and the length of the string increases by 1.
* DELETE pos β delete a character number pos (1 β€ pos β€ |s|) from the string s. At that the letters shift together and the length of the string decreases by 1.
* REPLACE pos ch β the letter in the position pos of the line s is replaced by ch (1 β€ pos β€ |s|, A β€ ch β€ Z). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a t string from an s string. You should also find the sequence of actions leading to the required results.
Input
The first line contains s, the second line contains t. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
Output
In the first line print the number of moves k in the given sequence of operations. The number should be the minimal possible one. Then print k lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
Examples
Input
ABA
ABBBA
Output
2
INSERT 3 B
INSERT 4 B
Input
ACCEPTED
WRONGANSWER
Output
10
REPLACE 1 W
REPLACE 2 R
REPLACE 3 O
REPLACE 4 N
REPLACE 5 G
REPLACE 6 A
INSERT 7 N
INSERT 8 S
INSERT 9 W
REPLACE 11 R
Submitted Solution:
```
# https://www.youtube.com/watch?v=MiqoA-yF-0M&feature=youtu.be
s, t = input(), input()
n, m = len(s), len(t)
M, camino = [], []
for _ in range(n+1):
M.append([0]*(m+1))
camino.append([None] * (m + 1))
for i in range(1, n+1):
M[i][0] = i
for j in range(1, m+1):
M[0][j] = j
for i in range(1, n+1):
for j in range(1, m+1):
distintos = s[i-1] != t[j-1]
m1, m2, m3 = M[i-1][j-1], M[i-1][j], M[i][j-1]
if not distintos:
M[i][j] = m1
camino[i][j] = (1, distintos)
continue
minimo = min(m1, m2, m3)
M[i][j] = minimo + 1
if minimo == m3:
camino[i][j] = (3, distintos)
elif minimo == m1:
camino[i][j] = (1, distintos)
else:
camino[i][j] = (2, distintos)
print(M[n][m])
instrucciones = []
i, j = n, m
while i*j != 0:
if camino[i][j][0] == 1:
if camino[i][j][1]:
instrucciones.append("REPLACE " + str(j) + " " + t[j-1])
i, j = i-1, j-1
elif camino[i][j][0] == 2:
if camino[i][j][1]:
instrucciones.append("DELETE " + str(j))
i -= 1
else:
if camino[i][j][1]:
instrucciones.append("INSERT " + str(j) + " " + t[j-1])
j -= 1
if i == 0:
for k in range(j-1, -1, -1):
instrucciones.append("INSERT " + str(k+1) + " " + t[k])
else:
for k in range(i-1, -1, -1):
instrucciones.append("DELETE " + str(1))
for instruccion in instrucciones[::-1]:
print(instruccion)
``` | instruction | 0 | 22,486 | 0 | 44,972 |
No | output | 1 | 22,486 | 0 | 44,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,508 | 0 | 45,016 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
a00, a01, a10, a11 = map(int, input().split())
numZeros = 0
numOnes = 0
ans = True
for n0 in range(1, 2*a00 + 1):
if n0 * (n0 - 1) == 2 * a00:
numZeros = n0
break
elif n0 * (n0 - 1) > 2 * a00:
ans = False
break;
for n1 in range(1, 2*a11 + 1):
if n1 * (n1 - 1) == 2 * a11:
numOnes = n1
break
elif n1 * (n1 - 1) > 2 * a11:
ans = False
break;
res = []
def generateResult(x,a01, a10, n0, n1):
while n0 > 0 or n1 > 0:
if a01 >= n1 and n1 > 0:
if n0 >= 1:
res.append(0)
a01 = a01 - n1
n0 = n0-1
else:
return
elif a10 >= n0 and n0 > 0:
if n1 >= 1:
res.append(1)
a10 = a10 - n0
n1 = n1-1
else:
return
elif n0 > 0 and n1 > 0:
return
elif 0 < n0 == a01 + a10 + n0 + n1:
for i in range(n0):
res.append(0)
n0 = 0
elif 0 < n1 == a01 + a10 + n0 + n1:
for i in range(n1):
res.append(1)
n1 = 0
else:
return
if a01 > 0 or a10 > 0:
numOnes = max(numOnes, 1)
numZeros = max(numZeros, 1)
if a00 == a01 == a10 == a11 == 0:
print(1)
elif ans:
generateResult(res, a01, a10, numZeros, numOnes)
if len(res) == numZeros + numOnes:
print("".join(map(str, res)))
else:
print("Impossible")
else:
print("Impossible")
``` | output | 1 | 22,508 | 0 | 45,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,509 | 0 | 45,018 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
root = lambda k : int((1 + (1 + 8*k) ** 0.5)/2)
def check(k):
n = root(k)
return n*(n-1)/2 == k
def solve(a00, a01, a10, a11):
s = a00 + a01 + a10 + a11
if not check(a00) or not check(a11) or not check(s):
return None
x, y, z = root(a00), root(a11), root(s)
if a00 == 0 or a11 == 0:
if s == 0:
return '0'
elif a10 == 0 and a01 == 0:
return '0'*z if a00 > 0 else '1'*z
elif a10 + a01 == z -1:
return '1'*a10 + '0' + '1'*a01 if a11 > 0 else '0'*a01 + '1' + '0'*a10
else:
return None
if x + y != z:
return None
if x == 0:
if a00 == 0 and a01 == 0 and a10 == 0:
return '1'*z
else:
return None
t, k = a10//x, a10%x
if z-t-x > 0:
return '1'*t + '0'*(x-k) + '1' + '0'*k + '1'*(z-t-x-1)
else:
return '1'*t + x*'0'
a00, a01, a10, a11 = [int(x) for x in input().split()]
ans = solve(a00, a01, a10, a11)
if ans is None:
print('Impossible')
else:
print(ans)
``` | output | 1 | 22,509 | 0 | 45,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,510 | 0 | 45,020 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
def main():
from itertools import product
def f(a):
x = int((a * 2. + .25) ** .5 + .51)
if x * (x - 1) != a * 2:
raise ValueError
return (x,) if a else (1, 0)
a00, a01, a10, a11 = map(int, input().split())
try:
for b, w in product(f(a00), f(a11)):
if b * w == a01 + a10:
break
else:
raise ValueError
except ValueError:
print("Impossible")
else:
a01, rest = divmod(a01, w) if w else (b, 0)
if rest:
l = ['0' * a01, '1' * (w - rest), '0', '1' * rest, '0' * (b - a01 - 1)]
else:
l = ['0' * a01, '1' * w, '0' * (b - a01)]
print(''.join(l))
if __name__ == '__main__':
main()
``` | output | 1 | 22,510 | 0 | 45,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,511 | 0 | 45,022 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
import sys
def BS(x):
l = 1
r = 1000000
while (r-l) > 1:
m = (l+r)//2
if m*(m-1)//2 > x:
r = m
else:
l = m
if l*(l-1)//2 != x:
print("Impossible")
sys.exit()
return l
a00,a01,a10,a11=map(int,input().split())
if (a00 + a01 + a10 + a11) == 0:
print("0")
sys.exit()
c0 = BS(a00)
c1 = BS(a11)
if a00==0 or a11==0:
if (a01 + a10) == 0:
if c0 == 1:
c0 = 0
if c1 == 1:
c1 = 0
print("0" * c0, end = "")
print("1" * c1)
sys.exit()
if (c0*c1) != (a01+a10):
print("Impossible")
sys.exit()
s = list("0" * (c0+c1))
for i in range(c0+c1):
if c0==0 or a01 < c1:
s[i] = "1"
a10 -+ c1
c1 -= 1
else:
a01 -= c1
c0 -= 1
print("".join(s))
``` | output | 1 | 22,511 | 0 | 45,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,512 | 0 | 45,024 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
import math
def get_cnt(res):
n = 1 + math.floor(math.sqrt(2 * res))
if n * (n-1) / 2 == res:
return n
else:
return -1
def calc():
cnt = list(map(int, input().split()))
if cnt == [0,0,0,0]:
return '0'
a = get_cnt(cnt[0])
b = get_cnt(cnt[3])
if cnt == [0,0,0,cnt[3]]:
a = 0
if cnt == [cnt[0],0,0,0]:
b = 0
if a == -1 or b == -1 or (a * b) != (cnt[1] + cnt[2]):
return "Impossible"
ans = ['0'] * (a + b)
i = 0
while b > 0:
while cnt[1] >= b:
i = i + 1
cnt[1] -= b
b -= 1
ans[i] = '1'
i += 1
return ''.join(ans)
print(calc())
``` | output | 1 | 22,512 | 0 | 45,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,513 | 0 | 45,026 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
a00, a01, a10, a11 = map(int, input().split())
numZeros = 0
numOnes = 0
ans = True
for n0 in range(1, 2*a00 + 1):
if n0 * (n0 - 1) == 2 * a00:
numZeros = n0
break
elif n0 * (n0 - 1) > 2 * a00:
ans = False
break;
for n1 in range(1, 2*a11 + 1):
if n1 * (n1 - 1) == 2 * a11:
numOnes = n1
break
elif n1 * (n1 - 1) > 2 * a11:
ans = False
break;
res = []
def generateResult(x,a01, a10, n0, n1):
while n0 > 0 or n1 > 0:
if a01 >= n1 and n1 > 0:
if n0 >= 1:
res.append(0)
a01 = a01 - n1
n0 = n0-1
else:
return
elif a10 >= n0 and n0 > 0:
if n1 >= 1:
res.append(1)
a10 = a10 - n0
n1 = n1-1
else:
return
elif n0 > 0 and n1 > 0:
return
elif 0 < n0 == a01 + a10 + n0 + n1:
for i in range(n0):
res.append(0)
n0 = 0
elif 0 < n1 == a01 + a10 + n0 + n1:
for i in range(n1):
res.append(1)
n1 = 0
else:
return
if a01 > 0 or a10 > 0:
numOnes = max(numOnes, 1)
numZeros = max(numZeros, 1)
if a00 == a01 == a10 == a11 == 0:
print(1)
elif a11 == 0 and a00 == 0 and a10 == 1 and a01 == 0:
print("10")
elif a11 == 0 and a00 == 0 and a01 == 1 and a10 == 0:
print("01")
elif ans:
generateResult(res, a01, a10, numZeros, numOnes)
if len(res) == numZeros + numOnes:
print("".join(map(str, res)))
else:
print("Impossible")
else:
print("Impossible")
``` | output | 1 | 22,513 | 0 | 45,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,514 | 0 | 45,028 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
def F(s):
x00 = x01 = x10 = x11 = 0
for i in range(len(s)):
for j in range(i + 1, len(s)):
cur = s[i] + s[j]
if cur == '00': x00 += 1
if cur == '01': x01 += 1
if cur == '10': x10 += 1
if cur == '11': x11 += 1
return x00, x01, x10, x11
def F2(s):
x00 = x01 = x10 = x11 = 0
c0 = s.count(0)
c1 = s.count(1)
x00 = c0 * (c0 - 1) // 2
x11 = c1 * (c1 - 1) // 2
cur0 = 0
cur1 = 0
for i in range(len(s)):
if s[i] == 0:
x10 += cur1
cur0 += 1
else:
x01 += cur0
cur1 += 1
return x00, x01, x10, x11
def fail():
print('Impossible')
exit()
a00, a01, a10, a11 = map(int, input().split())
f = lambda x: x * (x - 1) // 2
L, R = 0, 10 ** 6
while R - L > 1:
M = (L + R) // 2
if f(M) >= a00: R = M
else: L = M
c0 = R
L, R = 0, 10 ** 6
while R - L > 1:
M = (L + R) // 2
if f(M) >= a11: R = M
else: L = M
c1 = R
if a00 == 0 and a11 == 0:
if (a01, a10) == (1, 0): s = [0, 1]
elif (a01, a10) == (0, 1): s = [1, 0]
elif (a01, a10) == (0, 0): s = [1]
else: fail()
print(''.join(map(str, s)))
exit()
elif a00 == 0:
if (a01, a10) == (0, 0): c0 = 0
elif a11 == 0:
if (a01, a10) == (0, 0): c1 = 0
s = [0] * c0 + [1] * c1
b01 = c0 * c1
if b01 != a01 + a10: fail()
for i in range(c1):
if b01 - c0 < a01:
j = i + c0
break
b01 -= c0
s[i], s[i + c0] = s[i + c0], s[i]
while b01 > a01:
s[j], s[j - 1] = s[j - 1], s[j]
b01 -= 1
j -= 1
if F2(s) != (a00, a01, a10, a11): fail()
print(''.join(map(str, s)))
``` | output | 1 | 22,514 | 0 | 45,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110 | instruction | 0 | 22,515 | 0 | 45,030 |
Tags: constructive algorithms, greedy, implementation, math
Correct Solution:
```
def impossible():
print("Impossible")
exit()
def good(ones, zeros):
return ones * zeros == a01 + a10
a00, a01, a10, a11 = map(int, input().split())
if int(round((a00 * 8 + 1) ** 0.5)) ** 2 != a00 * 8 + 1:
impossible()
if int(round((a11 * 8 + 1) ** 0.5)) ** 2 != a11 * 8 + 1:
impossible()
zeros = 1 + int(round((a00 * 8 + 1) ** 0.5))
zeros //= 2
ones = 1 + int(round((a11 * 8 + 1) ** 0.5))
ones //= 2
#~ print(ones, zeros)
if ones == 1:
if not good(ones, zeros) and not good(ones - 1, zeros):
impossible()
elif good(ones - 1, zeros):
ones -= 1
#~ print(ones, zeros)
if zeros == 1:
if not good(ones, zeros) and not good(ones, zeros - 1):
impossible()
elif good(ones, zeros - 1) and not good(ones, zeros):
zeros -= 1
if zeros == 0 and ones == 0:
impossible()
if zeros * ones != a01 + a10:
impossible()
#~ print(zeros, ones)
if zeros != 0:
start = a10 // zeros
end = a01 // zeros
if a01 % zeros == 0 and a10 % zeros == 0:
print('1' * start + '0' * zeros + '1' * end)
else:
a01 %= zeros
a10 %= zeros
print('1' * start + a01 * '0' + '1' + a10 * '0' + '1' * end)
else:
start = a01 // ones
end = a10 // ones
if a01 % ones == 0 and a10 % ones == 0:
print('0' * start + '1' * ones + '0' * end)
else:
a01 %= ones
a10 %= ones
print('0' * start + a10 * '1' + '0' + a01 * '1' + '0' * end)
``` | output | 1 | 22,515 | 0 | 45,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110
Submitted Solution:
```
def impossible():
print("Impossible")
exit()
def good(ones, zeros):
return ones * zeros == a01 + a10
a00, a01, a10, a11 = map(int, input().split())
if int(round((a00 * 8 + 1) ** 0.5)) ** 2 != a00 * 8 + 1:
impossible()
if int(round((a11 * 8 + 1) ** 0.5)) ** 2 != a11 * 8 + 1:
impossible()
zeros = 1 + int(round((a00 * 8 + 1) ** 0.5))
zeros //= 2
ones = 1 + int(round((a11 * 8 + 1) ** 0.5))
ones //= 2
if ones == 1:
if not good(ones, zeros) and not good(ones - 1, zeros):
impossible()
elif good(ones - 1, zeros):
ones -= 1
if zeros == 1:
if not good(ones, zeros) and not good(ones, zeros - 1):
impossible()
elif good(ones, zeros - 1) and not good(ones, zeros):
zeros -= 1
if zeros == 0 and ones == 0:
impossible()
if zeros * ones != a01 + a10:
impossible()
if zeros != 0:
start = a10 // zeros
end = a01 // zeros
if a01 % zeros == 0 and a10 % zeros == 0:
print('1' * start + '0' * zeros + '1' * end)
else:
a01 %= zeros
a10 %= zeros
print('1' * start + a01 * '0' + '1' + a10 * '0' + '1' * end)
else:
print('1' * ones)
``` | instruction | 0 | 22,517 | 0 | 45,034 |
Yes | output | 1 | 22,517 | 0 | 45,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110
Submitted Solution:
```
#!/usr/bin/env python3.5
import sys
import math
def read_data():
return tuple(map(int, next(sys.stdin).split()))
def reverse_count(k):
d = math.sqrt(1 + 8*k)
n = int((1+d)/2 + .5)
if n*(n-1) == 2*k:
return n
return None
def solve1(a, b, x01, x10):
if x01 + x10 != a * b:
return None
characters = []
while a > 0 or b > 0:
if a >= 1 and x01 >= b:
a -= 1
x01 -= b
characters.append('0')
elif b >= 1 and x10 >= a:
b -= 1
x10 -= a
characters.append('1')
else:
return None
return ''.join(characters)
def solve(x00, x01, x10, x11):
if x00 == 0:
if x11 == 0:
if x01 == x10 == 0:
return "0"
return solve1(1, 1, x01, x10)
b = reverse_count(x11)
if b is not None:
return solve1(0, b, x01, x10) or solve1(1, b, x01, x10)
return None
if x11 == 0:
a = reverse_count(x00)
if a is not None:
return solve1(a, 0, x01, x10) or solve1(a, 1, x01, x10)
return None
a = reverse_count(x00)
b = reverse_count(x11)
if a is not None and b is not None:
return solve1(a, b, x01, x10)
return None
if __name__ == "__main__":
x00, x01, x10, x11 = read_data()
s = solve(x00, x01, x10, x11)
if s is None:
print("Impossible")
else:
print(s)
``` | instruction | 0 | 22,518 | 0 | 45,036 |
Yes | output | 1 | 22,518 | 0 | 45,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.
Examples
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output
0110
Submitted Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import sys,math
a00,a01,a10,a11=map(int,input().split())
s=a00+a01+a10+a11
if not s:
print('1')
sys.exit()
s=a01+a10
if a00:
z=a00<<1
x=math.ceil(math.sqrt(z))
if (x-1)*x!=z:
print('Impossible')
sys.exit()
else:x=s and 1
if a11:
z=a11<<1
y=math.ceil(math.sqrt(z))
if (y-1)*y!=z:
print('Impossible')
sys.exit()
else:y=s and 1
if x*y!=s:
print('Impossible')
sys.exit()
i=j=0
if s:i,j=divmod(a10,y)
print((x-i-1)*'0',j*'1','0',(y-j)*'1',i*'0',sep='')
``` | instruction | 0 | 22,522 | 0 | 45,044 |
No | output | 1 | 22,522 | 0 | 45,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string t is called nice if a string "2017" occurs in t as a subsequence but a string "2016" doesn't occur in t as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.
The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is - 1.
Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
Input
The first line of the input contains two integers n and q (4 β€ n β€ 200 000, 1 β€ q β€ 200 000) β the length of the string s and the number of queries respectively.
The second line contains a string s of length n. Every character is one of digits '0'β'9'.
The i-th of next q lines contains two integers ai and bi (1 β€ ai β€ bi β€ n), describing a substring in the i-th query.
Output
For each query print the ugliness of the given substring.
Examples
Input
8 3
20166766
1 8
1 7
2 8
Output
4
3
-1
Input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
Output
-1
2
1
-1
-1
Input
4 2
1234
2 4
1 2
Output
-1
-1
Note
In the first sample:
* In the first query, ugliness("20166766") = 4 because all four sixes must be removed.
* In the second query, ugliness("2016676") = 3 because all three sixes must be removed.
* In the third query, ugliness("0166766") = - 1 because it's impossible to remove some digits to get a nice string.
In the second sample:
* In the second query, ugliness("01201666209167") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice.
* In the third query, ugliness("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170".
Submitted Solution:
```
#
``` | instruction | 0 | 22,524 | 0 | 45,048 |
No | output | 1 | 22,524 | 0 | 45,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string t is called nice if a string "2017" occurs in t as a subsequence but a string "2016" doesn't occur in t as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.
The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is - 1.
Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
Input
The first line of the input contains two integers n and q (4 β€ n β€ 200 000, 1 β€ q β€ 200 000) β the length of the string s and the number of queries respectively.
The second line contains a string s of length n. Every character is one of digits '0'β'9'.
The i-th of next q lines contains two integers ai and bi (1 β€ ai β€ bi β€ n), describing a substring in the i-th query.
Output
For each query print the ugliness of the given substring.
Examples
Input
8 3
20166766
1 8
1 7
2 8
Output
4
3
-1
Input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
Output
-1
2
1
-1
-1
Input
4 2
1234
2 4
1 2
Output
-1
-1
Note
In the first sample:
* In the first query, ugliness("20166766") = 4 because all four sixes must be removed.
* In the second query, ugliness("2016676") = 3 because all three sixes must be removed.
* In the third query, ugliness("0166766") = - 1 because it's impossible to remove some digits to get a nice string.
In the second sample:
* In the second query, ugliness("01201666209167") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice.
* In the third query, ugliness("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170".
Submitted Solution:
```
``` | instruction | 0 | 22,525 | 0 | 45,050 |
No | output | 1 | 22,525 | 0 | 45,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string t is called nice if a string "2017" occurs in t as a subsequence but a string "2016" doesn't occur in t as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.
The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is - 1.
Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
Input
The first line of the input contains two integers n and q (4 β€ n β€ 200 000, 1 β€ q β€ 200 000) β the length of the string s and the number of queries respectively.
The second line contains a string s of length n. Every character is one of digits '0'β'9'.
The i-th of next q lines contains two integers ai and bi (1 β€ ai β€ bi β€ n), describing a substring in the i-th query.
Output
For each query print the ugliness of the given substring.
Examples
Input
8 3
20166766
1 8
1 7
2 8
Output
4
3
-1
Input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
Output
-1
2
1
-1
-1
Input
4 2
1234
2 4
1 2
Output
-1
-1
Note
In the first sample:
* In the first query, ugliness("20166766") = 4 because all four sixes must be removed.
* In the second query, ugliness("2016676") = 3 because all three sixes must be removed.
* In the third query, ugliness("0166766") = - 1 because it's impossible to remove some digits to get a nice string.
In the second sample:
* In the second query, ugliness("01201666209167") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice.
* In the third query, ugliness("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170".
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 6 22:53:14 2017
@author: kyle
"""
def de201(s):
a='2'
dic={'2':'0','0':'1','1':'done'}
s=list(s)
while len(s)>0 and a!='done':
if s[0]==a:
a=dic[a]
s.pop(0)
return s
def nice_str(s):
for i in list('34589'):
s=s.replace(i,'')
ends=de201(s)
if ends==[]:
return -1
elif '7' not in ends:
return -1
elif '6' in ends:
return ends.count('6')
l,n=map(int,input().split())
s=input()
for i in range(n):
a,b=map(int,input().split())
print(nice_str(s[a-1:b]))
``` | instruction | 0 | 22,526 | 0 | 45,052 |
No | output | 1 | 22,526 | 0 | 45,053 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,719 | 0 | 45,438 |
"Correct Solution:
```
from itertools import groupby
s = [{c[0]: len(list(c[1])) for c in groupby(sorted(input()))} for _ in range(int(input()))]
out = ''.join(sorted([key * min([sx.get(key, 0) for sx in s]) for key in s[0]]))
print(out)
``` | output | 1 | 22,719 | 0 | 45,439 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,720 | 0 | 45,440 |
"Correct Solution:
```
from collections import*;c=eval(("Counter(list(input()))&"*int(input()))[:-1]);print("".join(sorted(v*k for k,v in c.items())))
``` | output | 1 | 22,720 | 0 | 45,441 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,721 | 0 | 45,442 |
"Correct Solution:
```
n = int(input())
l = [[0]*30 for i in range(n)]
for i in range(n):
for c in list(input()): l[i][ord(c)-97]+=1
ans = ""
for i in range(30):
tmp = 50
for j in range(n):tmp=min(tmp, l[j][i])
ans += chr(i+97)*tmp
print(ans)
``` | output | 1 | 22,721 | 0 | 45,443 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,722 | 0 | 45,444 |
"Correct Solution:
```
n = int(input())
l = [input() for i in range(n)]
ans = ""
for i in range(97, 123):
now = chr(i)
temp = []
for j in l:
temp.append(j.count(now))
ans += now*min(temp)
print(ans)
``` | output | 1 | 22,722 | 0 | 45,445 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,723 | 0 | 45,446 |
"Correct Solution:
```
n = int(input())
s = [input() for _ in range(n)]
abc = list("abcdefghijklmnopqrstuvwxyz")
ans = ""
for i in range(26):
cnt = 10*8
for j in range(n):
cnt = min(cnt, s[j].count(abc[i]))
ans += abc[i]*cnt
print(ans)
``` | output | 1 | 22,723 | 0 | 45,447 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,724 | 0 | 45,448 |
"Correct Solution:
```
_,*s=open(0)
for c in sorted(set(s[0])):print(c*min(t.count(c)for t in s),end='')
``` | output | 1 | 22,724 | 0 | 45,449 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,725 | 0 | 45,450 |
"Correct Solution:
```
from collections import Counter
N = int(input())
S = [Counter(input()) for i in range(N)]
merged = S[0]
for s in S:
merged &= s
ans = ""
for (key, value) in merged.items():
ans += key * value
print("".join(sorted(ans)))
``` | output | 1 | 22,725 | 0 | 45,451 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output | instruction | 0 | 22,726 | 0 | 45,452 |
"Correct Solution:
```
from collections import Counter
n = int(input().strip())
S = [input().strip() for _ in range(n)]
s = Counter(S[0])
for i in range(1,n):
s = s & Counter(S[i])
ans = ''
for k,v in s.items():
ans += k*v
print(''.join(sorted(ans)))
``` | output | 1 | 22,726 | 0 | 45,453 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output
Submitted Solution:
```
from collections import Counter
n = int(input())
s = Counter(input())
for i in range(n - 1):
s = s & Counter(input())
ans = ''
for k, v in s.items():
ans += k * v
print(''.join(sorted(list(ans))))
``` | instruction | 0 | 22,728 | 0 | 45,456 |
Yes | output | 1 | 22,728 | 0 | 45,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output
Submitted Solution:
```
def main():
n = int(input())
S = []
A = [0] * n
for i in range(n):
S.append(input())
A[i] = S[i]
S[i] = set(S[i])
if i:
S[i] &= S[i - 1]
Ist = sorted(S[n - 1]) #intersection
M = []
for i in range(n):
c = ""
for j in range(len(A[i])):
if A[i][j] in Ist:
c += A[i][j]
M.append(c)
m_len = 51
ans = ""
for i in range(len(M)):
m_len = min(m_len, len(M[i]))
if(m_len == len(M[i])):
if ans:
ans = min(ans, M[i])
else:
ans = M[i]
if ans:
ans = sorted(ans)
for a in ans:
print(a, end = "")
print()
else:
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 22,732 | 0 | 45,464 |
No | output | 1 | 22,732 | 0 | 45,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output
Submitted Solution:
```
# ABC058C - ζͺζζΈ / Dubious Document (ARC071C)
from functools import reduce
def main():
N = int(input())
S = tuple(input() for _ in range(N))
ch = sorted(reduce(lambda a, b: set(a) & set(b), S))
ans = ""
for i in ch:
ans += i * min(s.count(i) for s in S)
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 22,733 | 0 | 45,466 |
No | output | 1 | 22,733 | 0 | 45,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.
He will receive a headline which contains one of the strings S_1,...,S_n tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.
Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.
Constraints
* 1 \leq n \leq 50
* 1 \leq |S_i| \leq 50 for every i = 1, ..., n.
* S_i consists of lowercase English letters (`a` - `z`) for every i = 1, ..., n.
Input
Input is given from Standard Input in the following format:
n
S_1
...
S_n
Output
Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.
Examples
Input
3
cbaa
daacc
acacac
Output
aac
Input
3
a
aa
b
Output
Submitted Solution:
```
n = int(input())
s = [sorted(list(input())) for i in range(n)]
rem = s[0]
for i in range(1,n):
tmp = rem[:]
for j in s[i]:
try:
tmp.remove(j)
except:
pass
for k in tmp:
try:
rem.remove(k)
except:
pass
print(''.join(rem))
``` | instruction | 0 | 22,734 | 0 | 45,468 |
No | output | 1 | 22,734 | 0 | 45,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,842 | 0 | 45,684 |
Tags: implementation
Correct Solution:
```
import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
# sys.stdin.readline()
def swap(s, ind):
ind -= 1
s = s[:ind] + s[ind+1] + s[ind] + s[ind+2:]
return s
if __name__ == "__main__":
# single variables
n = [int(val) for val in sys.stdin.readline().split()][0]
s = [str(val) for val in sys.stdin.readline().split()][0]
t = [str(val) for val in sys.stdin.readline().split()][0]
if(Counter(s) != Counter(t)):
print(-1)
else:
moves = []
for i in range(len(s)-1):
c = t[i]
ind = i
while(s[ind] != c):
ind += 1
for j in range(ind-1, i-1, -1):
s = swap(s, j+1)
moves.append(j+1)
print(len(moves))
for move in moves:
print(move, end=' ')
print('')
``` | output | 1 | 22,842 | 0 | 45,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,843 | 0 | 45,686 |
Tags: implementation
Correct Solution:
```
from __future__ import print_function
import sys
import math
import os.path
from copy import deepcopy
from functools import reduce
from collections import Counter, ChainMap, defaultdict
from itertools import cycle, chain
from queue import Queue, PriorityQueue, deque
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, nlargest, nsmallest
import bisect
from statistics import mean, mode, median, median_low, median_high
# CONFIG
sys.setrecursionlimit(10**9)
# LOG
def log(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# INPUT
def ni():
return map(int, input().split())
def nio(offset):
return map(lambda x: int(x) + offset, input().split())
def nia():
return list(map(int, input().split()))
# CONVERT
def toString(aList, sep=" "):
return sep.join(str(x) for x in aList)
def toMapInvertIndex(aList):
return {k: v for v, k in enumerate(aList)}
# SORT
def sortId(arr):
return sorted(range(len(arr)), key=lambda k: arr[k])
# MAIN
n, = ni()
s = input()
t = input()
def check():
ss = sorted(s)
tt = sorted(t)
# print (ss, tt)
return ss == tt
s = list(s)
t = list(t)
if check():
res = deque()
flag = [0]*n
for i in range(n):
pos = i
while s[pos] != t[i]:
pos += 1
if (pos != i):
#move from s[pos] -> t[i]
for ii in range(pos, i,-1):
s[ii], s[ii-1] = s[ii-1], s[ii]
res.append(ii)
# print(ii, pos)
# print(s)
# print(t)
# print()
print(len(res))
print(toString(res))
else:
print(-1)
``` | output | 1 | 22,843 | 0 | 45,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,844 | 0 | 45,688 |
Tags: implementation
Correct Solution:
```
n=int(input())
pop=list(input())
poop=list(input())
tam=[]
tom=[]
if sorted(pop)==sorted(poop):
for x in range(len(pop)):
if pop[x]==poop[x]:
pop[x]="565"
else:
tol=pop.index(poop[x])
ol=list(range(x+1,tol+1))
pop.remove(pop[tol])
pop.insert(x,poop[x])
for item in ol:
tam.append(item)
tam=tam[::-1]
for item in tam:
tom.append(item)
pop[x]="565"
tam=[]
print(len(tom))
print(*tom)
else:
print("-1")
``` | output | 1 | 22,844 | 0 | 45,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,845 | 0 | 45,690 |
Tags: implementation
Correct Solution:
```
def is_permutation(s, t):
return sorted(s) == sorted(t)
def solve(s, t, index):
global ans
if s == t and s == '':
return
if s[0] != t[0]:
j = 0
for i in range(1, len(s)):
if s[i] == t[0]:
j = i
break
l = list(s)
for i in range(j, 0, -1):
ans.append(i+index)
l[i], l[i-1] = l[i-1], l[i]
s = ''.join(l)
solve(s[1:], t[1:], index+1)
n = int(input())
s = input()
t = input()
ans = []
if not is_permutation(s, t):
print(-1)
else:
solve(s, t, 0)
print(len(ans))
print(*ans)
``` | output | 1 | 22,845 | 0 | 45,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,846 | 0 | 45,692 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = list(input())
t = list(input())
ans = 0
moves = []
for i in range(n):
if s[i] != t[i]:
if not (t[i] in s[i:]):
ans = -1
moves = []
break
ind = s[i:].index(t[i]) + i
del(s[ind])
s.insert(i, t[i])
ans += ind-i
k = ind-1
while k >= i:
moves.append(k+1)
k -= 1
print(ans)
for i in moves:
print(i, end = " ")
``` | output | 1 | 22,846 | 0 | 45,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,847 | 0 | 45,694 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = list(input())
t = list(input())
ans = []
for i in range(n):
if s[i]==t[i]: continue
x = -1
for j in range(i+1,n):
if t[i]==s[j]: x = j
if x==-1:
print(-1)
exit()
for j in range(x,i,-1):
s[j], s[j-1] = s[j-1], s[j]
ans.append(str(j))
print(len(ans))
print(' '.join(ans))
``` | output | 1 | 22,847 | 0 | 45,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.
You can successively perform the following move any number of times (possibly, zero):
* swap any two adjacent (neighboring) characters of s (i.e. for any i = \{1, 2, ..., n - 1\} you can swap s_i and s_{i + 1}).
You can't apply a move to the string t. The moves are applied to the string s one after another.
Your task is to obtain the string t from the string s. Find any way to do it with at most 10^4 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 10^4 or less to transform s into t.
Input
The first line of the input contains one integer n (1 β€ n β€ 50) β the length of strings s and t.
The second line of the input contains the string s consisting of n lowercase Latin letters.
The third line of the input contains the string t consisting of n lowercase Latin letters.
Output
If it is impossible to obtain the string t using moves, print "-1".
Otherwise in the first line print one integer k β the number of moves to transform s to t. Note that k must be an integer number between 0 and 10^4 inclusive.
In the second line print k integers c_j (1 β€ c_j < n), where c_j means that on the j-th move you swap characters s_{c_j} and s_{c_j + 1}.
If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.
Examples
Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1
Note
In the first example the string s changes as follows: "abcdef" β "abdcef" β "abdcfe" β "abdfce" β "abdfec".
In the second example there is no way to transform the string s into the string t through any allowed moves. | instruction | 0 | 22,848 | 0 | 45,696 |
Tags: implementation
Correct Solution:
```
def v(s,t):
for k in range(len(s)):
if s[k]!=t[k]:
return False
return True
n=int(input())
t=list(input())
s=list(input())
move=""
poss=1
count=0
for a in set(s):
if(s.count(a)!=t.count(a)):
poss=0
if(poss):
m={}
for k in range(len(s)):
if s[k] in m.keys():
m[s[k]].append(k)
else:m[s[k]]=[k]
s[k]=k
for k in range(len(t)):
if len(m[t[k]])==1:
t[k]=m[t[k]][0]
else:
ex=m[t[k]][0]
m[t[k]]=m[t[k]][1:]
t[k]=ex
#tri a bulle
while(not v(s,t)):
for k in range(len(t)-1):
if(t[k]>t[k+1]):
temp=t[k]
t[k]=t[k+1]
t[k+1]=temp
move+=str(k+1)+" "
count+=1
print(count)
if(count!=0):print(move)
else:print(-1)
``` | output | 1 | 22,848 | 0 | 45,697 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.