message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` a, b = input(), input() i = 0 j = len(a) // len(b) e = a.count('?') from collections import Counter ca, cb = Counter(a), Counter(b) while i <= j: m = (i + j) // 2 if sum(cb[x] * m - min(cb[x] * m, ca[x]) for x in set(b)) <= e: i = m + 1 else: j = m - 1 r = [] for i in set(b): r.extend([*i * max(0, cb[i] * j - ca[i])]) ans = [*a] for i in range(len(a)): if ans[i] == '?': if len(r): ans[i] = r[-1] del r[-1] else: ans[i] = 'a' print(''.join(ans)) ```
instruction
0
5,548
0
11,096
Yes
output
1
5,548
0
11,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` import sys input = sys.stdin.readline def solve(): s = input().strip() t = input().strip() t_dict = {} s_dict = {} for c in t: if c in t_dict: t_dict[c] += 1 else: t_dict[c] = 1 s_dict[c] = 0 for c in s: if c in s_dict: s_dict[c] += 1 elif c != '?': s_dict[c] = 1 def good(x): return sum([max(0, x * t_dict[c] - s_dict[c]) for c in t_dict]) <= s.count('?') l = 0 r = len(s) + 1 while r - l > 1: m = (l + r) // 2 if good(m): l = m else: r = m s = list(s) chars = [c for c in t_dict] i = 0 for j in range(len(s)): while i < len(chars) and s_dict[chars[i]] >= l * t_dict[chars[i]]: i += 1 if i == len(chars) and s[j] == '?': s[j] = 'x' elif s[j] == '?': s[j] = chars[i] s_dict[chars[i]] += 1 return ''.join(s) print(solve()) ```
instruction
0
5,549
0
11,098
Yes
output
1
5,549
0
11,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` #Bhargey Mehta (Sophomore) #DA-IICT, Gandhinagar import sys, math, queue, bisect #sys.stdin = open("input.txt", "r") MOD = 10**9+7 sys.setrecursionlimit(1000000) def can(x): rem = ops for k in need: rem -= max(0, x*need[k]-have[k]) return rem >= 0 s = list(input()) t = input() need, have = {}, {} for ti in t: if ti in need: need[ti] += 1 else: have[ti] = 0 need[ti] = 1 ops = 0 for si in s: if si in have: have[si] += 1 elif si == '?': ops += 1 low, high = 0, len(s)//len(t) while low <= high: mid = (low+high)//2 if can(mid): ans = mid low = mid+1 else: high = mid-1 x = [] for k in need: for i in range(max(0, ans*need[k]-have[k])): x.append(k) j = 0 for i in range(len(s)): if s[i] != '?': continue if j == len(x): s[i] = 'z' else: s[i] = x[j] j += 1 print(''.join(s)) ```
instruction
0
5,550
0
11,100
Yes
output
1
5,550
0
11,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` s,t=input(),input() n=len(s) m=len(t) i=0 while i<n-m+1: v=[d for d in t if d not in s] k=s[i:i+m].count('?') if len(v)==k: for _ in range(k): s=s.replace('?',v.pop(0),1) i+=m else:i+=1 print(s.replace('?','z')) ```
instruction
0
5,551
0
11,102
No
output
1
5,551
0
11,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` s = input() t = input() Ds = {chr(97 + i) : 0 for i in range(26)} Ds["?"] = 0 Dt = {chr(97 + i) : 0 for i in range(26)} Dcnt = {chr(97 + i) : 0 for i in range(26)} A = list(Dt) A.sort() for u in s: Ds[u] += 1 for u in t: Dt[u] += 1 cnt = 0 for u in A: if Dt[u] == 0: continue Dcnt[u] = Dt[u] - Ds[u] cnt += Dcnt[u] while cnt + len(t) <= Ds["?"]: for a in A: Dcnt[a] += Dt[a] cnt += Dt[a] s = list(s) Dcnt["z"] += 1000000 for i,u in enumerate(s): if s[i] == "?": for a in A: if Dcnt[a] > 0: Dcnt[a] -= 1 s[i] = a break print("".join(s)) ```
instruction
0
5,552
0
11,104
No
output
1
5,552
0
11,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` s = input() t = input() nd = {} hv = {} for x in t: if x not in nd: nd[x]=1 else: nd[x]+=1 for x in s: if x not in hv: hv[x]=1 else: hv[x]+=1 def f(n): hp = 0 if("?" in hv): hp += hv["?"] for x in nd: if x not in hv: hp -=nd[x]*n else: if( hv[x] - nd[x]*n>=0): hp +=0 else: hp +=hv[x] - nd[x]*n if(hp<0): return False else: return True l =0 r = len(s)+100 while(r-1!=l): mid = (l+r)//2 if(f(mid)): l = mid else: r = mid def f1(n): hp = 0 if("?" in hv): hp += hv["?"] hv["?"] = 0 for x in nd: if(x in hv and hv[x]>=nd[x]): continue else: hv[x] = nd[x]*n #print(hv) f1(r) ans = "" #print(hv) for x in s: if(x!= "?"): hv[x]-=1 s = list(s) for x in range(len(s)): if(s[x] == "?"): for y in hv: if(hv[y]!=0): s[x] = y hv[y]-=1 break print("".join(s)) ```
instruction
0
5,553
0
11,106
No
output
1
5,553
0
11,107
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal. Input The first line contains string s (1 ≀ |s| ≀ 106). The second line contains string t (1 ≀ |t| ≀ 106). Output Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Examples Input ?aa? ab Output baab Input ??b? za Output azbz Input abcd abacaba Output abcd Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. Submitted Solution: ``` import sys input = sys.stdin.readline def solve(): s = input().strip() t = input().strip() t_dict = {} s_dict = {} for c in t: if c in t_dict: t_dict[c] += 1 else: t_dict[c] = 1 s_dict[c] = 0 for c in s: if c in s_dict: s_dict[c] += 1 elif c != '?': s_dict[c] = 1 def good(x): return sum([x * t_dict[c] - s_dict[c] for c in t_dict]) <= s.count('?') l = 0 r = len(s) + 1 while r - l > 1: m = (l + r) // 2 if good(m): l = m else: r = m s = list(s) chars = [c for c in t_dict] i = 0 for j in range(len(s)): while i < len(chars) and s_dict[chars[i]] >= l * t_dict[chars[i]]: i += 1 if i == len(chars) and s[j] == '?': s[j] = 'x' elif s[j] == '?': s[j] = chars[i] s_dict[chars[i]] += 1 return ''.join(s) print(solve()) ```
instruction
0
5,554
0
11,108
No
output
1
5,554
0
11,109
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≀ n, a, b ≀ 5000) β€” the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer β€” the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
5,827
0
11,654
Tags: dp, strings Correct Solution: ``` def f(x): z=[0 for i in range(len(x))] l=0 r=0 for i in range(1,len(x)): if i<=r: z[i]=min(z[i-l],r-i+1) while i+z[i]<len(x) and x[z[i]]==x[i+z[i]]: z[i]+=1 if i+z[i]-1>r: l,r=i,i+z[i]-1 return z a=list(map(int,input('').split())) n,a,b=a[0],a[1],a[2] s=input('') dp=[0 for i in range(n)] dp[0]=a for i in range(1,n): t=s[:i+1] dp[i]=dp[i-1]+a q=f(t[::-1]) maxs=[0 for j in range(i+1)] maxs[0]=q[i] for j in range(1,i): maxs[j]=max(maxs[j-1],q[i-j]) for j in range(i): if maxs[j]>=i-j: dp[i]=min(dp[i],dp[j]+b) print(dp[len(dp)-1]) ```
output
1
5,827
0
11,655
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≀ n, a, b ≀ 5000) β€” the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer β€” the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
5,828
0
11,656
Tags: dp, strings Correct Solution: ``` n, a, b = map(int, input().split()) s = input() lcp = [[0]*n for _ in ' '*n] for i in range(n-1, -1, -1): for r in range(n-1, -1, -1): if s[i] == s[r]: if i == n - 1 or r == n - 1: lcp[i][r] = 1 else: lcp[i][r] = lcp[i+1][r+1]+1 d = [10**10]*(n+1) d[0] = 0 for i in range(n): d[i+1] = min(d[i+1], d[i]+a) for j in range(i): k = min(lcp[i][j], i-j) d[i+k] = min(d[i+k], d[i]+b) print(d[-1]) ```
output
1
5,828
0
11,657
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,893
0
11,786
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` __author__ = 'Utena' from heapq import heappushpop, heapify,heappop,heappush string=input() s=len(string) k=int(input()) total=0 line=[] for i in range(s): line.append((string[i],i)) heapify(line) while total<k: value,key=heappop(line) ans=value total+=1 if key<s-1:heappush(line,(value+string[key+1],key+1)) if len(line)==0: if total<k: print('No such line.') exit(0) print(ans) ```
output
1
5,893
0
11,787
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,894
0
11,788
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` s=input() l=len(s) k=int(input()) if l*(l+1)/2<k: print('No such line.') else: a=[i for i in range(l)] p=0 ans='' while True: t=[(s[i+p],i) for i in a] t=sorted(t) i=j=0 prod=0 while i<len(t): pre=prod while j<len(t) and t[i][0]==t[j][0]: prod+=l-t[j][1]-p j+=1 if prod>=k: break i=j ans+=t[i][0] if pre+j-i>=k: break k-=pre+j-i p+=1 a=[t[q][1] for q in range(i,j) if t[q][1]+p<l] print(ans) ```
output
1
5,894
0
11,789
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,895
0
11,790
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` import heapq as hq s = input() k = int(input()) n = len(s) news = [([s[i]], i + 1) for i in range(n)] if k > n * (n + 1) // 2: print('No such line.') exit() hq.heapify(news) for i in range(k): t, nxt = hq.heappop(news) if nxt < n and i + 1 < k: t.append(s[nxt]) hq.heappush(news, (t, nxt + 1)) print(''.join(t)) ```
output
1
5,895
0
11,791
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,896
0
11,792
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` from heapq import * s=input() k=int(input()) n=len(s) ss=[(s[i],i) for i in range(n)] heapify(ss) if k>n*(n+1)/2: print('No such line.') else: while k: k-=1 t=heappop(ss) if k==0: print(t[0]) else: if t[1]<len(s)-1: heappush(ss,(t[0]+s[t[1]+1],t[1]+1)) ```
output
1
5,896
0
11,793
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,897
0
11,794
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` import sys,heapq x=input(); k=int(input()); i=0; h=[(x[i],i+1) for i in range(len(x))]; heapq.heapify(h); while(True): i=i+1; if(not h): print("No such line."); sys.exit(); s,next=heapq.heappop(h); if(i==k): print(s); break; if(next>=len(x)): continue s=s+x[next] heapq.heappush(h,(s,next+1)) ```
output
1
5,897
0
11,795
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,898
0
11,796
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` from heapq import * l=input() k=int(input()) n=len(l) if k>n*(n+1)/2: print("No such line.") quit() ss=[(l[i],i) for i in range(n)] heapify(ss) while k: k-=1 t=heappop(ss) if k==0: print(t[0]) else: if t[1]<n-1: heappush(ss,(t[0]+l[t[1]+1],t[1]+1)) ```
output
1
5,898
0
11,797
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,899
0
11,798
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` import heapq from heapq import heappush, heappop, heappushpop a = input() n = int(input()) c=list(a) l = len(c) num = 0 if (l*l+l)/2 < n: print('No such line.') else: d = [] # for i in range(l): heappush(d,(c[i],i)) while num < n: num += 1 t,f = heappop(d) if f<l-1: heappush(d,(t+c[f+1],f+1)) print(t) # for i in range(l): # for j in range(1,l+1): # if i+j<=l: # d.append(a[i:i+j]) #print(d)# # for i in range(n-1): # d.remove(min(d)) # print(d)# # print(min(d)) ```
output
1
5,899
0
11,799
Provide tags and a correct Python 3 solution for this coding contest problem. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
instruction
0
5,900
0
11,800
Tags: brute force, constructive algorithms, hashing, implementation, string suffix structures, strings Correct Solution: ``` ''' Created on 2016-4-28 @author: chronocorax ''' from heapq import heapify, heappush, heappop S = input() k = int(input()) n = len(S) H = [([S[i]], i + 1) for i in range(n)] if k > n * (n + 1) // 2: print('No such line.') exit() heapify(H) for i in range(k): s, nxt = heappop(H) if nxt < n and i + 1 < k: s.append(S[nxt]) heappush(H, (s, nxt + 1)) print(''.join(s)) ```
output
1
5,900
0
11,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` import heapq string = input() k = int(input()) if k > (len(string) + 1) * len(string) / 2: print("No such line.") else: substring = [] for i in range(len(string)): heapq.heappush(substring,(string[i],i + 1)) for i in range(k): a,b = heapq.heappop(substring) if b < len(string): heapq.heappush(substring,(a + string[b],b + 1)) print(a) ```
instruction
0
5,901
0
11,802
Yes
output
1
5,901
0
11,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` import heapq s = input() k = int(input()) N = len(s) if k>N*(N+1)//2: print('No such line.') exit() A = [(c,j) for j,c in enumerate(list(s))] heapq.heapify(A) for i in range(k): if not A: break min_item, index = heapq.heappop(A) if index + 1 < N: heapq.heappush(A, (min_item + s[index+1], index+1)) print(min_item) ```
instruction
0
5,902
0
11,804
Yes
output
1
5,902
0
11,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` from heapq import * string = input() k = int(input()) m = k t = len(string) u = t * (t + 1) / 2 if k > u: print('No such line.') exit() substring = [[string[i],i] for i in range(t)] heapify(substring) while m > 0: m -= 1 a = heappop(substring) if a[1] < t - 1: b = a[0] + string[a[1]+1] heappush(substring,[b, a[1] + 1]) print(a[0]) ```
instruction
0
5,903
0
11,806
Yes
output
1
5,903
0
11,807
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` from heapq import heapify, heappush, heappop n = input() k = int(input()) i = 0 h = [(n[i], i + 1) for i in range(len(n))] heapify(h) while True: i += 1 if (not h): print("No such line.") exit(0) s, nxt = heappop(h) if (i == k): print(s) exit(0) if (nxt >= len(n)): continue s = s + n[nxt] heappush(h, (s, nxt + 1)) ```
instruction
0
5,904
0
11,808
Yes
output
1
5,904
0
11,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` from heapq import * l=input() k=int(input()) n=len(l) if k>n*(n+1)/2: print("No such line.") quit() ss=[(l[i],i) for i in range(n)] while k: k-=1 t=heappop(ss) if k==0: print(t[0]) else: if t[1]<n-1: heappush(ss,(t[0]+l[t[1]+1],t[1]+1)) ```
instruction
0
5,905
0
11,810
No
output
1
5,905
0
11,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` # -*-encoding=gbk-*- ch = input() K = int(input() ) if len(ch)*(len(ch) + 1)/2 < K : print ("No such line.") else: nn = len(ch) pos = [i for i in range(nn)] print(pos) ich = [ord(i) for i in ch] print(ich) ans = "" while True: num = [0] * 128 for i in pos : num[ ich[i] ] += nn - i print(num[ich[i]],i) addChar = 0 for c in range(ord('a'), ord('z')+1): if num[c] >= K: addChar = c break K -= num[c] pos = [i for i in pos if ich[i]==addChar] ans += chr(addChar) if len(pos) >= K: break K -= len(pos) pos = [i+1 for i in pos if i+1 < nn] print (ans) ```
instruction
0
5,906
0
11,812
No
output
1
5,906
0
11,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` string = input() n = len(string) s = int(input()) l = [] q = 0 if n > 20: print('a') q = 1 for i in range (0,n): if q == 1: exit for o in range (i,n): k = string[i:o+1] while len(k)<n: k += "`" kk = "" for p in range(0,n): kk +=str(ord(k[p])-30) l.append(int(kk)) l.sort() out = str(l[s-1]) out += "66" outt = "" for i in range(0,n): if q==1: exit outt += chr(int(out[2*i:2*i+2])+30) if int(out[2*i+2:2*i+4]) == 66: break if q !=1: print(outt) ```
instruction
0
5,907
0
11,814
No
output
1
5,907
0
11,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x β‰  y), or there exists such i (1 ≀ i ≀ min(|x|, |y|)), that xi < yi, and for any j (1 ≀ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≀ k ≀ 105). Output Print the string Anna and Maria need β€” the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes). Examples Input aa 2 Output a Input abc 5 Output bc Input abab 7 Output b Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b". Submitted Solution: ``` ''' Created on 2016-4-28 @author: chronocorax ''' from heapq import heapify, heappush, heappop S = input() k = int(input()) n = len(S) H = [([S[i]], i + 1) for i in range(n)] if k > n * (n + 1) // 2: print('No such line.') exit() heapify(H) for _ in range(k): s, nxt = heappop(H) res = s if nxt < n: s.append(S[nxt]) heappush(H, (s, nxt + 1)) print(''.join(res)) ```
instruction
0
5,908
0
11,816
No
output
1
5,908
0
11,817
Provide tags and a correct Python 2 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,909
0
11,818
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations from fractions import gcd raw_input = stdin.readline pr = stdout.write def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): for i in arr: stdout.write(str(i)+' ') stdout.write('\n') range = xrange # not for python 3.0+ for t in range(input()): n,k=in_arr() s=raw_input().strip() d=defaultdict(Counter) for i in range(n): d[i%k][s[i]]+=1 c=n/k ans=0 for i in range(k/2): mn=1000000000 for j in [chr(s) for s in range(97,97+26)]: sm=d[i][j]+d[k-i-1][j] #print i,j,sm if (2*c)-sm<mn: mn=(2*c)-sm ans+=mn if k%2: mn=1000000000 for j in [chr(s) for s in range(97,97+26)]: mn=min(mn,(c)-d[(k/2)][j]) ans+=mn pr_num(ans) ```
output
1
5,909
0
11,819
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,910
0
11,820
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` from collections import Counter for lp in range(int(input())): n,k=map(int,input().split()) s=input() q,t=0,1 for j in range((k+1)//2): l=[] for i in range(j,n,k):l.append(s[i]) if k%2==0 or j!=(k+1)//2-1: for i in range(k-j-1,n,k):l.append(s[i]) else:t=0 q+=(n//k)*(1+t)-Counter(l).most_common(1)[0][1] print(q) ```
output
1
5,910
0
11,821
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,911
0
11,822
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` from sys import stdin from collections import defaultdict, Counter from math import ceil t = int(input()) for _ in range(t): n, k = map(int, stdin.readline().split()) S = stdin.readline().strip() count = 0 sub_k = [] for i in range(n//k): sub_k.append(S[k*i:k*i + k]) for i in range(int(ceil(k/2))): c = Counter() for j in range(n//k): if i != k - 1 - i: c[sub_k[j][i]] += 1 c[sub_k[j][k - 1 - i]] += 1 else: c[sub_k[j][i]] += 1 M = max(c.values()) if i != k - 1 - i: count += 2*(n//k) - M else: count += n//k - M print (count) ```
output
1
5,911
0
11,823
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,912
0
11,824
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` #input = raw_input from collections import Counter def answer(): n, k = map(int, input().split(" ")) s = input() bunkatu_s_list = [s[i:i+k] for i in range(0, n, k)] bunkatu_n = n // k half_s_list = [] out = 0 if k % 2 == 0: half_k = k // 2 for i in range(half_k): i1 = i i2 = k - (i+1) #tmp1 = [s[i] for i in range(i1, n, k)] tmp1 = s[i1::k] tmp2 = s[i2::k] #tmp2 = [s[i] for i in range(i2, n, k)] tmp = len(tmp1) + len(tmp2) - Counter(tmp1+tmp2).most_common(1)[0][1] out += tmp aidas = None else: half_k = (k-1) // 2 for i in range(half_k): i1 = i i2 = k - (i+1) #tmp1 = [s[i] for i in range(i1, n, k)] #tmp2 = [s[i] for i in range(i2, n, k)] tmp1 = s[i1::k] tmp2 = s[i2::k] tmp = len(tmp1) + len(tmp2) - Counter(tmp1+tmp2).most_common(1)[0][1] out += tmp aidas = [bunkatu_s[half_k] for bunkatu_s in bunkatu_s_list] tmp = bunkatu_n - max(Counter(aidas).values()) out += tmp print(out) mm = int(input()) for _ in range(mm): answer() ```
output
1
5,912
0
11,825
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,913
0
11,826
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` from sys import stdin inp = stdin.readline t = int(inp().strip()) for c in range(t): n, k = [int(x) for x in inp().strip().split()] array = list(inp().strip()) d1 = {} for i in range(n): if i%k < k/2: dIndex = k - 1 - i%k else: dIndex = i%k if not d1.get(dIndex, 0): d1[dIndex] = {array[i]:1} elif not d1[dIndex].get(array[i],0): d1[dIndex][array[i]] = 1 else: d1[dIndex][array[i]] += 1 number = n//k numberOfMoves = 0 for i in d1.values(): maximum = 0 for j in i.values(): if j > maximum: maximum = j numberOfMoves += (2 * number - maximum) if k%2: numberOfMoves -= number print(numberOfMoves) ```
output
1
5,913
0
11,827
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,914
0
11,828
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` from collections import Counter import sys sys.setrecursionlimit(10 ** 5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): for _ in range(II()): n,k=MI() aa=[ord(c) for c in SI()] fin=[False]*k ans=0 for m1 in range(k): if fin[m1]:continue m2=(n-m1-1)%k if m1==m2: cnt = Counter(aa[m1::k]) fin[m1] = True ans += n//k - max(cnt.values()) else: cnt=Counter(aa[m1::k]+aa[m2::k]) fin[m1]=fin[m2]=True ans+=2*n//k-max(cnt.values()) print(ans) main() ```
output
1
5,914
0
11,829
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,915
0
11,830
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` import os, sys, bisect, copy from collections import defaultdict, Counter, deque from functools import lru_cache #use @lru_cache(None) if os.path.exists('in.txt'): sys.stdin=open('in.txt','r') if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w') # def input(): return sys.stdin.readline() def mapi(arg=0): return map(int if arg==0 else str,input().split()) #------------------------------------------------------------------ cnt = [[0]*26 for i in range(200005)] par = [0]*200005 size = [0]*200005 def parent(v): if v==par[v]: return v par[v]= parent(par[v]) return par[v] def merge(u,v): a = parent(u) b = parent(v) if a!=b: if size[a]<size[b]: a,b = b,a par[b] = a size[a] += size[b] for i in range(26): cnt[a][i]+=cnt[b][i] for _ in range(int(input())): n,k = mapi() a = list(input().strip()) cnt = [[0]*26 for i in range(n+1)] par = [0]*(n+1) size = [0]*(n+1) for i in range(n): cnt[i][ord(a[i])-ord("a")]+=1 par[i] = i size[i] = 1 for i in range(n): if i < n-i-1: merge(i,n-i-1) if i+k < n: merge(i,i+k) res = 0 for i in range(n): if par[i]==i: mx = -1 for j in range(26): mx = max(mx, cnt[i][j]) res+=size[i]-mx print(res) ```
output
1
5,915
0
11,831
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,916
0
11,832
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# #vsInput() for _ in range(Int()): n,k=value() s=input() votes=defaultdict(lambda : defaultdict(int)) ans=defaultdict(lambda: ['',-1]) for i in range(0,n,k): l=i r=i+k-1 index=1 while(l<=r): if(r!=l): votes[index][s[l]]+=1 votes[index][s[r]]+=1 if(r==l): votes[index][s[l]]+=1 if(votes[index][s[l]] > ans[index][1]): ans[index][0]=s[l] ans[index][1]=votes[index][s[l]] if(votes[index][s[r]] > ans[index][1]): ans[index][0]=s[r] ans[index][1]=votes[index][s[r]] l+=1 r-=1 index+=1 #print(votes) #print(ans) count=0 for i in range(0,n,k): l=i r=i+k-1 index=1 while(l<=r): if(l!=r): if(s[l]!=ans[index][0]): count+=1 if(s[r]!=ans[index][0]): count+=1 else: if(s[l]!=ans[index][0]): count+=1 l+=1 r-=1 index+=1 print(count) ```
output
1
5,916
0
11,833
Provide tags and a correct Python 3 solution for this coding contest problem. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete.
instruction
0
5,917
0
11,834
Tags: dfs and similar, dsu, greedy, implementation, strings Correct Solution: ``` def solve(s, k): n = len(s) r = n//k res = 0 for j in range(k//2): d = [0] * 26 for i in range(r): d[ord(s[i*k+j]) - 97] += 1 d[ord(s[i*k+k-1-j]) - 97] += 1 res += 2*r - max(d) if k%2 == 1: d = [0] * 26 for i in range(r): d[ord(s[i*k+k//2]) - 97] += 1 res += r - max(d) return res if __name__ == '__main__': t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() print(solve(s, k)) ```
output
1
5,917
0
11,835
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` from sys import stdin inp = stdin.readline t = int(inp().strip()) for c in range(t): n, k = [int(x) for x in inp().strip().split()] array = list(inp().strip()) d1 = {} for i in range(n): if i%k < k/2: dIndex = k - 1 - i%k else: dIndex = i%k if not d1.get(dIndex, 0): d1[dIndex] = {array[i]:1} elif not d1[dIndex].get(array[i],0): d1[dIndex][array[i]] = 1 else: d1[dIndex][array[i]] += 1 number = n//k numberOfMoves = 0 for i in d1.keys(): maximum = 0 for j in d1[i].keys(): if d1[i][j] > maximum: maximum = d1[i][j] if k%2 == 1 and i == k//2: numberOfMoves += (number - maximum) else: numberOfMoves += (2 * number - maximum) print(numberOfMoves) ```
instruction
0
5,918
0
11,836
Yes
output
1
5,918
0
11,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` import sys from collections import Counter def input(): return sys.stdin.readline()[:-1] t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() c = [Counter() for _ in range((k + 1)//2)] for i in range(n): c[min(i%k, k-1 - (i%k))][s[i]] += 1 ans = 0 for d in c: ans += sum(d.values()) - max(d.values()) print(ans) ```
instruction
0
5,919
0
11,838
Yes
output
1
5,919
0
11,839
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` for _ in range(int(input())): n,k = map(int,input().split()) s = list(str(input())) l1 = [0]*n i = 0 c = 0 if n == 2: if s[0] == s[1]: print(0) else: print(1) continue while i<=n//2: l2 = [] for j in range(i,n,k): if l1[j] == 0: l2.append(s[j]) l1[j] = 1 if l1[n-j-1] == 0: l2.append(s[n-j-1]) l1[n-j-1] = 1 dict = {} count, itm = 0, '' for item in reversed(l2): dict[item] = dict.get(item, 0) + 1 if dict[item] >= count : count, itm = dict[item], item aa = count c+=(len(l2)-count) for j in range(i,n): if l1[j] == 1: i+=1 if i>n//2: break else: break print(c) ```
instruction
0
5,920
0
11,840
Yes
output
1
5,920
0
11,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` import sys input = sys.stdin.readline from collections import Counter P=[2,3,5,7,11,13,17,19,23,29,31] t=int(input()) for tests in range(t): n,k=map(int,input().split()) S=input().strip() A=[Counter() for i in range(k)] for i in range(n): A[min(i%k,(n-1-i)%k)][S[i]]+=1 #print(A) ANS=0 for a in A: if len(a)>1: ANS+=sum(a.values())-max(a.values()) print(ANS) ```
instruction
0
5,921
0
11,842
Yes
output
1
5,921
0
11,843
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` from collections import defaultdict for _ in range(int(input())): N,K=map(int,input().split()) S=list(input()) Hash=defaultdict(lambda:0) for i in range(N): Hash[S[i]]+=1 count=0 for i in range(K): if S[i]!=S[K-1-i]: if Hash[S[i]]>Hash[S[K-1-i]]: S[K-1-i]=S[i] count+=1 else: S[i]=S[K-1-i] count+=1 for i in range(N-K-1): if S[i+K]!=S[i]: S[i+K]=S[i] count+=1 print(count) ```
instruction
0
5,922
0
11,844
No
output
1
5,922
0
11,845
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` #GaandFatiPadiHaiIsliyeEasyQuestionKrnePadRheHai #Contest668NeMaaChodDi import math def dfs(node): vis[node]=1 temp[0]+=1 lis[ord(s[node])-97]+=1 for j in edge[node]: if vis[j]==0: dfs(j) t=int(input()) for _ in range(t): n,k=list(map(int,input().split())) s=str(input()) if n==1: print(0) else: vis=[0]*n edge=[[] for i in range(n)] for i in range(k,n): edge[i%k].append(i) p=math.ceil(n//2) if n%2==0: q=p-1 else: q=p-2 for i in range(p,n): if q%k!=i%k: edge[q].append(i) edge[i].append(q) q-=1 ans=0 for i in range(n): if vis[i]==0: temp=[0] lis=[0]*26 dfs(i) a=max(lis) ans+=temp[0]-a print(ans) ```
instruction
0
5,923
0
11,846
No
output
1
5,923
0
11,847
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` import sys def read(): return list(map(int,sys.stdin.readline().split())) for _ in range(int(input())): n, k = read() s = sys.stdin.readline() count = 0 for i in range(1,n-k+1): a = {s[i-1],s[k+i-1],s[n-i]} count += len(a)-1 for i in range(n-k+1,n): if s[i-1] != s[n-i]: count += 1 print(count) ```
instruction
0
5,924
0
11,848
No
output
1
5,924
0
11,849
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Word s of length n is called k-complete if * s is a palindrome, i.e. s_i=s_{n+1-i} for all 1 ≀ i ≀ n; * s has a period of k, i.e. s_i=s_{k+i} for all 1 ≀ i ≀ n-k. For example, "abaaba" is a 3-complete word, while "abccba" is not. Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word. To do this Bob can choose some i (1 ≀ i ≀ n) and replace the letter at position i with some other lowercase Latin letter. So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word. Note that Bob can do zero changes if the word s is already k-complete. You are required to answer t test cases independently. Input The first line contains a single integer t (1 ≀ t≀ 10^5) β€” the number of test cases. The first line of each test case contains two integers n and k (1 ≀ k < n ≀ 2 β‹… 10^5, n is divisible by k). The second line of each test case contains a word s of length n. It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2 β‹… 10^5. Output For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word. Example Input 4 6 2 abaaba 6 3 abaaba 36 9 hippopotomonstrosesquippedaliophobia 21 7 wudixiaoxingxingheclp Output 2 0 23 16 Note In the first test case, one optimal solution is aaaaaa. In the second test case, the given word itself is k-complete. Submitted Solution: ``` t = int(input()) for _ in range(t): count=0 n,k = map(int, input().split()) l=input() s=list(l) for i in range(k): d={} j=0 while(i+(j*k)<n): if(s[i+(j*k)] in d): d[s[i+(j*k)]]+=1 else: d[s[i+(j*k)]]=1 z=k-i-1 if(s[z+(j*k)] in d): d[s[z+(j*k)]]+=1 else: d[s[z+(j*k)]]=1 j+=1 p=max(d, key=d.get) j=0 while(i+(j*k)<n): if(s[i+(j*k)]!=p): s[i+(j*k)]=p count+=1 j+=1 #print(max(d, key=d.get),d,i) m=''.join(s) #print(count) co=0 for i in range(n): if(s[i]!=l[i]): co+=1 print(co) ```
instruction
0
5,925
0
11,850
No
output
1
5,925
0
11,851
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,926
0
11,852
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` t=int(input()) for i in range(t): n=list(map(int,input().split())) ans="" if n[1]==0: if n[0]==0: print("1"*(n[2]+1)) else: print("0"*(n[0]+1)) continue cnt=0 for i in range(n[1]+1): if cnt%2==0: ans+="1" else: ans+="0" cnt+=1 ans="1"*n[2]+ans[0]+"0"*n[0]+ans[1:] print(ans) ```
output
1
5,926
0
11,853
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,927
0
11,854
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` for _ in range(int(input())): n0, n1, n2 = map(int, input().split(' ')) if n0 > 0 and n2 > 0: n1 -= 1 if n0 != 0: print(0, end='') for i in range(n0): print(0, end='') if n2 != 0: print(1, end='') for i in range(n2): print(1, end='') start = 1 if n2 != 0: start = 0 if n0 == 0 and n2 == 0: print(0, end='') for i in range(n1): print(start, end='') start = (start + 1) % 2 print() ```
output
1
5,927
0
11,855
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,928
0
11,856
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): n0,n1,n2=list(map(int,input().split())) ans='' if n0>0: ans='0'+'0'*(n0) if n1>0: if ans=='': ans+='0' if n1%2==0: ans='1'+ans ans+='1' ans+='01'*(n1//2-1) else: ans+='1' ans+='01'*((n1+1)//2-1) if ans=='': ans+='1' ans+='1'*(n2) yield ans if __name__ == '__main__': t= int(input()) ans = gift() print(*ans,sep='\n') ##100 55 ##1 3 5 ##33 22 ##x+3*(k-x)=n ##n=3*k-2*x ##x=(3*k-n)//2 ##1 3 1 3 1 3 49 ```
output
1
5,928
0
11,857
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,929
0
11,858
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` for i in range(int(input())): a, b, c = map(int, input().split()) s = '' s1 = '1' * c s0 = '0' * a if b > 0: s += '1' for j in range(b): if j % 2 == 0: s += '0' else: s += '1' s = s[0] + s0 + s[1::] s = s1 + s else: s = s1 + s0 if a != 0: s += '0' else: s += '1' print(s) ```
output
1
5,929
0
11,859
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,930
0
11,860
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` tim = {'00': 0, '01': 1, '10': 1, '11': 2} def binary_find(): def dfs(s,i,j,k): if (i > n0) or (j > n1) or (k > n2): return False if (i == n0) and (j == n1) and (k == n2): print(s) return True for new in range(2): temp = s[-1] + str(new) if temp == '00': found = dfs(s + str(new),i+1,j,k) elif temp in ['01','10']: found = dfs(s + str(new),i,j+1,k) else: found = dfs(s + str(new),i,j,k+1) if found : return True n0, n1, n2 = [int(x) for x in input().split()] if not dfs('0',0,0,0): dfs('1',0,0,0) t = int(input()) for i in range(t): binary_find() ```
output
1
5,930
0
11,861
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,931
0
11,862
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): for _ in range(II()): n0,n1,n2=MI() if n1==1: ans="0"*(n0+1)+"1"*(n2+1) elif n1 == n2 == 0: ans = "0" * (n0 + 1) elif n0==n2==0: ans="0" for i in range(n1): ans+="0" if i%2 else "1" elif n0 == n1 == 0: ans = "1" * (n2 + 1) elif n0 == 0: ans = "1" * (n2 + 1) for i in range(n1): ans += "1" if i % 2 else "0" elif n2==0: ans="0"*(n0+1) for i in range(n1): ans+="0" if i%2 else "1" else: ans="0"*(n0+1)+"1"*(n2+1) for i in range(n1-1): ans += "1" if i % 2 else "0" print(ans) main() ```
output
1
5,931
0
11,863
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,932
0
11,864
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` t=int(input()) for you in range(t): l=input().split() n0=int(l[0]) n1=int(l[1]) n2=int(l[2]) if(n0==0 and n1==0): for i in range(n2+1): print(1,end="") print() elif(n1==0 and n2==0): for i in range(n0+1): print(0,end="") print() elif(n1%2==0): for i in range(n1//2): print("01",end="") for i in range(n2): print("1",end="") for i in range(n0+1): print("0",end="") print() else: for i in range(n2): print("1",end="") for i in range((n1+1)//2): print("10",end="") for i in range(n0): print("0",end="") print() ```
output
1
5,932
0
11,865
Provide tags and a correct Python 3 solution for this coding contest problem. For some binary string s (i.e. each character s_i is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of '1' (ones) in it was calculated. You are given three numbers: * n_0 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 0; * n_1 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 1; * n_2 β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals 2. For example, for the string s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n_0=1, n_1=3, n_2=5. Your task is to restore any suitable binary string s from the given values n_0, n_1, n_2. It is guaranteed that at least one of the numbers n_0, n_1, n_2 is greater than 0. Also, it is guaranteed that a solution exists. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers n_0, n_1, n_2 (0 ≀ n_0, n_1, n_2 ≀ 100; n_0 + n_1 + n_2 > 0). It is guaranteed that the answer for given n_0, n_1, n_2 exists. Output Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them. Example Input 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 Output 1110011110 0011 0110001100101011 10 0000111 1111 000
instruction
0
5,933
0
11,866
Tags: constructive algorithms, dfs and similar, math Correct Solution: ``` # https://codeforces.com/problemset/problem/1352/F def checkEven(n): if n % 2 == 0: return True else: return False def main(): n = int(input()) arr = [] for _ in range(n): temp = list(map(int, input().split())) arr.append(temp) for i in range(n): reconstruct(arr[i][0], arr[i][1], arr[i][2]) def reconstruct(n0, n1, n2): res = "" if n1 == 0: if n0 != 0: res += "0" * (n0 + 1) elif n2 != 0: res += "1" * (n2 + 1) else: if checkEven(n1): res += "10" * (n1 // 2) res += "1" else: res += "10" * ((n1 + 1) // 2) if n0 != 0: res = "1" + "0" * n0 + res[1:] if n2 != 0: res = "1" * n2 + res print(res) if __name__ == "__main__": main() ```
output
1
5,933
0
11,867