message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000
instruction
0
13,418
0
26,836
Tags: constructive algorithms, dp, graphs, greedy, sortings Correct Solution: ``` n=int(input()) s=input() rama=[] for i in range(n): rama.append(ord(s[i])-96) visit=[1 for i in range(27)] a=[] maxi=0 for i in range(n): p=visit[rama[i]] a.append(p) maxi=max(maxi,p) for j in range(rama[i]): visit[j]=max(visit[j],p+1) print(maxi) for i in a: print(i,end=' ') ```
output
1
13,418
0
26,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') # sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 def bisearch_min(mn, mx, func): ok = mx ng = mn while ng+1 < ok: mid = (ok+ng) // 2 if func(mid): ok = mid else: ng = mid return ok def check(m): if m == len(B): return True if B[m][-1][0] <= a: return True else: return False N = INT() A = [ord(c)-97 for c in input()] B = [[] for i in range(1)] B[0].append((A[0], 0)) for i, a in enumerate(A[1:], 1): idx = bisearch_min(-1, len(B), check) if idx == len(B): B.append([(a, i)]) else: B[idx].append((a, i)) ans = [0] * N for a, li in enumerate(B): for _, idx in li: ans[idx] = str(a) if len(B) <= 2: YES() print(''.join(ans)) else: NO() ```
instruction
0
13,419
0
26,838
Yes
output
1
13,419
0
26,839
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` n = int(input()) s = input() g = {} for i in range(len(s)): g[i] = [] for i in range(len(s)): for j in range(i): if s[j] > s[i]: g[i].append(j) g[j].append(i) colored = [False] * len(s) color = [None] * len(s) def brush(v, c): colored[v] = True color[v] = c for n in g[v]: if colored[n]: if color[n] == c: return False else: if not brush(n, c ^ 1): return False return True failed = False for i in range(len(s)): if not colored[i]: if not brush(i, 0): failed = True break if failed: print ('NO') else: print ('YES') print (''.join( map(lambda i: '1' if color[i] == 1 else '0', range(len(s))) )) ```
instruction
0
13,420
0
26,840
Yes
output
1
13,420
0
26,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` import io import os from collections import Counter, defaultdict, deque def solve(N, S): # Same color must be already sorted since they can't be swapped with each other # Greedily build increasing subsequences indices = [[0]] # last value -> which list for i, x in enumerate(S[1:], 1): for l in indices: if S[l[-1]] <= x: l.append(i) break else: indices.append([i]) ans = [None for i in range(N)] for color, l in enumerate(indices): for i in l: ans[i] = color # Format for E1 if len(indices) <= 2: return "YES\n" + "".join(map(str, ans)) else: return 'NO' # Format for E2 possible = str(len(indices)) return possible + "\n" + " ".join(str(x + 1) for x in ans) if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline (N,) = [int(x) for x in input().split()] S = input().decode().rstrip() ans = solve(N, S) print(ans) ```
instruction
0
13,421
0
26,842
Yes
output
1
13,421
0
26,843
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` def find(x): r = x while r != f[r]: r = f[r] while x != r: pre = f[x] f[x] = r x = pre return r a = input() n = int(a) a = input() str = list(a) id = [0] * n f = [0] * 2 * n name = [0] * 2 * n for i in range(n): id[i] = i for i in range(2 * n): f[i] = i name[i] = -1 Find = 0 for i in range(n): if Find: break for j in range(n - i - 1): if str[j] > str[j + 1]: str[j], str[j + 1] = str[j + 1], str[j] id[j], id[j + 1] = id[j + 1], id[j] fx = find(id[j]) fnx = find(id[j] + n) fy = find(id[j + 1]) fny = find(id[j + 1] + n) if fx == fy or fnx == fny: Find = 1 break else: f[fx] = fny f[fy] = fnx if Find: print('NO') else: print('YES') for i in range(n): fx = find(i) if name[fx] < 0: name[fx] = 0 if(fx >= n): name[find(fx - n)] = 1 else: name[find(fx + n)] = 1 print('{:d}'.format(name[fx]), end = '') ```
instruction
0
13,422
0
26,844
Yes
output
1
13,422
0
26,845
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` import math import sys from collections import defaultdict def stdinWrapper(): data = '''8 aaabbcbb ''' for line in data.split('\n'): yield line if '--debug' not in sys.argv: def stdinWrapper(): while True: yield input() inputs = stdinWrapper() def inputWrapper(): return next(inputs) def getType(_type): return _type(inputWrapper()) def getArray(_type): return [_type(x) for x in inputWrapper().split()] ''' Solution ''' l = getType(int) _str = getType(str) dp = [1] * l maxdp = [0] * 26 dp = [1] * len(_str) p = [-1] * len(_str) for i in range(1, len(_str)): for c in range(ord(_str[i])-ord('a')+1, 26): dp[i] = max(dp[i], maxdp[c]+1) maxdp[ord(_str[i]) - ord('a')] = max(dp[i], maxdp[ord(_str[i]) - ord('a')]) pos = -1 _max = 0 for i in range(len(_str)): if dp[i] > _max: _max = dp[i] pos = i result = [] while True: if pos == -1: break result.append(_str[pos]) pos = p[pos] print(max(dp)) print(' '.join([str(x) for x in dp])) ```
instruction
0
13,423
0
26,846
No
output
1
13,423
0
26,847
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` import sys import math from collections import defaultdict,Counter # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdout=open("CP1/output.txt",'w') # sys.stdin=open("CP1/input.txt",'r') # m=pow(10,9)+7 n=int(input()) s=list(input()) s2=s[:] s1=sorted(s) c=[0]*n # c1=[0]*n for j in range(1,n): if s[j]<s[j-1]: c[j]=1-c[j-1] # c1[j]=1-c1[j-1] elif s[j]==s[j-1]: c[j]=c[j-1] c2=''.join(list(map(str,c))) flag=0 # print(c) while s!=s1: for j in range(1,n): if s[j]<s[j-1]: if c[j]!=c[j-1]: c[j],c[j-1]=c[j-1],c[j] s[j],s[j-1]=s[j-1],s[j] else: flag=1 break if flag==1: break # for j in range(1,n): # if s[j]<s[j-1]: # c[j]=1-c[j-1] # else: # c[j]=c[j-1] # print(s) # print(c) if flag==0: print("YES") print(c2) else: print("NO") # else: # flag=0 # c2=''.join(list(map(str,c1))) # # c2=c1[:] # s=s2[:] # while s!=s1: # for j in range(1,n): # if s[j]<s[j-1]: # if c1[j]!=c1[j-1]: # c1[j],c1[j-1]=c1[j-1],c1[j] # s[j],s[j-1]=s[j-1],s[j] # else: # flag=1 # break # if flag==1: # break # if flag==0: # print("YES") # print(c2) # else: # print("NO") ```
instruction
0
13,424
0
26,848
No
output
1
13,424
0
26,849
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` from sys import stdin, stdout def main(): n = int(stdin.readline()) s = stdin.readline().rstrip() h1 = 'A' h2 = 'B' res = [] for i,x in enumerate(s): if i == 0: res.append("0") h1 = s[i] continue if s[i] < h1 and s[i] < h2: stdout.write("NO") return if s[i] > h1: h2 = 'B' h1 = s[i] res.append("0") elif s[i] > h2: h2 = s[i] res.append("1") else: res.append("1") stdout.write("YES\n" + "".join(res)) main() ```
instruction
0
13,425
0
26,850
No
output
1
13,425
0
26,851
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string s consisting of n lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer n (1 ≤ n ≤ 200) — the length of s. The second line of the input contains the string s consisting of exactly n lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of n characters, the i-th character should be '0' if the i-th character is colored the first color and '1' otherwise). Examples Input 9 abacbecfd Output YES 001010101 Input 8 aaabbcbb Output YES 01011011 Input 7 abcdedc Output NO Input 5 abcde Output YES 00000 Submitted Solution: ``` def binSearch(a, n): i, j = 0, len(a) - 1 while i != j: if (j - i) % 2 == 1: if a[i + ((j - i) // 2)] > n: i = i + (j - i) // 2 + 1 else: j = i + (j - i) // 2 else: if a[i + (j - i) // 2] > n: i = i + (j - i) // 2 else: j = i + (j - i) // 2 return i input() k = ['a', 'a'] ans = '' maxColor = 1 for symb in input(): i = binSearch(k, symb) ans += str(i + 1) if i + 1 > maxColor: maxColor = i + 1 k.append('a') k[i] = symb print(maxColor) print(*ans) ```
instruction
0
13,426
0
26,852
No
output
1
13,426
0
26,853
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,454
0
26,908
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` def solve(arr, n , m ) : fix = arr[0] for t in range(m) : ch = fix[t] for k in range(26) : fix[t] = chr(ord('a') + k ) flag = True for i in range(n) : count = 0 for j in range(m) : if arr[i][j] != fix[j] : count +=1 if count >1 : flag= False break if flag == True : return ''.join(fix) fix[t] = ch return -1 for T in range(int(input())) : n , m = list(map(int,input().split())) arr = [] for i in range(n) : arr.append(list(input())) v = solve(arr,n , m) print(v) ```
output
1
13,454
0
26,909
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,455
0
26,910
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` t=int(input()) for _ in range(t): n,m=map(int,input().split()) s=[] for _ in range(n): s.append(input()) ans=list(s[0]) l=[] for i in range(m): for j in range(26): ans[i]=chr(j+97) l.append(''.join(ans)) ans[i]=s[0][i] nf=0 for i in l: flag=0 for j in s: ct=0 for k in range(m): if j[k]!=i[k]: ct+=1 if ct>1: flag=1 break if flag==0: print(''.join(i)) nf=1 break if nf==0: print('-1') ```
output
1
13,455
0
26,911
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,456
0
26,912
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` import math import string def change(s, i, symbol): ans = s[0:i] + symbol + s[(i + 1):] return ans def check(a, n, m, s): ans = 0 for i in range(n): cnt = 0 for j in range(m): if(a[i][j] != s[j]): cnt += 1 if(cnt > 1): return 0 return 1 t = int(input()) for ttt in range(t): n, m = map(int, input().split()) a = [] for i in range(n): a.append(str(input())) s = a[0] ans = 0 for j in range(m): if(ans == 1): break for c in string.ascii_lowercase: now = change(s, j, c) if(check(a, n, m, now)): ans = 1 print(now) break if(ans == 0): print(-1) ```
output
1
13,456
0
26,913
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,457
0
26,914
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` import string def spystring(words): def neigh(word): candidates = {word} for i in range(len(word)): for c in string.ascii_lowercase: candidates.add(word[:i] + c + word[i + 1:]) return candidates intersection = neigh(words[0]) for word in words[1:]: intersection &= neigh(word) return intersection and intersection.pop() or -1 def solve(): n, m = map(int, input().split()) s = [input() for _ in range(n)] print(spystring(s)) for _ in range(int(input())): solve() ```
output
1
13,457
0
26,915
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,458
0
26,916
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` import sys import math from collections import defaultdict,deque import heapq t = int(sys.stdin.readline()) for _ in range(t): n,m = map(int,sys.stdin.readline().split()) l =[] for i in range(n): s = sys.stdin.readline()[:-1] l.append(s) #print(l,'l') ind = -1 z = True till = -1 for i in range(m): #z = True let = l[0][i] for j in range(n): if l[j][i] != let: z = False ind = j till = i break if not z: break if z: print(l[0]) continue z = True for k in range(26): ans = l[0][:till] + chr(97+k) + l[0][till+1:] z=True for i in range(n): cnt = 0 for j in range(m): if l[i][j] != ans[j]: cnt += 1 #print(cnt,'cnt',i,'i') if cnt > 1: z = False break if z: print(ans) break if z: continue z = True for k in range(26): ans = l[ind][:till] + chr(97+k) + l[ind][till+1:] z=True for i in range(n): cnt = 0 for j in range(m): if l[i][j] != ans[j]: cnt += 1 #print(cnt,'cnt',i,'i') if cnt > 1: z = False break if z: print(ans) break if not z: print(-1) ```
output
1
13,458
0
26,917
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,459
0
26,918
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` #import math #from functools import lru_cache #import heapq #from collections import defaultdict #from collections import Counter #from sys import stdout #from sys import setrecursionlimit from sys import stdin input = stdin.readline for Ti in range(int(input().strip())): n, m = [int(x) for x in input().strip().split()] sa = [] for ni in range(n): sa.append(input().strip()) s1 = sa[0] val = True ans = '' for m1 in range(m): for ci in list('abcdefghijklmnopqrstuvwwxyz'): s1 = sa[0] s1 = s1[:m1] + ci + s1[m1+1:] #print(s1) val = True for ni in range(n): dc = 0 for mi in range(m): if(sa[ni][mi]!=s1[mi]): dc+=1 if(dc>1): val=False break if val: ans = s1 break else: continue break if not ans: print('-1') else: print(ans) ```
output
1
13,459
0
26,919
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,460
0
26,920
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` # cook your dish here import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') import math import collections from sys import stdin,stdout,setrecursionlimit import bisect as bs setrecursionlimit(2**20) M = 10**9+7 T = int(stdin.readline()) # T = 1 def check(s1,s2): co = 0 for i in range(m): if(s1[i] != s2[i]): co += 1 if(co > 1): return False return True for _ in range(T): # n = int(stdin.readline()) n,m = list(map(int,stdin.readline().split())) # h = list(map(int,stdin.readline().split())) # q = list(map(int,stdin.readline().split())) # b = list(map(int,stdin.readline().split())) # s = stdin.readline().strip('\n') ss = [] for i in range(n): ss.append(list(stdin.readline().strip('\n'))) res = False for i in range(m): for a in range(26): c = chr(ord('a')+a) t = ss[0].copy() t[i] = c cc = 0 for j in range(1,n): if(not check(t,ss[j])): break cc += 1 if(cc == n-1): res = True break if(res): break if(res): print(''.join(t)) continue print(-1) ```
output
1
13,460
0
26,921
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist.
instruction
0
13,461
0
26,922
Tags: bitmasks, brute force, constructive algorithms, dp, hashing, strings Correct Solution: ``` import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): n,m = list(map(int,input().split())) arrys=[] for i in range(n): kap=list(input()) arrys.append(kap) ans=[] checkdiff=True for i in range(n): for j in range(i,n): if checkdiff: counter=0 for k in range(m): if arrys[i][k]!=arrys[j][k]: counter+=1 if counter>2: checkdiff=False break else: break if checkdiff: for j in range(m): counterdic={} for i in range(n): ele=arrys[i][j] freq=counterdic.get(ele,0) counterdic[ele]=freq+1 #print(counterdic,len(counterdic),arrys[0][j]) if len(counterdic)==1: ans.append(arrys[0][j]) else: ans.append(list(counterdic.keys())) pans=ans[0] for j in range(1,m): #print(pans,ans) tis=ans[j] #print(tis,pans,ans) tans=[] if len(tis)==1: for di in pans: di+=tis tans.append(di) else: for di in pans: for ti in tis: diu=di+ti tans.append(diu) #print(tans) pans=tans gotans=False for ina in pans: ans=True for i in range(n): counter=0 for j in range(m): #print(arrys) if ina[j]!=arrys[i][j]: counter+=1 if counter>=2: ans=False break if ans: yield ina gotans=True break if not gotans: yield -1 else: yield -1 #aaaab #abaaa #aaaab if __name__ == '__main__': t= int(input()) ans = gift() print(*ans,sep='\n') ```
output
1
13,461
0
26,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` #include <CodeforcesSolutions.h> #include <ONLINE_JUDGE <solution.cf(contestID = "1360",problemID = "F",method = "GET")>.h> """ Author : thekushalghosh Team : CodeDiggers I prefer Python language over the C++ language :p :D Visit my website : thekushalghosh.github.io """ import sys,math,cmath,time start_time = time.time() global tt #---------------------- USER DEFINED INPUT FUNCTIONS ----------------------# def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): return(input().strip()) def invr(): return(map(int,input().split())) ########################################################################## ################# ---- THE ACTUAL CODE STARTS BELOW ---- ################# def solve(): n,m = invr() a = [] for i in range(n): s = insr() a.append(s) b = [["0"] * len(a) for i in range(m)] for i in range(m): for j in range(n): b[i][j] = a[j][i] q = a[0] qw = 0 for i in range(m): qq = string_counter(b[i]) for j in range(26): w = q[:i] + chr(97 + j) + q[i + 1:] for k in range(n): ww = 0 for l in range(m): if w[l] != a[k][l]: ww = ww + 1 if ww < 2: True else: break else: print(w) qw = 1 break if qw == 1: break if qw == 0: print(-1) ################## ---- THE ACTUAL CODE ENDS ABOVE ---- ################## ########################################################################## def main(): global tt if not ONLINE_JUDGE: sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") t = 1 t = inp() for tt in range(t): solve() if not ONLINE_JUDGE: print("Time Elapsed :",time.time() - start_time,"seconds") sys.stdout.close() #------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------# def counter(a): q = [0] * max(a) for i in range(len(a)): q[a[i] - 1] = q[a[i] - 1] + 1 return(q) def string_counter(a): q = [0] * 26 for i in range(len(a)): q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1 return(q) ONLINE_JUDGE = __debug__ if ONLINE_JUDGE: input = sys.stdin.readline main() ```
instruction
0
13,463
0
26,926
Yes
output
1
13,463
0
26,927
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` ## necessary imports import sys input = sys.stdin.readline from math import * # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return res ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### def isValid(this): for i in range(n): count = 0; for j in range(m): if this[j] != a[i][j]: count += 1; if count > 1: return False return True; for _ in range(int(input())): n, m = int_array(); a = []; for __ in range(n): a.append(list(input().strip())); x = a[0]; f = 1; for i in range(m): if not f: break; for j in range(26): this = x.copy(); this[i] = chr(97 + j); if isValid(this): print(''.join(this)); f = 0; break; if f: print(-1); ```
instruction
0
13,464
0
26,928
Yes
output
1
13,464
0
26,929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` from copy import deepcopy t=int(input()) for _ in range(t): n,m=list(map(int,input().split())) s=[] for i in range(n): s.append(list(input())) def diff(a,b): count=0 for i in range(0,len(a)): if a[i]!=b[i]: count+=1 return count flag1=0 str=-1 temp = deepcopy(s[0]) for i in range(0,m): temp=deepcopy(s[0]) for c in 'qwertyuiopasdfghjklzxcvbnm': temp[i]=c # print(temp) flag=1 for l in s[1:]: if diff(l,temp)>1: flag=0 break if flag==1: flag1=1 break if flag1==1: break if flag1==1: print(''.join(temp)) else: print(-1) ```
instruction
0
13,465
0
26,930
Yes
output
1
13,465
0
26,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` for T in range(int(input())): n, m = [int(x) for x in input().split()] arr = [] for i in range(n): arr.append(list(input())) if n == 1: print(*arr[0], sep = '') continue first = arr[0] done = 0 for i in range(m): prev = first[i] for j in range(97, 97 + 26): ch = chr(j) first[i] = ch for k in range(1, n): bad = 0 for pos in range(m): if first[pos] != arr[k][pos]: bad += 1 if bad > 1: break if bad > 1: break else: print(*first, sep = '') done = 1 break if done: break if done: break else: first[i] = prev if not done: print(-1) ```
instruction
0
13,466
0
26,932
No
output
1
13,466
0
26,933
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` import sys try: sys.stdin = open('Input.txt', 'r') sys.stdout = open('Output.txt', 'w') except: pass for testCases in range(int(input())): s = [] n,m = map(int,input().split()) for i in range(n): s.append(list(input())) # print(s) # break ans = s[0] index = -1 char = '' limit = 0 flag = True checkAns = True for i in range(n): limit = 0 diff1 = 0 diff2 = 0 for j in range(m): # print(j,ans) if flag: if s[i][j] != ans[j]: if limit == 2: checkAns = False break if limit == 0: index = j char = s[i][j] limit +=1 else: temp1 = ans[:] temp1[index] = char temp2 = ans[:] temp2[j] = s[i][j] ans = [temp1,temp2] flag = False # print(ans) else: # print("YAY") if ans[0]!=[] and s[i][j]!=ans[0][j]: diff1 += 1 if ans[1]!=[] and s[i][j]!=ans[1][j]: diff2 += 1 if diff1>=2: ans[0] = [] if diff2>=2: ans[1] = [] if checkAns == False: break if checkAns: try: print("".join(ans)) except: if ans[0]!=[]: print("".join(ans[0])) elif ans[1]!=[]: print("".join(ans[1])) else: print(-1) else: print(-1) ```
instruction
0
13,467
0
26,934
No
output
1
13,467
0
26,935
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` def stringgen(arr,n,m): first=arr[0] for i in range(m): save=first[i] for ch in "abcdefghijklmnopqrstuvxyz": first[i]=ch flag=True for rest in arr[1:]: cnt=0 for x in range(m): if rest[x]!=first[x]: cnt+=1 if cnt>1: flag=False break if flag==True: return "".join(first) first[i]=save return -1 for i in range(int(input())): n,m=map(int,input().strip().split()) blanck=[] for j in range(n): blanck.append([i for i in input()]) print(stringgen(blanck,n,m)) ```
instruction
0
13,468
0
26,936
No
output
1
13,468
0
26,937
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` def solve(n, m, arr): if n == 1: print(arr[0]) return # s1 = arr[0] # s2 = "" s1, s2 = "", "" for i in range(1, n): diff = check(arr[0], arr[i]) if len(diff) <= 1: continue elif len(diff) == 2: idx1, idx2 = diff s1, s2 = arr[0], arr[i] s1 = s1[:idx1] + arr[i][idx1] + s1[idx1 + 1:] s2 = s2[:idx2] + arr[0][idx2] + s2[idx2 + 1:] break else: print(-1) return if s1 == "": print(arr[0]) return f1, f2 = True, True for s in arr: diff = check(s1, s) if len(diff) > 1: f1 = False break if f1: print(s1) return for s in arr: diff = check(s2, s) if len(diff) > 1: f2 = False break if f2: print(s2) return print(-1) return def check(s1, s2): diff = [] for i in range(len(s1)): if s1[i] != s2[i]: diff.append(i) return diff t = int(input()) for i in range(0, t): n, m = map(int, input().split()) arr = [] for j in range(n): arr.append(input()) solve(n, m, arr) ```
instruction
0
13,469
0
26,938
No
output
1
13,469
0
26,939
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n strings a_1, a_2, …, a_n: all of them have the same length m. The strings consist of lowercase English letters. Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string a_i, there is no more than one position j such that a_i[j] ≠ s[j]. Note that the desired string s may be equal to one of the given strings a_i, or it may differ from all the given strings. For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first. Input The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow. Each test case starts with a line containing two positive integers n (1 ≤ n ≤ 10) and m (1 ≤ m ≤ 10) — the number of strings and their length. Then follow n strings a_i, one per line. Each of them has length m and consists of lowercase English letters. Output Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes). Example Input 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c Output abab -1 aaa ab z Note The first test case was explained in the statement. In the second test case, the answer does not exist. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write mod=10**9+7 def ni(): return int(raw_input()) def li(): return map(int,raw_input().split()) def pn(n): stdout.write(str(n)+'\n') def pa(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code for t in range(ni()): n,m=li() l=[] for i in range(n): l.append(list(raw_input().strip())) f1=0 for i in range(n): ans=list(l[i]) f2=0 f3=0 for j in range(n): if i==j: continue f=0 for k in range(m): if ans[k]!=l[j][k]: #print i,j,k,'asdf' if not f2: ans[k]=l[j][k] f2=1 else: f+=1 if f>1: break if f>1: f3=1 break #print f,f3,f2,j,k,ans if not f3: f1=1 break if f1: pr(''.join(ans)+'\n') else: pn(-1) ```
instruction
0
13,470
0
26,940
No
output
1
13,470
0
26,941
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,888
0
27,776
Tags: implementation Correct Solution: ``` n = input() s = input() n = int(n) print('1'*min(s.count('1'), 1)+'0'*s.count('0')) ```
output
1
13,888
0
27,777
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,889
0
27,778
Tags: implementation Correct Solution: ``` n = int(input()) s = input() flag=0 ans="" for ele in s: if ele=='1' and flag==0: ans+=ele flag=1 if ele=='0': ans+=ele print(ans) ```
output
1
13,889
0
27,779
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,890
0
27,780
Tags: implementation Correct Solution: ``` n=int(input()) s=input() m='' counter=0 for i in range(n): if s[i]== '1': counter=counter+1 for j in range(n-counter): m=m+'0' if s=='0': print(0) else: print('1'+m) ```
output
1
13,890
0
27,781
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,891
0
27,782
Tags: implementation Correct Solution: ``` n=int(input()) s=input() while('01' in s or '11' in s): if('01' in s): s=s.replace('01','10') elif('11' in s): s=s.replace('11','1') print(s) ```
output
1
13,891
0
27,783
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,892
0
27,784
Tags: implementation Correct Solution: ``` num = int(input()) s = input() if "1" not in s: print(s) else: ans = "1" + s.count("0")*"0" print(ans) ```
output
1
13,892
0
27,785
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,893
0
27,786
Tags: implementation Correct Solution: ``` n=int(input()) s=input() if n==1: print(s) exit() s=list(s) zero=s.count('0') print('1'+'0'*zero) ```
output
1
13,893
0
27,787
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,894
0
27,788
Tags: implementation Correct Solution: ``` # int(input()) # [int(s) for s in input().split()] # input() def solve(): n = int(input()) s = input() if s.count("1") == 0: print(s) else: print("1"+"0"*(s.count("0"))) if __name__ == "__main__": solve() ```
output
1
13,894
0
27,789
Provide tags and a correct Python 3 solution for this coding contest problem. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use.
instruction
0
13,895
0
27,790
Tags: implementation Correct Solution: ``` n=int(input()) s=input() z=0 o=0 for c in s: if c=="1": o+=1 else: z+=1 t="" for i in range(z): t+="0" if o==0: print(t) else: t="1"+t print(t) ```
output
1
13,895
0
27,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n, s = input(), input() if s == '0': print(s) else: print('1','0'*s.count('0'),sep='') ```
instruction
0
13,896
0
27,792
Yes
output
1
13,896
0
27,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n=int(input()) s=input() print(s[0]+'0'*s[1:].count('0')) ```
instruction
0
13,897
0
27,794
Yes
output
1
13,897
0
27,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n = int(input()) s1 = input() if s1.count('1') == 0: print(0) else: print('1' + '0' * s1.count('0')) ```
instruction
0
13,898
0
27,796
Yes
output
1
13,898
0
27,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n = int(input()) a = input() while '01' in a or '11' in a: a = a.replace('01', '10') a = a.replace('11', '1') print(a) ```
instruction
0
13,899
0
27,798
Yes
output
1
13,899
0
27,799
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n=int(input()) s=input() count=0 for i in s: if(i=='0'): count+=1 print('1'+'0'*count) ```
instruction
0
13,900
0
27,800
No
output
1
13,900
0
27,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n = int(input()) s = input() z = 0 o = 0 for c in s: if c == '0': z += 1 else: o += 1 ans = None if o >= 1: ans = 1 << z else: ans = 0 s = "" while ans > 0: s = str(ans % 2) + s ans //= 2 print(s) ```
instruction
0
13,901
0
27,802
No
output
1
13,901
0
27,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n = int(input()) s = input() c = 2 j = 0 for i in s: if i == 1: c += 1 if n == 1: print(1) elif n % 2 == 0: print(1, end='') while j < (n - c): print(0, end = '') j += 1 else: print(1, end='') while j < (n - c): print(0, end = '') j += 1 print(1, end='') ```
instruction
0
13,902
0
27,804
No
output
1
13,902
0
27,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001". You are given a correct string s. You can perform two different operations on this string: 1. swap any pair of adjacent characters (for example, "101" <image> "110"); 2. replace "11" with "1" (for example, "110" <image> "10"). Let val(s) be such a number that s is its binary representation. Correct string a is less than some other correct string b iff val(a) < val(b). Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all). Input The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s. The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct. Output Print one string — the minimum correct string that you can obtain from the given one. Examples Input 4 1001 Output 100 Input 1 1 Output 1 Note In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100". In the second example you can't obtain smaller answer no matter what operations you use. Submitted Solution: ``` n = int(input()) B = input() z = 0 for c in B: z += c == '0' print('1'+'0'*z) ```
instruction
0
13,903
0
27,806
No
output
1
13,903
0
27,807
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes. String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|. String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1. Input The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters. Output Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces. Examples Input caaaba 5 1 1 1 4 2 3 4 6 4 5 Output 1 7 3 4 2 Note Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
instruction
0
14,465
0
28,930
Tags: dp, hashing, strings Correct Solution: ``` import os import sys from io import BytesIO, IOBase def main(): # n, k = map(int, input().split()) # s = input() s=input() n=len(s) palin = [[0 for _ in range(n)] for _ in range(n)] dp = [[0 for _ in range(n)] for _ in range(n)] for sz in range(n): for i in range(n - sz): j = i + sz if sz == 0: palin[i][j] = 1 elif s[i] == s[j]: if sz == 1: palin[i][j] = 1 else: palin[i][j] = palin[i + 1][j - 1] else: palin[i][j] = int(s[i] == s[j] and palin[i + 1][j - 1]) for sz in range(n): for i in range(n - sz): j = i + sz if sz == 0: dp[i][j] = 1 else: dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1] + palin[i][j] # print(*dp,sep='\n') for _ in range(int(input())): l, r = list(map(int, input().split())) print(dp[l - 1][r - 1]) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": for t in range(1):main()#int(input())): main() ```
output
1
14,465
0
28,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes. String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|. String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1. Input The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters. Output Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces. Examples Input caaaba 5 1 1 1 4 2 3 4 6 4 5 Output 1 7 3 4 2 Note Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba». Submitted Solution: ``` s=input() n=len(s) mark=[] dp=[] for i in range(n): mark.append([0]*n) dp.append([0]*n) for i in range(0,n): for j in range(0,n): if i+j>=n or i-j<0 :break if s[i-j]==s[i+j]: print(i-j,' ',i+j) mark[i-j][i+j]=1 else : break for j in range(1,n): if i+j>=n or i-j+1<0:break if s[i-j+1]==s[i+j]: print(i-j+1,' ',i+j) mark[i-j+1][i+j]=1 else : break for i in range(n-1,-1,-1): dp[i][i]=1 for j in range(i+1,n,1): dp[i][j]=dp[i+1][j]+dp[i][j-1]+mark[i][j] if i+1<=j-1:dp[i][j]-=dp[i+1][j-1] q=int(input()) while q>0: q-=1 A=list(map(int,input().split())) A[0]-=1 A[1]-=1 print(dp[A[0]][A[1]]) ```
instruction
0
14,466
0
28,932
No
output
1
14,466
0
28,933
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes. String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|. String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1. Input The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters. Output Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces. Examples Input caaaba 5 1 1 1 4 2 3 4 6 4 5 Output 1 7 3 4 2 Note Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba». Submitted Solution: ``` s=str(input()) dp=[[0 for i in range(len(s))]for j in range(len(s))] for i in range(len(s)): dp[i][i]=1 n=len(s) for i in range(n-1): if s[i]==s[i+1]: dp[i][i+1]=1 # print(dp) for i in range(2,n): for j in range(n-i): # print(i, # j) if s[j]==s[i+j] and dp[j+1][i+j-1]: dp[j][i+j]=1 a=[] for j in range(n): c=0 for i in range(n): c+=dp[i][j] a.append(c) prefix=[a[0]] for i in range(1,n): prefix.append(prefix[i-1]+a[i]) for _ in range(int(input())): u,v=map(int,input().split()) # print(u,v) print(prefix[v-1]-prefix[u-1]+1) ```
instruction
0
14,467
0
28,934
No
output
1
14,467
0
28,935
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes. String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|. String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1. Input The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters. Output Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces. Examples Input caaaba 5 1 1 1 4 2 3 4 6 4 5 Output 1 7 3 4 2 Note Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba». Submitted Solution: ``` s = input() n = len(s) + 1 dp = [[0] * n for i in range(n)] for i in range(n): p, q = i, i while q < n - 1 and p >= 0: if s[p] == s[q]: dp[p][q] += 1 p, q = p - 1, q + 1 for i in range(n): p, q = i, i + 1 while q < n - 1 and p >= 0: if s[p] == s[q]: dp[p][q] += 1 p, q = p - 1, q + 1 for i in range(n): for j in range(i + 1, n): dp[i][j] += dp[i][j - 1] for j in range(n): for i in range(j - 1, -1, -1): dp[i][j] += dp[i + 1][j] Q = int(input()) for i in range(Q): l, r = map(int, input().split()) print(dp[l - 1][r - 1]) ```
instruction
0
14,468
0
28,936
No
output
1
14,468
0
28,937
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes. String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|. String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1. Input The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters. Output Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces. Examples Input caaaba 5 1 1 1 4 2 3 4 6 4 5 Output 1 7 3 4 2 Note Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba». Submitted Solution: ``` s = input() n = len(s) + 1 dp = [[0] * n for i in range(n)] for i in range(n): p, q = i, i while q < n - 1 and p >= 0: if s[p] == s[q]: dp[p][q] += 1 p, q = p - 1, q + 1 for i in range(n): p, q = i, i + 1 while q < n - 1 and p >= 0: if s[p] == s[q]: dp[p][q] += 1 p, q = p - 1, q + 1 for i in range(n): for j in range(1, n): dp[i][j] += dp[i][j - 1] for j in range(n): for i in range(n - 2, -1, -1): dp[i][j] += dp[i + 1][j] Q = int(input()) for i in range(Q): l, r = map(int, input().split()) print(dp[l - 1][r - 1]) ```
instruction
0
14,469
0
28,938
No
output
1
14,469
0
28,939
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,796
0
29,592
"Correct Solution: ``` from string import ascii_lowercase from bisect import bisect_right s = input() t = input() p = {c: [] for c in s} for x, c in enumerate(s): p[c].append(x) z = 0 l = -1 for c in t: if c not in p: print(-1) break x = bisect_right(p[c], l) if x == len(p[c]): x = 0 z += 1 l = p[c][x] else: print(z * len(s) + l + 1) ```
output
1
14,796
0
29,593
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,797
0
29,594
"Correct Solution: ``` s=input() t=input() ans1=0 ans2=0 m=len(s) n=len(t) s=s+s for i in range(n): a=s.find(t[i],ans2) if a==-1: print(-1) exit() if a>=m: ans1+=1 ans2=a%m+1 print((ans1*m)+ans2) ```
output
1
14,797
0
29,595
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,798
0
29,596
"Correct Solution: ``` import bisect s = list(input()) t = list(input()) if len(set(t)-set(s)) > 0: print(-1) exit() c = set(s) dic = {} for char in c: dic[char] = [] for i in range(len(s)): dic[s[i]] += [i] last = -1 r = 0 for char in t: tmp = last idx = bisect.bisect_right(dic[char], last) if idx < len(dic[char]): last = dic[char][idx] if tmp == last: r += 1 last = dic[char][0] print(len(s)*r + last + 1) ```
output
1
14,798
0
29,597
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,799
0
29,598
"Correct Solution: ``` s = input() t = input() temp = s[:] cnt = 0 strmax = len(t) ans = 0 for i in t: if s.find(i)==-1: print("-1") exit(0) while cnt<strmax: num = temp.find(t[cnt]) if num != -1: ans+=num+1 temp = temp[num+1:] cnt+=1 else: ans+=len(temp) temp = s[:] print(ans) ```
output
1
14,799
0
29,599