message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb
instruction
0
60,437
0
120,874
"Correct Solution: ``` n,k=map(int,input().split()) s=input() u=s+s[::-1] u=min((u[i:i+n])for i in range(n)) for i in range(n): if u[i]!=u[0]:break c=i for _ in range(k-1): c*=2 if c>n:break print((u[0]*c+u[i:])[:n]) ```
output
1
60,437
0
120,875
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb
instruction
0
60,438
0
120,876
"Correct Solution: ``` N, K = map(int, input().split()) S = input() SS = S + S[::-1] D = [] for i in range(N): D.append(SS[i:i+N]) D.sort() B = D[0] for i in range(N): if B[i] != B[0]: break if i * 1 << (K - 1) > N: print(B[0] * N) else: print((B[0] * (i * (1 << (K - 1))) + B[i:])[:N]) ```
output
1
60,438
0
120,877
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` n, k = map(int, input().split()) s = input() if k > 5000 * 26: k = 5000 * 26 if k != 1: for i in range(k - 1): sList = [] t = (s[::-1]) u = s + t for j in range(0, n+1): sList.append((u[j:(j + n)])[::-1]) sList.sort() if s == sList[0][::-1]: break s = sList[0][::-1] for i in range(1): sList = [] t = (s[::-1]) u = s + t for j in range(0, n+1): sList.append(u[j:(j + n)]) sList.sort() s = sList[0] print(s) ```
instruction
0
60,439
0
120,878
Yes
output
1
60,439
0
120,879
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` n,k=map(int,input().split()) s=input() t=s[::-1] u="".join([s,t]) ls=[] for i in range(n): ls.append(u[i:i+n]) ls.sort() k-=1 x=ls[0] cnt=1 for i in range(1,n): if x[0]==x[i]: cnt+=1 else: break if cnt*2**k<n: y=n-cnt*2**k print(x[0]*(cnt*2**k)+x[cnt:y+cnt]) else: print(x[0]*n) ```
instruction
0
60,440
0
120,880
Yes
output
1
60,440
0
120,881
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` def solve(n, k, s): u = s + s[::-1] t = min(u[i:i + n] for i in range(n + 1)) i, h = 0, t[0] for i, c in enumerate(t): if c != h: break j = i for _ in range(k - 1): j <<= 1 if j >= n: return h * n return h * j + t[i:i + n - j] n, k = map(int, input().split()) s = input() print(solve(n, k, s)) ```
instruction
0
60,441
0
120,882
Yes
output
1
60,441
0
120,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` import sys N,K=map(int,input().split()) S=list(input()) MIN=min(S) def minimum(S): T=S+S[::-1] ANS=[] for i in range(N+1): ANS.append(T[i:i+N]) return min(ANS) def next(S): T=S+S[::-1] LIST=[] CL=[0]*2*N count=0 for i in range(2*N): if T[i]==MIN: count+=1 else: count=0 CL[i]=count MAX=max(CL) LIST=[] for i in range(N-1,2*N): if CL[i]==MAX: LIST.append(T[i+1-N:i+1]) return LIST,MAX def next2(S,x): return [MIN]*x+S[x:][::-1] if K==1: print("".join(minimum(S))) else: CAN,MAX=next(S) if (MAX<<(K-1))>=N: print(MIN*N) sys.exit() ANS=[] for c in CAN: ANS.append(next2(c,MAX*((1<<(K-1))-1))) print("".join(min(ANS))) ```
instruction
0
60,442
0
120,884
Yes
output
1
60,442
0
120,885
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` n, k = (int(i) for i in input().split()) s1 = input() ```
instruction
0
60,443
0
120,886
No
output
1
60,443
0
120,887
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` f=lambda s:min(s[i:i+n]for i in range(n+1)) n,k=map(int,input().split()) s=input() if k<2:print(f(s+s[::-1])) elif k>n:print(min(s)*n) else: u=s+s[::-1] s,u=min((u[i],u[i+n-1:i+k-1:-1])for i in range(n)) u=s*k+u[::-1] print(f(u[::-1]+u)) ```
instruction
0
60,444
0
120,888
No
output
1
60,444
0
120,889
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` def E(S, N): T = S[::-1] U = S+T return [U[i:N+i] for i in range(N)] N, K = input().split() N, K = int(N), int(K) S = [input()] for i in range(K): memory = [] for s in S: memory += E(s, N) S = sorted(memory)[:N//2] print(S[0]) ```
instruction
0
60,445
0
120,890
No
output
1
60,445
0
120,891
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, and replace S with S'. Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one. Constraints * 1 \leq N \leq 5000 * 1 \leq K \leq 10^9 * |S|=N * S consists of lowercase English letters. Input Input is given from Standard Input in the following format: N K S Output Print the lexicographically smallest possible string that can be the string S after the K operations. Examples Input 5 1 bacba Output aabca Input 10 2 bbaabbbaab Output aaaabbaabb Submitted Solution: ``` N,K = (int(i) for i in input().split()) S = input() a = [S] res = [] for i in range(K): res = [] for k in a: Ss = k[::-1] SSs = k + Ss a = [] for j in range(len(S)): a.append(SSs[j:len(S)+j]) res.append(SSs[j:len(S)+j]) print(min(res)) ```
instruction
0
60,446
0
120,892
No
output
1
60,446
0
120,893
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,495
0
120,990
"Correct Solution: ``` from collections import Counter s = input() counter = Counter(s) a, b, c = [counter[e] for e in "abc"] if max(a, b, c) - min(a, b, c) <= 1: ans = "YES" else: ans = "NO" print(ans) ```
output
1
60,495
0
120,991
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,496
0
120,992
"Correct Solution: ``` import collections import math S = list(input()) count = dict(collections.Counter(S)) m = list(count.values()) if len(m) == 1 and len(S) == 1: # 文字1種類のとき print("YES") elif len(m) == 1 and len(S) > 1: print("NO") elif len(m) == 2 and len(S) == 2: print("YES") elif len(m) == 2 and len(S) > 2: print("NO") elif (max(m) - min(m)) <= 1: print("YES") else: print("NO") ```
output
1
60,496
0
120,993
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,497
0
120,994
"Correct Solution: ``` S = input() a = S.count('a') b = S.count('b') c = S.count('c') if max(a, b, c) - min(a, b, c) > 1: print('NO') else: print('YES') ```
output
1
60,497
0
120,995
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,498
0
120,996
"Correct Solution: ``` from collections import Counter def solve(s): freq = sorted(Counter(s).values()) if sum(freq) == 1: return True if sum(freq) == 2: return freq[-1] == 1 return len(freq) == 3 and freq[2] - freq[0] <= 1 print('YES' if solve(input()) else 'NO') ```
output
1
60,498
0
120,997
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,499
0
120,998
"Correct Solution: ``` def main(): S = input() a = S.count("a") b = S.count("b") c = S.count("c") diff = max((a, b, c)) - min((a, b, c)) if diff <= 1: print("YES") else: print("NO") if __name__ == "__main__": main() ```
output
1
60,499
0
120,999
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,500
0
121,000
"Correct Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- from collections import Counter def main(): S = input() cnt = Counter() for c in "abc": cnt[c] = 0 cnt.update(S) v = sorted(cnt.values()) ok = v[2] - v[0] <= 1 print("YES" if ok else "NO") if __name__ == "__main__": main() ```
output
1
60,500
0
121,001
Provide a correct Python 3 solution for this coding contest problem. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES
instruction
0
60,502
0
121,004
"Correct Solution: ``` S = input().strip() C = {"a":0,"b":0,"c":0} for i in range(len(S)): C[S[i]] += 1 cmin = min(C["a"],C["b"],C["c"]) for x in C: C[x] -= cmin cmax = max(C["a"],C["b"],C["c"]) if cmax<=1: print("YES") else: print("NO") ```
output
1
60,502
0
121,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES Submitted Solution: ``` from collections import Counter c = Counter(input()) if abs(c['a'] - c['b']) > 1\ or abs(c['b'] - c['c']) > 1 \ or abs(c['c'] - c['a']) > 1: print('NO') else: print('YES') ```
instruction
0
60,503
0
121,006
Yes
output
1
60,503
0
121,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES Submitted Solution: ``` #!/usr/bin/env python3 S = input() n = [S.count('a'), S.count('b'), S.count('c')] ans = False if len(S) == 1: ans = True elif len(S) == 2 and len([x for x in n if x != 0]) == 2: ans = True else: n.sort() if n[0] != 0 and n[2] - n[0] <= 1: ans = True print(('NO', 'YES')[ans]) ```
instruction
0
60,505
0
121,010
Yes
output
1
60,505
0
121,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES Submitted Solution: ``` import io import sys _INPUT="""\ aaabbbbcccaba """ sys.stdin=io.StringIO(_INPUT) import math s=input() def kaibun(s): ls=len(s) if ls==1: print("YES") return ca,cb,cc=0,0,0 l=[] for i in range(ls): if s[i]=="a": ca+=1 if s[i]=="b": cb+=1 if s[i]=="c": cc+=1 l.append(ca) l.append(cb) l.append(cc) cnt_zero=0 for j in l: if j==0: cnt_zero+=1 if cnt_zero==2: print("NO") return elif cnt_zero==1: if ls==2: print("YES") return else: print("NO") return #cnt_zero=0のとき,つまりすべての文字が文字列に含まれるとき else: if ls==3 or ls==4: print("YES") return nmin=math.floor(ls/3.0) nmax=math.ceil(ls/3.0) nmax=int(nmax) nmin=int(nmin) out=0 for j in l: if j<nmin: print("NO") return if nmax<j: out+=1 if out>=2: print("NO") return print("YES") return kaibun(s) ```
instruction
0
60,507
0
121,014
No
output
1
60,507
0
121,015
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES Submitted Solution: ``` import math s=input() def kaibun(s): ls=len(s) if ls==1: print("YES") return ca,cb,cc=0,0,0 l=[] for i in range(ls): if s[i]=="a": ca+=1 if s[i]=="b": cb+=1 if s[i]=="c": cc+=1 l.append(ca) l.append(cb) l.append(cc) cnt_zero=0 for j in l: if j==0: cnt_zero+=1 if cnt_zero==2: print("NO") return elif cnt_zero==1: if ls==2: print("YES") return else: print("NO") return #cnt_zero=0のとき,つまりすべての文字が文字列に含まれるとき else: if ls==3 or ls==4: print("YES") return nmin=math.floor(ls/3.0) nmax=math.ceil(ls/3.0) nmax=int(nmax) nmin=int(nmin) out=0 for j in l: if j<nmin or nmax<j: print("NO") return if j==nmax: out+=1 if out>=2: print("NO") return print("YES") return kaibun(s) ```
instruction
0
60,508
0
121,016
No
output
1
60,508
0
121,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a string S consisting of three kinds of letters: `a`, `b` and `c`. He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible. Constraints * 1 \leq |S| \leq 10^5 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If the objective is achievable, print `YES`; if it is unachievable, print `NO`. Examples Input abac Output YES Input aba Output NO Input babacccabab Output YES Submitted Solution: ``` s = input() a = b = c = 0 for i in range(len(s)): if(s[i] == "a"): a += 1 elif(s[i] == "b"): b += 1 else: c += 1 if((a + b + c > 1) and ((a > b + c) or (b > a + c) or (c > a + b))): print("NO") elif(abs(a - b) < 2 or abs(b - c) < 2 or abs(c - a) < 2): print("NO") else: print("YES") ```
instruction
0
60,509
0
121,018
No
output
1
60,509
0
121,019
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,075
0
122,150
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` import math import time from collections import defaultdict,deque from sys import stdin,stdout from bisect import bisect_left,bisect_right s=stdin.readline().strip() n=len(s) m=int(stdin.readline()) arr=[0]*(n+1) a=list(map(int,stdin.readline().split())) for i in range(m): arr[a[i]-1]+=1 arr[n-a[i]+1]-=1 curr=0 ans=[""]*n for i in range(n): curr+=arr[i] if(curr%2==0): ans[i]=s[i] else: ans[i]=s[n-i-1] print(*ans,sep="") ```
output
1
61,075
0
122,151
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,076
0
122,152
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` s = [""]+list(input()) n = int(input()) n = len(s) l = n * [0] for q in map(int, input().split()): l[q]+=1 for i in range(1,n//2+1): l[i]+=l[i-1] if l[i] & 1: s[i], s[n-i] = s[n-i], s[i] print("".join(s)) ```
output
1
61,076
0
122,153
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,078
0
122,156
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` string = input() string = list(string) Slen = len(string) m = int(input()) inline = input().split() a = [] for i in range (m): a.append(int(inline[i])) a.append(Slen // 2 + 1) a.sort() changes = [0 for i in range(Slen)] flag = 0 for i in range(len(a) - 1): if flag == 0: for j in range(a[i], a[i + 1]): string[j - 1], string[Slen - j - 1 + 1] = string[Slen - j - 1 + 1], string[j - 1] flag = 1 - flag for i in range(Slen): print (string[i], sep = '', end = '') ```
output
1
61,078
0
122,157
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,079
0
122,158
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` s = input() n = len(s) ans = ["a"]*(n) if len(s)%2 == 1: x = n//2 else: x = (n//2)-1 p = [0]*(x+1) m = int(input()) l = list(map(int,input().split())) for i in l: p[i-1] += 1 for i in range(len(p)): if p[i]%2 == 0: p[i] = 0 else: p[i] = 1 x = 0 for i in range(len(p)): v = p[i] r = v^x x = r if r%2 == 1: ans[i],ans[n-i-1] = s[n-i-1],s[i] else: ans[i], ans[n - i - 1] = s[i],s[n - i - 1] print("".join(ans)) ```
output
1
61,079
0
122,159
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,080
0
122,160
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` # print ("Input the string") st = input() ln = len(st) switches = [0 for i in range(ln)] answer = [0 for i in range(ln)] # print ("Input number of reversals") m = int(input()) # print ("Input all the switch values") arr = input().split() arr = [int(i) for i in arr] for val in arr: index = val-1 switches[index] = 1 - switches[index] for i in range(ln // 2): if switches[i] == 1: # Make the reversal AND THE CARRYOVER answer[i] = st[ln-i-1] answer[ln-i-1] = st[i] switches[i+1] = 1 - switches[i+1] else: answer[i] = st[i] answer[ln-i-1] = st[ln-i-1] if (ln % 2 == 1): # Add the final element in the middle if odd answer[ln // 2] = st[ln // 2] print (''.join(answer)) ```
output
1
61,080
0
122,161
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea
instruction
0
61,082
0
122,164
Tags: constructive algorithms, greedy, math, strings Correct Solution: ``` st = list(input()) n = int(input()) s = [0] * len(st) for x in input().split(): s[int(x)-1] += 1 for i in range(1 , len(st)): s[i] += s[i-1] for i in range(len(st)//2): if s[i] & 1 : st[i] , st[len(st)-i-1] = st[len(st)-i-1] , st[i] print(''.join(st)) ```
output
1
61,082
0
122,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` s = input() s = list(s) n=input() l=list(map(int,input().split())) l.sort() x = [0]*len(s) toggle = 1 for i in range(len(l)): x[l[i]-1]+=1 for i in range(1,len(x)//2): x[i] += x[i-1] for i in range(len(x)//2): if(x[i] %2== 1): s[i],s[len(s)-i-1]=s[len(s)-i-1],s[i] print(''.join(s)) ```
instruction
0
61,083
0
122,166
Yes
output
1
61,083
0
122,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` s = input() m = len(s) n = int(input()) arr = [0 for _ in range(m+1)] out = list(map(int, input().split())) for x in out: arr[x] = 1 + arr[x] if m-x+2<=m: arr[m-x+2] -= 1 for y in range(1, m+1): arr[y] = arr[y] + arr[y-1] out = list(s) i = 1 while i <= m//2: if arr[i]&1: out[i-1], out[m-i] = out[m-i], out[i-1] i+=1 print("".join(out)) ```
instruction
0
61,084
0
122,168
Yes
output
1
61,084
0
122,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` def main(): l = list(input()) input() le = len(l) ll = sorted(map(int, input().split())) ll.append(le // 2 + 1) le -= 1 for a, b in zip(ll[::2], ll[1::2]): for i in range(a - 1, b - 1): l[i], l[le - i] = l[le - i], l[i] print(''.join(l)) if __name__ == '__main__': main() ```
instruction
0
61,086
0
122,172
Yes
output
1
61,086
0
122,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` s=input() l=len(s) print(l) m=int(input()) print(m) a=input() al=a.split(" ") print(al) for i in range(0,m): for j in range(int(al[i]),(l//2)+1): s=s[0:j-1]+s[l-j]+s[j:l-j]+s[j-1]+s[l-j+1:] print(s) ```
instruction
0
61,087
0
122,174
No
output
1
61,087
0
122,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` s=input() n=input() asdf=input().split(' ') b=list(s) a1=asdf.count('1') if a1%2==0: b[0]=s[0] b[-1]=s[-1] else: b[0]=s[-1] b[-1]=s[0] mem=a1%2 for i in range(len(s)//2): if asdf.count(str(i+2))>0: mem+=asdf.count(str(i+2)) mem%=2 if mem==0: b[i+1],b[-i-2]=s[i+1],s[-i-2] else: b[i+1],b[-i-2]=s[-i-2],s[i+1] for letter in b: print(letter,end='') ```
instruction
0
61,088
0
122,176
No
output
1
61,088
0
122,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` s=input() n=int(input()) asdf=input().split(' ') imp=[] asdf.sort(reverse=True) x=asdf.index(asdf[-1])-1 if (n-x)%2==0: imp.append(int(asdf[x+1])-1) while x>=0: y=asdf.index(asdf[x])-1 if (x-y)%2==1: imp.append(int(asdf[x])-1) x=y b=[] for i in range(len(s)): b.append('') m=0 for i in range(len(s)//2): if i in imp: m+=1 m%=2 if m==0: b[i],b[-i-1]=s[i],s[-i-1] else: b[i],b[-i-1]=s[-i-1],s[i] for letter in b: print(letter,end='') ```
instruction
0
61,089
0
122,178
No
output
1
61,089
0
122,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. Input The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. Output In the first line of the output print what Pasha's string s will look like after m days. Examples Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea Submitted Solution: ``` #!/usr/bin/python3 s = input() m = int(input()) arr = [int(i) for i in input().split()] prefix = [0 for i in range(10 ** 5)] for i in arr: prefix[i - 1] += 1 for i in range(1, len(prefix)): prefix[i] += prefix[i - 1] counter = 0 i = 0 while (i + 1) * 2 <= len(s): counter ^= prefix[i] % 2 if counter: right = len(s) - i - 1 s = s[:i] + s[right] + s[i + 1: right] + s[i] + s[right + 1:] i += 1 print(s) ```
instruction
0
61,090
0
122,180
No
output
1
61,090
0
122,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden. You want to find a string a such that the value of |a|·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s is aaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index. Calculate the maximum possible value of |a|·f(a) you can get. Input The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s. The second line contains a string s, consisting of n lowercase Latin letters. The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden). Output Print the maximum possible value of |a|·f(a). Examples Input 5 ababa 00100 Output 5 Input 5 ababa 00000 Output 6 Input 5 ababa 11111 Output 0 Submitted Solution: ``` n=int(input()) ch=input() t=input() maximum=0 if (n/2)!=n//2: mid=((n//2)+1)*((n//2)+1) elif (n/2)==n//2: mid=((n//2)+1)*(n//2) for i in range(1,n): j=0 oc=[] while maximum<mid and j<n: if j not in oc and j+i-1<=n-1 and t[j+i-1]=="0" : a=ch[j:j+i] rep=0 k=0 while k<n: if k+i-1<=n-1 and t[k+i-1]=="0" and k not in oc and ch[k:k+i]==a: rep=rep+1 oc.append(k) k=k+1 if rep*len(a)>maximum: maximum=rep*len(a) if maximum>=mid: break j=j+1 print(maximum) ```
instruction
0
61,199
0
122,398
No
output
1
61,199
0
122,399
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden. You want to find a string a such that the value of |a|·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s is aaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index. Calculate the maximum possible value of |a|·f(a) you can get. Input The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s. The second line contains a string s, consisting of n lowercase Latin letters. The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden). Output Print the maximum possible value of |a|·f(a). Examples Input 5 ababa 00100 Output 5 Input 5 ababa 00000 Output 6 Input 5 ababa 11111 Output 0 Submitted Solution: ``` n=int(input()) ch=input() t=input() maximum=0 if t[n-1]=="0": maximum=n for i in range(1,n): oc=[] j=0 while maximum< ((n//2)+1)*((n//2)+1) and j<n: if j not in oc: if j+i-1<=n-1: if t[j+i-1]=="0": a=ch[j:j+i] print(a) rep=0 for k in range(n): if k+i-1<=n-1: if t[k+i-1]=="0": if ch[k:k+i]==a: rep=rep+1 oc.append(k) if rep*len(a)>maximum: maximum=rep*len(a) j=j+1 print(maximum) ```
instruction
0
61,200
0
122,400
No
output
1
61,200
0
122,401
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden. You want to find a string a such that the value of |a|·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s is aaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index. Calculate the maximum possible value of |a|·f(a) you can get. Input The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s. The second line contains a string s, consisting of n lowercase Latin letters. The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden). Output Print the maximum possible value of |a|·f(a). Examples Input 5 ababa 00100 Output 5 Input 5 ababa 00000 Output 6 Input 5 ababa 11111 Output 0 Submitted Solution: ``` n=int(input()) ch=input() t=input() maximum=0 if t[n-1]=="0": maximum=n if (n/2)!=n//2: mid=((n//2)+1)*((n//2)+1) elif (n/2)==n//2: mid=((n//2)+1)*(n//2) for i in range(1,n): oc=[] j=0 while maximum<mid and j<n: if j not in oc: if j+i-1<=n-1: if t[j+i-1]=="0": a=ch[j:j+i] print(a) rep=0 k=0 while k<n: if k+i-1<=n-1: if t[k+i-1]=="0": if ch[k:k+i]==a: rep=rep+1 oc.append(k) k=k+1 if rep*len(a)>maximum: maximum=rep*len(a) print('maximum; ',maximum) if maximum>=mid: break j=j+1 print(maximum) ```
instruction
0
61,201
0
122,402
No
output
1
61,201
0
122,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden. You want to find a string a such that the value of |a|·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s is aaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index. Calculate the maximum possible value of |a|·f(a) you can get. Input The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s. The second line contains a string s, consisting of n lowercase Latin letters. The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden). Output Print the maximum possible value of |a|·f(a). Examples Input 5 ababa 00100 Output 5 Input 5 ababa 00000 Output 6 Input 5 ababa 11111 Output 0 Submitted Solution: ``` n=int(input()) ch=input() t=input() maximum=0 if t[n-1]=="0": maximum=n if (n/2)!=n//2: mid=((n//2)+1)*((n//2)+1) elif (n/2)==n//2: mid=((n//2)+1)*(n//2) print('mid: ',mid) for i in range(1,n): j=0 oc=[] while maximum<mid and j<n: if j not in oc: if j+i-1<=n-1: if t[j+i-1]=="0": a=ch[j:j+i] print(a) rep=0 k=0 while k<n: if k+i-1<=n-1: if t[k+i-1]=="0": if k not in oc: if ch[k:k+i]==a: rep=rep+1 oc.append(k) print(oc) k=k+1 if rep*len(a)>maximum: maximum=rep*len(a) print('maximum; ',maximum) if maximum>=mid: break j=j+1 print(maximum) ```
instruction
0
61,202
0
122,404
No
output
1
61,202
0
122,405
Provide tags and a correct Python 3 solution for this coding contest problem. Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: * Extract the first character of s and append t with this character. * Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. Input First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. Output Print resulting string u. Examples Input cab Output abc Input acdb Output abdc
instruction
0
61,954
0
123,908
Tags: data structures, greedy, strings Correct Solution: ``` f = lambda q: ord(q) - 97 s, t, u = input(), [], '' k = [0] * 26 for i in s: k[f(i)] += 1 for i in s: t.append(i) k[f(i)] -= 1 while t and sum(k[:f(t[-1])]) == 0: u += t.pop() print(u) ```
output
1
61,954
0
123,909
Provide tags and a correct Python 3 solution for this coding contest problem. Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: * Extract the first character of s and append t with this character. * Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. Input First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. Output Print resulting string u. Examples Input cab Output abc Input acdb Output abdc
instruction
0
61,957
0
123,914
Tags: data structures, greedy, strings Correct Solution: ``` from queue import deque dp = {} def sol_1(): idx = 0 while True: min_idx = get_min_char_idx(s, idx) if min_idx == -1: break if len(t) > 0 and ord(t[-1]) <= ord(s[min_idx]): # we need to take t u.append(t.pop()) else: # take up to min_idx t.extend(s[idx:min_idx+1]) idx = min_idx+1 def efficient_sol(): global u, t, s import string indices = {char: [] for char in string.ascii_lowercase} # will hold indices for each char # fill indices for idx, char in enumerate(s): indices[char].append(idx) curr_idx = 0 for char in string.ascii_lowercase: if curr_idx == len(s): break if len(t) > 0 and ord(char) >= ord(t[-1]): # We've started searching for bigger characters, so we need to empty the smaller ones first while len(t) > 0 and ord(char) >= ord(t[-1]): u.append(t.pop()) for idx in sorted(indices[char]): if curr_idx == len(s): return min_idx = idx if min_idx < curr_idx: # we've passed this character continue elif min_idx == curr_idx: if len(t) > 0 and ord(char) > ord(t[-1]): raise Exception() # we are at that character, so just add it u.append(char) curr_idx += 1 continue # mid_idx is bigger, so we put everything up until this character in T # then, add the character himself t.extend(s[curr_idx:min_idx]) u.append(char) curr_idx = min_idx + 1 while curr_idx < len(s): pass def get_min_char_idx(s: str, start_idx: int): global dp if start_idx >= len(s): return -1 if start_idx in dp: return dp[start_idx] min_char = s[start_idx] min_idx = start_idx while start_idx < len(s): if ord(s[start_idx]) < ord(min_char): min_char = s[start_idx] min_idx = start_idx start_idx += 1 dp[start_idx] = min_idx return min_idx # aaaczbgjs import string s = input() # s = 'abcadc' # s = string.ascii_lowercase + string.ascii_lowercase u = [] t = [] # if len(s) >= 10**3: efficient_sol() # else: # sol_1() # abaaabababacba # print(t) print(''.join(u + list(reversed(t)))) ```
output
1
61,957
0
123,915
Provide tags and a correct Python 3 solution for this coding contest problem. Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: * Extract the first character of s and append t with this character. * Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. Input First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. Output Print resulting string u. Examples Input cab Output abc Input acdb Output abdc
instruction
0
61,958
0
123,916
Tags: data structures, greedy, strings Correct Solution: ``` s=input() t=[] u=[] n=len(s) a=[0]*26 for i in range(len(s)): z=ord(s[i])-97 a[z]+=1 FirstLetterInS=0 LengthT=0 while len(u)!=n: if len(t)==0: a[ord(s[FirstLetterInS])-97]-=1 z=s[FirstLetterInS] t.append(z) FirstLetterInS+=1 else: flag=False x = -1 for i in range(ord(t[-1])-97): if a[i]>0: flag=True break if flag: z=s[FirstLetterInS] t.append(z) FirstLetterInS+=1 a[ord(z) - 97] -= 1 else: u+=t[-1] t.pop() print("".join(u)) ```
output
1
61,958
0
123,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: * Extract the first character of s and append t with this character. * Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal. You should write a program that will help Petya win the game. Input First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. Output Print resulting string u. Examples Input cab Output abc Input acdb Output abdc Submitted Solution: ``` s = list(input()) ss = sorted(s, reverse=True) s.reverse() t = [] u = [] while s or t: if not t: t.append(s.pop()) elif not s: u.append(t.pop()) else: #if min(s) < t[-1]: if s[-1] < t[-1]: t.append(s.pop()) ss.pop() else: u.append(t.pop()) #print(s, t, u) print("".join(u)) ```
instruction
0
61,968
0
123,936
No
output
1
61,968
0
123,937
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,990
0
123,980
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` from sys import exit n = int(input()) letters_prev = [None for i in range(30)] letters_fow = [None for i in range(30)] okk = [False for i in range(30)] for i in range(n): task = [ord(i) - ord('a') for i in input()] if len(task) == 1: okk[task[0]] = True for i in range(1, len(task)): if (letters_prev[task[i]] is not None and letters_prev[task[i]] != task[i-1]) or task[i] == task[i-1]: print('NO') exit() else: letters_prev[task[i]] = task[i-1] for i in range(len(task)-1): if (letters_fow[task[i]] is not None and letters_fow[task[i]] != task[i+1]) or task[i] == task[i+1]: print('NO') exit() else: letters_fow[task[i]] = task[i+1] # print(task, letters_prev, letters_fow) def chain_p(x, was=[]): global letters_fow, letters_prev if x is None: return [] if letters_prev[x] is None: return [x] else: if letters_prev[x] in was: print('NO') exit() ans = chain_p(letters_prev[x], was=was+[letters_prev[x]]) + [x] # letters_prev[x] = None return ans def chain_f(x, was=[]): global letters_fow, letters_prev # print('_f', x, letters_fow[x]) if x is None: return [] if letters_fow[x] is None: # letters_fow[x] = None return [x] else: if letters_fow[x] in was: print('NO') exit() ans = chain_f(letters_fow[x], was=was+[letters_fow[x]]) + [x] # letters_fow[x] = None return ans done = [] cc = [] while True: flag = False for i in range(30): if i in done: continue prev = [] post = [] if letters_prev[i] is not None: flag = True prev = chain_p(letters_prev[i]) if letters_fow[i] is not None: flag = True post = chain_f(letters_fow[i]) done.extend(prev) done.extend(post) done.append(i) if len(prev) + len(post) == 0 and okk[i]: cc.append(chr(i+ord('a'))) elif len(prev) + len(post) > 0: cc.append("".join([chr(i+ord('a')) for i in prev] + [chr(i+ord('a'))] + [chr(i+ord('a')) for i in post][::-1])) if not flag: break cc.sort() print("".join(cc)) ```
output
1
61,990
0
123,981
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,991
0
123,982
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` n=int(input()) m=[] sc=[] for i in range(n): m.append(input()) sc.append(set(m[i])) if len(sc[i])!=len(m[i]): print('NO') break else: i=0 pX=False while i<len(m): j=i+1 p=False while j<len(m): #print(m) z=len(sc[i].intersection(sc[j])) #a=len(sc[i]) #b=len(sc[j]) if m[i] in m[j]: m[i]=m[j] sc[i]=sc[j] sc.pop(j) m.pop(j) p=True break elif m[j] in m[i]: sc.pop(j) m.pop(j) j-=1 elif z>0: if m[i][-z:]==m[j][:z]: m[i]+=m[j][z:] elif m[j][-z:]==m[i][:z]: m[i]=m[j]+m[i][z:] else: pX=True break sc[i]=set(m[i]) m.pop(j) sc.pop(j) j-=1 p=True j+=1 if not p: i+=1 if pX: print('NO') break if not pX: print(''.join(sorted(m))) ```
output
1
61,991
0
123,983
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,992
0
123,984
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` def generate_good_string(subs : list): In, Out, S = {}, {}, set() for s in subs: if len(s) == 1: S.add(s) for fr, to in zip(s, s[1:]): if fr != In.get(to, fr) or to != Out.get(fr, to): return(print('NO')) Out[fr], In[to] = to, fr Outset, Inset = set(Out), set(In) S -= (set.union(Outset, Inset)) for s in Outset - Inset: while Out.get(s[-1]): s += Out.pop(s[-1]) S.add(s) print('NO' if Out else ''.join(sorted(S))) substrings = [input() for _ in range(int(input()))] generate_good_string(substrings) ```
output
1
61,992
0
123,985
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,993
0
123,986
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` u, v, d = {}, {}, set() for i in range(int(input())): t = input() if len(t) == 1: d.add(t) for a, b in zip(t, t[1:]): if u.get(a, b) != b or v.get(b, a) != a: exit(print('NO')) u[a], v[b] = b, a d = d - set(u) - set(v) for q in set(u).difference(v): while q[-1] in u: q += u.pop(q[-1]) d.add(q) if u: exit(print('NO')) print(''.join(sorted(d))) ```
output
1
61,993
0
123,987
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,994
0
123,988
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` StringsNumber = int(input()) FinalStrings = [] Strings = [] for i in range(StringsNumber): Strings.append(input()) LetterGraph = {} # Генерим граф for i in range(len(Strings)): if len(Strings[i]) == 1: if Strings[i] not in LetterGraph: LetterGraph[Strings[i]] = "" #print("заапедил", i) continue for e in range(len(Strings[i]) - 1): if Strings[i][e] not in LetterGraph: Elements = [] for j in list(LetterGraph): if j != Strings[i][e + 1]: Elements.append(LetterGraph[j]) if Strings[i][e + 1] in Elements: print("NO") exit(0) LetterGraph[Strings[i][e]] = Strings[i][e + 1] continue if LetterGraph[Strings[i][e]] == Strings[i][e + 1] or LetterGraph[Strings[i][e]] == "": LetterGraph[Strings[i][e]] = Strings[i][e + 1] continue #print("Граф:", LetterGraph) print("NO") exit(0) #print("Я сгенерил граф, получилось:", LetterGraph) # Проверяем, что нету цикла if LetterGraph: Cycle = False for i in LetterGraph: Letter = LetterGraph[i] while True: if Letter in LetterGraph: if LetterGraph[Letter] == i: print("NO") exit(0) Letter = LetterGraph[Letter] else: break # Находим возможные первые символы if LetterGraph: IsIFirstSymbol = False FirstSymbols = [] for i in LetterGraph: IsIFirstSymbol = True for e in LetterGraph: if LetterGraph[e] == i: #print(i, "не подходит, потому что", e, "указывает на него.") IsIFirstSymbol = False if IsIFirstSymbol: FirstSymbols.append(i) if not FirstSymbols: print("NO") exit(0) #print("Варианты первого символа:", *FirstSymbols) # Создаем варианты финальной строки if LetterGraph: Letter = "" for i in FirstSymbols: FinalString = i Letter = i for e in range(len(LetterGraph)): if Letter in LetterGraph: if not (LetterGraph[Letter] == ""): FinalString += LetterGraph[Letter] #print(Letter, "есть в графе, так что добавляем", LetterGraph[Letter], ", на которое оно указывает.") Letter = LetterGraph[Letter] else: break else: break FinalStrings.append(FinalString) #print("Отдельные строки", *FinalStrings) FinalStrings.sort() RESULT = "" for i in FinalStrings: RESULT += i print(RESULT) ```
output
1
61,994
0
123,989
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,995
0
123,990
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` #https://codeforces.com/problemset/problem/886/D def is_all_used(used): for val in used.values(): if val != True: return False return True def is_circle(d, pre): used = {x:False for x in d} pre_none = [x for x in used if x not in pre] s_arr = [] for x in pre_none: cur = [] flg = dfs(x, d, used, cur) if flg==True: return True, None s_arr.append(cur) if is_all_used(used) != True: return True, None return False, s_arr def dfs(u, d, used, cur): used[u] = True cur.append(u) flg = False for v in d[u]: if used[v] == True: return True flg = dfs(v, d, used, cur) if flg==True: return flg return flg def push(d, u, v=None): if u not in d: d[u] = set() if v is not None: if v not in d: d[v] = set() d[u].add(v) def push_p(d, v): if v not in d: d[v] = 0 d[v]+=1 def is_deg_valid(d): for u in d: if len(d[u]) > 1: return True return False def solve(): n = int(input()) d = {} pre = {} for _ in range(n): s = input() if len(s) == 1: push(d, s) else: for u, v in zip(s[:-1], s[1:]): push(d, u, v) push_p(pre, v) flg, arr = is_circle(d, pre) if is_deg_valid(d) or flg==True: return 'NO' S = [''.join(x) for x in arr] S = sorted(S) return ''.join([s for s in S]) print(solve()) #4 #mail #ai #lru #cf #3 #kek #preceq #cheburek ```
output
1
61,995
0
123,991
Provide tags and a correct Python 3 solution for this coding contest problem. A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes). A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string. The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap. String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ. Input The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set. Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct. The total length of the strings doesn't exceed 105. Output Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. Examples Input 4 mail ai lru cf Output cfmailru Input 3 kek preceq cheburek Output NO Note One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
instruction
0
61,996
0
123,992
Tags: constructive algorithms, graphs, implementation Correct Solution: ``` n = int(input()) u = [] s = [] ok = True for i in range(n): u.append(input()) s.append(set(u[i])) if len(s[i]) != len(u[i]): print('NO') ok = False break if ok: i = 0 ok = False while i < len(u): j = i + 1 p = False while j < len(u): z = len(s[i].intersection(s[j])) if u[i] in u[j]: u[i] = u[j] s[i] = s[j] s.pop(j) u.pop(j) p = True break elif u[j] in u[i]: s.pop(j) u.pop(j) j -= 1 elif z > 0: if u[i][-z:] == u[j][:z]: u[i] += u[j][z:] elif u[j][-z:] == u[i][:z]: u[i] = u[j] + u[i][z:] else: ok = True break s[i] = set(u[i]) u.pop(j) s.pop(j) j -= 1 p = True j += 1 if not p: i += 1 if ok: print('NO') break if not ok: u.sort() print(''.join(u)) ```
output
1
61,996
0
123,993