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. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,800
0
29,600
"Correct Solution: ``` from bisect import bisect_right s = input() t = input() n = len(s) mp = [[] for _ in range(26)] s += s for i in range(len(s)): mp[ord(s[i]) - ord('a')].append(i) ans = 0 now = -1 for i in range(len(t)): a = ord(t[i]) - ord('a') nxt = bisect_right(mp[a], now) if len(mp[a]) == nxt: print(-1) exit() ans += mp[a][nxt] - now now = mp[a][nxt] if now >= n: now -= n print(ans) ```
output
1
14,800
0
29,601
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,801
0
29,602
"Correct Solution: ``` import string import bisect S = input() T = input() n = len(S) sa = {_: [] for _ in string.ascii_lowercase} for i, s in enumerate(S): sa[s] += [i] ans = 0 i = -1 for t in T: if sa[t]: j = bisect.bisect_left(sa[t], i + 1) if j == len(sa[t]): ni = sa[t][0] + n else: ni = sa[t][j] ans += ni - i i = ni % n else: print(-1) exit() print(ans) ```
output
1
14,801
0
29,603
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,802
0
29,604
"Correct Solution: ``` S = input() T = input() length = len(S) ans = 0 pre = -1 for t in T: x = S.find(t, pre+1, length) if x != -1: ans += x - pre pre = x else: x = S.find(t, 0, pre+1) if x != -1: ans += length + x - pre pre = x else: ans = -1 break print(ans) ```
output
1
14,802
0
29,605
Provide a correct Python 3 solution for this coding contest problem. Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. * Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s'). Constraints * 1 \leq |s| \leq 10^5 * 1 \leq |t| \leq 10^5 * s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print `-1`. Examples Input contest son Output 10 Input contest programming Output -1 Input contest sentence Output 33
instruction
0
14,803
0
29,606
"Correct Solution: ``` s = input() n = len(s) l = [[]for i in range(26)] for i, c in enumerate(s * 2): l[ord(c) - ord('a')].append(i) from bisect import * shukai = 0 now = 0 t = input() for c in t: x = ord(c) - ord('a') if len(l[x]) == 0: print(-1) exit() i = bisect_left(l[x], now) now = l[x][i] + 1 if now >= n: now -= n shukai += 1 print(shukai * n + now) ```
output
1
14,803
0
29,607
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,828
0
29,656
"Correct Solution: ``` import sys #input = sys.stdin.buffer.readline def main(): N = int(input()) S = input() Q = input() x = tuple(map(int,input().split())) for k in x: d,m,dm,dmc = 0,0,0,0 for i in range(N): st = S[i] if st == "D": d += 1 elif st == "M": m += 1 dm += d elif st == "C": dmc += dm if i >= k-1: if S[i-k+1] == "D": dm -= m d -= 1 elif S[i-k+1] == "M": m -= 1 print(dmc) if __name__ == "__main__": main() ```
output
1
14,828
0
29,657
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,829
0
29,658
"Correct Solution: ``` import sys input = sys.stdin.readline N = int(input()) S = list(input())[: -1] Q = int(input()) qs = list(map(int, input().split())) cs = [0] * (N + 1) c = [] d = [] for i in range(N): cs[i + 1] = cs[i] + (S[i] == "M") if S[i] == "C": c.append(i) if S[i] == "D": d.append(i) ccs = [0] * (N + 1) ccounts = [0] * (N + 1) for i in range(N): ccs[i + 1] = ccs[i] + cs[i] * (S[i] == "C") ccounts[i + 1] = ccounts[i] + (S[i] == "C") #print(cs, c) #print(ccs) for k in qs: res = 0 for i in d: l = i r = min(N, i + k) res += max(0, ccs[r] - ccs[l] - cs[i] * (ccounts[r] - ccounts[l])) print(res) ```
output
1
14,829
0
29,659
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,830
0
29,660
"Correct Solution: ``` n = int(input()) s = input() q = int(input()) K = list(map(int,input().split())) for ii in range(q): # D = [[0] * 3 for i in range(n)] d = 0 m = 0 dm = 0 ans = 0 k = K[ii] for i in range(n): # for j in range(3): # D[i][j] = D[i-1][j] if i-k >= 0: if s[i-k] == "D": d -= 1 dm -= m if s[i-k] == "M": m -= 1 if s[i] == "D": d += 1 if s[i] == "M": m += 1 dm += d if s[i] == "C": ans += dm print(ans) # print(D) ```
output
1
14,830
0
29,661
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,831
0
29,662
"Correct Solution: ``` def main(): n = int(input()) s = input() q = int(input()) k = list(map(int, input().split())) for ki in k: ans = 0 DMC = [0]*3 for i in range(n): if s[i] == "D": DMC[0] += 1 elif s[i] == "M": DMC[1] += 1 DMC[2] += DMC[0] if i >= ki: j = i-ki if s[j] == "D": DMC[0] -= 1 DMC[2] -= DMC[1] elif s[j] == "M": DMC[1] -= 1 if s[i] == "C": ans += DMC[2] print(ans) if __name__ == "__main__": main() ```
output
1
14,831
0
29,663
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,832
0
29,664
"Correct Solution: ``` import sys read = sys.stdin.buffer.read input = sys.stdin.readline input = sys.stdin.buffer.readline #sys.setrecursionlimit(10**9) #from functools import lru_cache def RD(): return sys.stdin.read() def II(): return int(input()) def MI(): return map(int,input().split()) def MF(): return map(float,input().split()) def LI(): return list(map(int,input().split())) def LF(): return list(map(float,input().split())) def TI(): return tuple(map(int,input().split())) # rstrip().decode('utf-8') def main(): n=II() s=input().rstrip().decode() li=[0]*n q=II() K=LI() for k in K: D=0 M=0 DM=0 ans=0 for i in range(n): if s[i]=="D": D+=1 elif s[i]=="M": M+=1 DM+=D elif s[i]=="C": ans+=DM if i-k+1>=0: if s[i-k+1]=="D": D-=1 DM-=M elif s[i-k+1]=="M": M-=1 print(ans) if __name__ == "__main__": main() ```
output
1
14,832
0
29,665
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,833
0
29,666
"Correct Solution: ``` n=int(input()) s=input() q=int(input()) k=list(map(int,input().split())) for x in k: d,m,dm,ans=0,0,0,0 for i in range(n): ss=s[i] if ss=='D': d+=1 elif ss=='M': m+=1 dm+=d elif ss=='C': ans+=dm if i>=x-1: ss=s[i-x+1] if ss=='D': dm-=m d-=1 elif ss=='M': m-=1 print(ans) ```
output
1
14,833
0
29,667
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,834
0
29,668
"Correct Solution: ``` N = int(input()) S = input() Q = int(input()) K = list(map(int, input().split())) for k in K: res = 0 d = 0 m = 0 dm = 0 for i in range(N): if S[i] == 'D': d += 1 elif S[i] == 'M': dm += d m += 1 elif S[i] == 'C': res += dm if i + 1 - k >= 0: if S[i + 1 - k] == 'D': d -= 1 dm -= m elif S[i + 1 - k] == 'M': m -= 1 print(res) ```
output
1
14,834
0
29,669
Provide a correct Python 3 solution for this coding contest problem. In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3), he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: * 0 \leq a < b < c \leq N - 1 * S[a] = `D` * S[b] = `M` * S[c] = `C` * c-a < k Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1). Constraints * 3 \leq N \leq 10^6 * S consists of uppercase English letters * 1 \leq Q \leq 75 * 3 \leq k_i \leq N * All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i-th line should contain the k_i-DMC number of the string S. Examples Input 18 DWANGOMEDIACLUSTER 1 18 Output 1 Input 18 DDDDDDMMMMMCCCCCCC 1 18 Output 210 Input 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Output 0 1 2 Input 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Output 10 52 110 140
instruction
0
14,835
0
29,670
"Correct Solution: ``` import sys input = sys.stdin.readline def main(): N = int(input()) S = input() Q = input() x = tuple(map(int,input().split())) for k in x: d,m,dm,dmc = 0,0,0,0 for i in range(N): st = S[i] if st == "D": d += 1 elif st == "M": m += 1 dm += d elif st == "C": dmc += dm if i >= k-1: if S[i-k+1] == "D": dm -= m d -= 1 elif S[i-k+1] == "M": m -= 1 print(dmc) if __name__ == "__main__": main() ```
output
1
14,835
0
29,671
Provide a correct Python 3 solution for this coding contest problem. () Problem Statement There is a string S. Initially, S is an empty string. Perform the following processing in order of n. * Add x_i p_i (=" (" or") ") to the end of S. After processing, determine if S is a well-balanced string. "The string is balanced" is defined as follows. * The empty string is well-balanced. * For balanced strings a and b, a + b (+ represents a concatenation of strings) is balanced. * For a balanced string a, "(" + a + ")" is balanced. Constraints * 1 ≀ n ≀ 1,000 * 1 ≀ x_i ≀ 10 ^ 6 * p_i is "(" or ")" Input Input follows the following format. All given numbers are integers. n p_1 x_1 .. .. p_n x_n Output Output "YES" if balanced, otherwise output "NO" on one line. Examples Input 3 ( 5 ) 4 ) 1 Output YES Input 5 ( 2 ) 2 ( 3 ) 1 ) 2 Output YES Input 2 ) 1 ( 1 Output NO
instruction
0
15,025
0
30,050
"Correct Solution: ``` n = int(input()) s = 0 f = 0 for i in range(n): p,x = input().split() x = int(x) if p == "(": s += x else: s -= x if s < 0: f = 1 if f or s != 0: print("NO") else: print("YES") ```
output
1
15,025
0
30,051
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,090
0
30,180
Tags: greedy, implementation, strings Correct Solution: ``` from sys import stdin input = stdin.readline def is_good(arr): digit = False upper = False lower = False for c in arr: if c.isdigit(): digit = True elif c.isupper(): upper = True else: lower = True return (digit and upper and lower) def solve(): s = list(input().rstrip()) # length 0 if is_good(s): print(*s, sep="") return # length 1 for i in range(len(s)): x = s[i] s[i] = "1" if is_good(s): print(*s, sep="") return s[i] = "a" if is_good(s): print(*s, sep="") return s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = x # length 2 for i in range(len(s)-1): x = s[i] y = s[i+1] s[i] = "1" s[i+1] = "a" if is_good(s): print(*s, sep="") return s[i] = "1" s[i+1] = "A" if is_good(s): print(*s, sep="") return s[i] = "a" s[i+1] = "A" if is_good(s): print(*s, sep="") return s[i] = x s[i+1] = y t = int(input()) for _ in range(t): solve() ```
output
1
15,090
0
30,181
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,091
0
30,182
Tags: greedy, implementation, strings Correct Solution: ``` n = int(input()) abc = 'qwertyuiopasdfghjklzxcvbnm' ABC = 'QWERTYUIOPASDFGHJKLZXCVBNM' nu = '0123456789' d = [] for i in range(n): coa = 0 coA = 0 co1 = 0 s = input() for j in range(len(s)): if s[j] in abc: coa += 1 continue elif s[j] in ABC: coA += 1 continue elif s[j] in nu: co1 += 1 d.append(([coa, coA, co1], s)) for i in range(n): x = d[i][0] s = d[i][1] for j in range(len(s)): if x[0] == 0: if x[1] - 1 >= 1: if s[j] in ABC: if j + 1 < len(s): s = s[:j] + 'a' + s[j + 1:] else: s = s[:j] + 'a' x[0] += 1 x[1] -= 1 elif x[2] - 1 >= 1: if s[j] in nu: if j + 1 < len(s): s = s[:j] + 'a' + s[j + 1:] else: s = s[:j] + 'a' x[2] -= 1 x[0] += 1 elif x[1] == 0: if x[0] - 1 >= 1: if s[j] in abc: if j + 1 < len(s): s = s[:j] + 'A' + s[j + 1:] else: s = s[:j] + 'A' x[0] -= 1 x[1] += 1 elif x[2] - 1 >= 1: if s[j] in nu: if j + 1 < len(s): s = s[:j] + 'A' + s[j + 1:] else: s = s[:j] + 'A' x[2] -= 1 x[1] += 1 elif x[2] == 0: if x[1] - 1 >= 1: if s[j] in ABC: if j + 1 < len(s): s = s[:j] + '1' + s[j + 1:] else: s = s[:j] + '1' x[1] -= 1 x[2] += 1 elif x[0] - 1 >= 1: if s[j] in abc: if j + 1 < len(s): s = s[:j] + '1' + s[j + 1:] else: s = s[:j] + '1' x[0] -= 1 x[2] += 1 print(s) ```
output
1
15,091
0
30,183
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,092
0
30,184
Tags: greedy, implementation, strings Correct Solution: ``` def check_pass(password): upper = False num = False lower = False for ch in password: if(ord(ch) >= 97 and ord(ch) <= 122): lower = True elif(ord(ch) >= 65 and ord(ch) <= 90): upper = True elif(int(ch) >= 0 and int(ch) <= 9): num = True return {'_acceptable': (upper & lower & num),'upper':upper, 'lower':lower, 'num':num} def change_char(password, index, conditions): new_password = list(password) if(not conditions['upper']): new_password[index] = 'A' elif(not conditions['lower']): new_password[index] = 'a' elif(not conditions['num']): new_password[index] = '4' new_password = "".join(new_password) obj = check_pass(new_password) if(not obj['_acceptable']): if(conditions['upper']): if(not obj['upper']): return password if(conditions['lower']): if(not obj['lower']): return password if(conditions['num']): if(not obj['num']): return password return new_password # Main Program n = int(input()) passwords = [] for i in range(n): passwords.append(input()) for password in passwords: if(not check_pass(password)['_acceptable']): index = 0 temp = password while(not check_pass(password)['_acceptable']): password = change_char(password, index, check_pass(password)) index += 1 print(password) ```
output
1
15,092
0
30,185
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,093
0
30,186
Tags: greedy, implementation, strings Correct Solution: ``` t = int(input()) for _ in range(t): s = str(input()) l = [] u = [] d = [] for i, c in enumerate(s): if c.islower(): l.append(i) elif c.isupper(): u.append(i) else: d.append(i) s = list(s) if l and u and d: pass elif not l and u and d: if len(u) > len(d): s[u[0]] = 'a' else: s[d[0]] = 'a' elif l and not u and d: if len(l) > len(d): s[l[0]] = 'A' else: s[d[0]] = 'A' elif l and u and not d: if len(l) > len(u): s[l[0]] = '1' else: s[u[0]] = '1' elif l and not u and not d: s[l[0]] = 'A' s[l[1]] = '1' elif not l and u and not d: s[u[0]] = 'a' s[u[1]] = '1' elif not l and not u and d: s[d[0]] = 'a' s[d[1]] = 'A' print(''.join(s)) ```
output
1
15,093
0
30,187
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,094
0
30,188
Tags: greedy, implementation, strings Correct Solution: ``` for _ in range(int(input())): C, s = {'0':0, 'A':0, 'a':0}, input() first = '0' if s[0].isdigit() else 'A' if s[0].isupper() else 'a' last = '0' if s[-1].isdigit() else 'A' if s[-1].isupper() else 'a' for ch in s: if ch.isdigit(): C['0'] += 1 elif ch.isupper(): C['A'] += 1 else: C['a'] += 1 zeros = sum(not C[x] for x in '0Aa') if zeros == 2: print(''.join(set('0Aa') - {first}) + s[2:]) elif zeros == 1: if C[first] > 1: print(''.join(x for x, c in C.items() if c == 0) + s[1:]) else: print(s[:-1] + ''.join(x for x, c in C.items() if c == 0)) else: print(s) ```
output
1
15,094
0
30,189
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,095
0
30,190
Tags: greedy, implementation, strings Correct Solution: ``` for _ in range(int(input())): s=input() counta=0 countA=0 count1=0 for j in range(len(s)): if ord('a')<=ord(s[j])<=ord('z'): counta+=1 elif ord('A')<=ord(s[j])<=ord('Z'): countA+=1 else: count1+=1 k=0;l=0 if count1!=0 and counta!=0 and countA!=0: print(s) elif count1+counta+countA==count1: s=s[:len(s)-1]+'a' s=s[:len(s)-2]+'A'+s[-1] print(s) elif count1+counta+countA==count1: s=s[:len(s)-1]+'a' s=s[:len(s)-2]+'A'+s[-1] print(s) elif count1+counta+countA==counta: s=s[:len(s)-1]+'1' s=s[:len(s)-2]+'A'+s[-1] print(s) elif count1+counta+countA==countA: s=s[:len(s)-1]+'1' s=s[:len(s)-2]+'a'+s[-1] print(s) elif count1==0: for i in range(len(s)): if ord('a')<=ord(s[i])<=ord('z'): k+=1 if ord('A')<=ord(s[i])<=ord('Z'): l+=1 if k>1 or l>1: s=s[:i]+'1'+s[i+1:] print(s) break elif counta==0: for i in range(len(s)): if ord('0')<=ord(s[i])<=ord('9'): k+=1 if ord('A')<=ord(s[i])<=ord('Z'): l+=1 if k>1 or l>1: s=s[:i]+'a'+s[i+1:] print(s) break elif countA==0: for i in range(len(s)): if ord('a')<=ord(s[i])<=ord('z'): k+=1 if ord('0')<=ord(s[i])<=ord('9'): l+=1 if k>1 or l>1: s=s[:i]+'A'+s[i+1:] print(s) break ```
output
1
15,095
0
30,191
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,096
0
30,192
Tags: greedy, implementation, strings Correct Solution: ``` import string (t,) = map(int, input().split()) def invalid(s): minus = len(list(filter(lambda x: x in string.ascii_lowercase, s))) mayus = len(list(filter(lambda x: x in string.ascii_uppercase, s))) digits = len(list(filter(lambda x: x in string.digits, s))) return any((minus == 0, mayus == 0, digits == 0)) def gen_str(s): minus = len(list(filter(lambda x: x in string.ascii_lowercase, s))) mayus = len(list(filter(lambda x: x in string.ascii_uppercase, s))) digits = len(list(filter(lambda x: x in string.digits, s))) result = '' if minus == 0: result += 'a' if mayus == 0: result += 'A' if digits == 0: result += '0' return result for _ in range(t): s = input() new_s = s if invalid(s): t = gen_str(s) new_s = t + s[len(t):] if invalid(new_s): new_s = s[:-len(t)] + t print(new_s) ```
output
1
15,096
0
30,193
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
instruction
0
15,097
0
30,194
Tags: greedy, implementation, strings Correct Solution: ``` n = int(input()) B = [{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} ,{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}] for i in range(n): s = input() A = [0] * 3 for i in range(len(s)): for j in range(3): if s[i] in B[j]: A[j] += 1 if A[0] != 0 and A[1] != 0 and A[2] != 0: print(s) else: for j1 in range(3): j2 = (j1 + 1) % 3 j3 = (j1 + 2) % 3 if A[j1] == 0 and A[j2] != 0 and A[j3] != 0: if (s[0] in B[j2] and A[j2] > 1) or (s[0] in B[j3] and A[j3] > 1): h = str(B[j1].pop()) B[j1].add(h) print(h + s[1:]) break else: h = str(B[j1].pop()) B[j1].add(h) print(s[0] + str(h) + s[2:]) break elif A[j1] == 0: h = str(B[j1].pop()) B[j1].add(h) if A[j2] == 0: h1 = str(B[j2].pop()) B[j2].add(h1) print(h + h1 + s[2:]) break else: h1 = str(B[j3].pop()) B[j3].add(h1) print(h + h1 + s[2:]) break ```
output
1
15,097
0
30,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` import string n = int(input()) def chr2id(c): if c in string.ascii_uppercase: return 1 if c in string.digits: return 2 return 0 repl = ['a', 'A', '1'] for _ in range(n): s = input() arr = [0] * len(s) chr_ids = [0] * len(s) last_n = [0] * 3 id_counts = [0] * 3 last_id = chr2id(s[0]) id_counts[last_id] = 1 chr_ids[0] = last_id for i, c in enumerate(s[1:]): c_id = chr2id(c) chr_ids[i+1] = c_id id_counts[c_id] += 1 if last_id == c_id: arr[i+1] = arr[i] + 1 if arr[i+1] < len(last_n): last_n[arr[i+1]] = i + 1 else: last_n.append(i+1) last_id = c_id change_count = sum(map(lambda x: x == 0, id_counts)) if change_count == 0: print(s) continue l = change_count while last_n[l] == 0 and l > 0: l -= 1 i = last_n[l] if l == 0: left = 0 right = 0 for i, c in enumerate(chr_ids): if chr_ids[left] == chr_ids[i] and id_counts[c] > change_count: right = i else: left = right = i if right - left >= change_count: break i = right i += 1 c = 0 s = list(s) for j in range(i - change_count, i): while(id_counts[c] > 0): c += 1 s[j] = repl[c] c += 1 print(''.join(s)) ```
instruction
0
15,098
0
30,196
Yes
output
1
15,098
0
30,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` from collections import defaultdict t = [] for i in range(int(input())): t.append(input()) v_const = { 'upper': 'A', 'lower': 'a', 'numbers': '1', } char_types = list(v_const.keys()) n_const = [str(i) for i in range(10)] for p in t: indexes = defaultdict(list) for i, v in enumerate(p): if v in n_const: indexes['numbers'].append(i) elif v.islower(): indexes['lower'].append(i) else: indexes['upper'].append(i) missing_arrays = {key: None for key in char_types if key not in indexes} keys = list(missing_arrays.keys()) if len(keys) == 0: print(p) elif len(keys) == 1: for key, val in indexes.items(): if len(val) > 1: missing_arrays[keys[0]] = val[0] break n_p = list(p) n_p[missing_arrays[keys[0]]] = v_const[keys[0]] print(''.join(n_p)) else: n_p = list(p) n_p[0] = v_const[keys[0]] n_p[1] = v_const[keys[1]] print(''.join(n_p)) ```
instruction
0
15,099
0
30,198
Yes
output
1
15,099
0
30,199
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` T = int(input()) hs = [ {chr(x) for x in range(ord('0'), ord('9')+1, 1)}, {chr(x) for x in range(ord('a'), ord('z')+1, 1)}, {chr(x) for x in range(ord('A'), ord('Z')+1, 1)} ] hch = ['1','a','A'] for t in range(T): s = input() c = [0] * 3 for i in range(len(s)): if s[i] in hs[0]: c[0] += 1 elif s[i] in hs[1]: c[1] += 1 else: c[2] += 1 if c.count(0) == 0: print(s) continue ls = list(s.strip()) if c.count(0) == 2: ip = 0 for hi in range(3): if c[hi] == 0: ls[ip] = hch[hi] ip += 1 print(*ls, sep='') continue # c.count(0) == 1: iipos = 0 while c[iipos] < 2: iipos += 1 ip = 0 while ls[ip] not in hs[iipos]: ip += 1 ls[ip] = hch[c.index(0)] print(*ls, sep='') continue ```
instruction
0
15,100
0
30,200
Yes
output
1
15,100
0
30,201
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` # -*- coding: utf-8 -*- # @Date : 2018-09-22 16:53:43 # @Author : raj lath (oorja.halt@gmail.com) # @Link : http://codeforces.com/contest/1051/problem/A # @Version : 1.0.0 from sys import stdin max_val=int(10e12) min_val=int(-10e12) def read_int() : return int(stdin.readline()) def read_ints() : return [int(x) for x in stdin.readline().split()] def read_str() : return input() def read_strs() : return [x for x in stdin.readline().split()] def get_id(a): c_id = 2 if "a" <= a <= "z": c_id = 0 if "A" <= a <= "Z": c_id = 1 return c_id nb_test = read_int() for _ in range(nb_test): replacement = ["a","A","1"] password = [x for x in input()] c_counts = [0] * 3 for i in password: c_counts[get_id(i)] += 1 for i,v in enumerate(password): ids = get_id(v) if c_counts[ids] > 1: for j in range(3): if not c_counts[j]: password[i] = replacement[j] c_counts[ids] -= 1 c_counts[j] += 1 break print(''.join(password)) ```
instruction
0
15,101
0
30,202
Yes
output
1
15,101
0
30,203
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` for _ in [0]*int(input()): s=list(input()) l=[-1]*3 for i,c in enumerate(s): l[(c>'9')+(c>'Z')]=i for i in range(len(s)): j = (s[i]>'0')+(s[i]>'Z') if l[j]<0and s[i]not in l:s[i]='0Aa'[j];l[j]=i print(''.join(s)) ```
instruction
0
15,102
0
30,204
No
output
1
15,102
0
30,205
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` def solve(s): tp = ['']*len(s) pat = {'a' , 'A' , '0'} for i , ch in enumerate(s): if ch.isupper(): tp[i] = 'A' elif ch.islower(): tp[i] = 'a' else: tp[i] = '0' st = set(tp) if len(st) == 3 : return s elif len(st) == 1: return ''.join(pat - st) + s[2 : ] else: #if tp[0] == tp[1]: return ''.join(pat - st) + s[1 : ] #else: #return s[:-1] + ''.join(pat - st) t = int(input()) for tt in range(t): s = input() print(solve(s)) ```
instruction
0
15,103
0
30,206
No
output
1
15,103
0
30,207
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` for _ in range(int(input())): s = list(input()) low, up, dig = 0, 0, 0 for c in s: if c.isupper(): up += 1 elif c.islower(): low += 1 else: dig += 1 if up > 0 and low > 0 and dig > 0: print("".join(s)) else: if len(s) == low: s[0] = "1" s[1] = "A" elif len(s) == up: s[0] = "1" s[1] = "a" elif len(s) == dig: s[0] = "a" s[1] = "A" elif low == 0: if s[0].isupper and up > 1: s[0] = "a" elif s[0].isdigit() and dig > 1: s[0] = "a" else: s[1] = "a" elif up == 0: if s[0].islower and low > 1: s[0] = "A" elif s[0].isdigit() and dig > 1: s[0] = "A" else: s[1] = "A" else: #dig == 0 if s[0].isupper and up > 1: s[0] = "1" elif s[0].islower() and lower > 1: s[0] = "1" else: s[1] = "1" print("".join(s)) ```
instruction
0
15,104
0
30,208
No
output
1
15,104
0
30,209
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya came up with a password to register for EatForces β€” a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string s is a string x = s_l s_{l + 1} ... s_{l + len - 1} (1 ≀ l ≀ |s|, 0 ≀ len ≀ |s| - l + 1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer T (1 ≀ T ≀ 100) β€” the number of testcases. Each of the next T lines contains the initial password s~(3 ≀ |s| ≀ 100), consisting of lowercase and uppercase Latin letters and digits. Only T = 1 is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" β†’ "a7cdEf" is 4, because the changed positions are 2 and 5, thus (5 - 2) + 1 = 4. It is guaranteed that such a password always exists. If there are several suitable passwords β€” output any of them. Example Input 2 abcDCE htQw27 Output abcD4E htQw27 Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). Submitted Solution: ``` from sys import stdin input = stdin.readline def is_good(arr): digit = False upper = False lower = False for c in arr: if c.isdigit(): digit = True elif c.isupper(): upper = True else: lower = True return (digit and upper and lower) def solve(): s = list(input().rstrip()) # length 0 if is_good(s): print(*s, sep="") return # length 1 for i in range(len(s)): x = s[i] s[i] = "1" if is_good(s): print(*s, sep="") return s[i] = "a" if is_good(s): print(*s, sep="") return s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = x # length 2 for i in range(len(s)-1): x = s[i] y = s[i+1] s[i] = "1" s[i] = "a" if is_good(s): print(*s, sep="") return s[i] = "1" s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = "a" s[i] = "A" if is_good(s): print(*s, sep="") return s[i] = x s[i] = y t = int(input()) for _ in range(t): solve() ```
instruction
0
15,105
0
30,210
No
output
1
15,105
0
30,211
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,157
0
30,314
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` from sys import stdin n, k = [int(i) for i in stdin.readline().strip().split()] s = stdin.readline().strip() class Subsequences: def __init__(self, sequence): self._seq = list(sequence) self._F = [len(sequence) * [0] for _ in range((len(sequence) + 1))] self._calc() def _calc(self): # iterative solution F[size][index] given number of distinct subsequences of given size # in slice [0: index + 1] of original sequence F = self._F size = 0 for index in range(len(s)): F[size][index] = 1 F[1][0] = 1 p = {s[0]: 0} for i in range(1, len(s)): for k in range(1, len(s) + 1): if k > i + 1: val = 0 else: val = F[k][i - 1] + F[k - 1][i - 1] if s[i] in p: val -= F[k - 1][p[s[i]] - 1] F[k][i] = val p[s[i]] = i def count(self, size, index=None): index = index or (len(self._seq) - 1) return self._F[size][index] # recursive solution with memoization previous_letter_index = {} found = {} for index, letter in enumerate(s): if letter in found: previous_letter_index[(letter, index)] = found[letter] found[letter] = index _subsequences = {} def subsequences(size, index): """Get number of distinct subsequences of given size in sequence[0:index + 1] slice""" if (size, index) not in _subsequences: if size == 0: res = 1 elif size > index + 1: res = 0 else: res = subsequences(size, index - 1) + subsequences(size - 1, index - 1) letter = s[index] if (letter, index) in previous_letter_index: res -= subsequences(size - 1, previous_letter_index[(letter, index)] - 1) _subsequences[(size, index)] = res return _subsequences[(size, index)] ss = Subsequences(s) total_cost = 0 for size in range(len(s), -1, -1): if k == 0: break step_cost = n - size sequence_count = subsequences(size, len(s) - 1) if sequence_count > k: sequence_count = k total_cost += step_cost * sequence_count k -= sequence_count if k == 0: print(total_cost) else: print(-1) ```
output
1
15,157
0
30,315
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,158
0
30,316
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` # your code goes here n,k=map(int,input().split()) s=input() c=0 q=[s] d=set() ls=0 while q: p=q.pop(0) if p not in d: ls+=1 c+=(n-len(p)) if ls==k: break d.add(p) for i in range(len(p)): temp=p[:i]+p[i+1:] if temp not in d: q.append(temp) if ls==k: print(c) else: print(-1) ```
output
1
15,158
0
30,317
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,159
0
30,318
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def c(chr): return ord(chr)-ord('a') def main(): n, k = RL() s = input() dp = [[0]*(n+1) for i in range(n+1)] rec = [-1]*26 pre = [-1]*n for i in range(n+1): dp[i][0] = 1 for i in range(n): now = c(s[i]) pre[i] = rec[now] rec[now] = i for i in range(1, n+1): for j in range(1, i+1): # if j>i: break dp[i][j] = dp[i-1][j] + dp[i-1][j-1] # if i == 6 and j == 1: print(dp[i-1][j], dp[i-1][j-1]) if pre[i-1]!=-1: # if i==5 and j==4: print(pre[i-1], '+++') # if i==6 and j==1: print(pre[i-1], dp[pre[i-1]][j], dp[i][j]) dp[i][j] -= dp[pre[i-1]][j-1] res = 0 for j in range(n, -1, -1): if k: num = min(k, dp[-1][j]) k-=num res += (n-j)*num # for i in dp: print(i) print(res if k==0 else -1) if __name__ == "__main__": main() ```
output
1
15,159
0
30,319
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,160
0
30,320
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import sys n, k = map(int, sys.stdin.readline().split(' ')) s = sys.stdin.readline().strip() lt = [s] count = 0 flag = False result = 0 while lt: cur_s = lt[0] lt.pop(0) result += len(s) - len(cur_s) count += 1 if count >= k: break for i in range(0, len(cur_s)): new_s = cur_s[0: i] + cur_s[i + 1: ] if new_s not in lt: lt.append(new_s) if count < k: sys.stdout.write(str(-1) + '\n') else: sys.stdout.write(str(result) + '\n') ```
output
1
15,160
0
30,321
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,161
0
30,322
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import sys input=sys.stdin.readline from collections import deque n,k=map(int,input().split()) s=list(input().rstrip()) vis=set() dq=deque(["".join(s)]) cnt=0 score=0 while dq: ss=dq.popleft() if ss not in vis: score+=n-len(ss) cnt+=1 vis.add(ss) else: continue if cnt>=k: break for i in range(len(ss)): nxt=ss[:i]+ss[i+1:] dq.append("".join(nxt)) if cnt<k: print(-1) else: print(score) ```
output
1
15,161
0
30,323
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,162
0
30,324
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` if __name__ == '__main__': n, k = map(int, input().split()) aa = list(input()) st = {"".join(aa)} arr = [aa] w = 0 c = 0 cst = 0 while len(arr) < k and w < len(arr): wrd = arr[w][:c] + arr[w][c + 1:] wrds = "".join(wrd) if wrds not in st: st.add(wrds) arr.append(wrd) cst += n - len(wrd) c += 1 if c >= len(arr[w]): c = 0 w += 1 if len(arr) < k: print(-1) else: print(cst) ```
output
1
15,162
0
30,325
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,163
0
30,326
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` n, k = map(int, input().split()) s = input() dp = [[0] * 102 for i in range(102)] dp1 = [[0] * 102 for i in range(30)] for i in range(0, n + 1): dp[i][0] = 1 for i in range(1, n + 1): nm = ord(s[i - 1]) - ord('a') for le in range(1, i + 1): dp[i][le] = dp[i - 1][le] + dp[i - 1][le - 1] dp[i][le] -= dp1[nm][le] for le in range(1, i + 1): dp1[nm][le] += (dp[i][le] - dp[i - 1][le]) ans = 0 for le in range(n, -1, -1): if k == 0: break x = min(dp[n][le], k) ans += (n - le) * x k -= x if k != 0: print(-1) else: print(ans) ```
output
1
15,163
0
30,327
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
instruction
0
15,164
0
30,328
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def main(): n, k = RL() s = input() q = deque() q.append(s) res = 0 rec = set() rec.add(s) while q: now = q.popleft() res+=n-len(now) if len(rec)>=k: continue for i in range(len(now)): nex = now[:i]+now[i+1:] if nex in rec: continue rec.add(nex) if len(rec)<=k: q.append(nex) print(res if len(rec)>=k else -1) if __name__ == "__main__": main() ```
output
1
15,164
0
30,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` R = lambda: map(int, input().split()) n,k = R() s = input() p = [s] ans = 0 d= set() while p: q = p.pop(0) if q not in d: k -= 1 ans += (n-len(q)) if k == 0: print(ans) quit() d.add(q) for i in range(len(q)): t = q[:i]+q[i+1:] if t not in p:p.append(t) print(-1) ```
instruction
0
15,165
0
30,330
Yes
output
1
15,165
0
30,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` n, k = map(int, input().split()) str = str(input()) q = [str] ans = 0 ma = {} while len(q) != 0 and k > 1 : s = q[0] q.remove(q[0]) for i in range(len(s)): ss = s s1 = ss[0:i] s2 = ss[i+1:] ss = s1 + s2 if ss not in ma.keys() : ma[ss] = 1 k -= 1 ans += n - len(ss) q.append(ss) if k == 1 : break if k == 1 : print(ans) else : print(-1) ```
instruction
0
15,166
0
30,332
Yes
output
1
15,166
0
30,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` import sys from itertools import combinations from functools import lru_cache from string import ascii_lowercase @lru_cache() def e(n, l): return sum(d(n, l, x) for x in letters) @lru_cache() def d(n, l, c): if n < l: return 0 if s[n - 1] != c: return d(n - 1, l, c) return e(n - 1, l - 1) if l > 1 else 1 def solve(s, k): n = len(s) cost = 0 for l in range(n, 0, -1): cnt = e(n, l) if k > cnt: k -= cnt cost += cnt * (n - l) else: cost += k * (n - l) k = 0 break if k > 1: return -1 if k == 1: cost += n k = 0 return cost n, k = map(int, input().split()) s = input() letters = set(s) print(solve(s, k)) ```
instruction
0
15,167
0
30,334
Yes
output
1
15,167
0
30,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` import sys import itertools inputs = sys.stdin.read().split() len_string = int(inputs[0]) desired_size = int(inputs[1]) string = inputs[2] size = 0 cost = 0 cur_set = set() cur_set.add(string) for i in range(len_string, -1, -1): cur_size = len(cur_set) if size+cur_size >= desired_size: cost += (desired_size-size)*(len_string-i) size = desired_size break cost += cur_size*(len_string-i) size += cur_size new_set = set() for substr in cur_set: for i in range(len(substr)): new_set.add(substr[:i]+substr[(i+1):]) cur_set = new_set if size >= desired_size: sys.stdout.write(str(cost)+"\n") else: sys.stdout.write("-1\n") ```
instruction
0
15,168
0
30,336
Yes
output
1
15,168
0
30,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` line1 = input().split(' ') n = int(line1[0]) k = int(line1[1]) s = list(input()) dp = [101*[0] for i in range(101)] last = 26*[-1] for i in range(n+1): dp[0][i] = 1 for l in range(1, n+1): dp[l][0] = 0 for c in range(26): last[c] = -1 for i in range(1, n+1): dp[l][i] = dp[l-1][i-1] + dp[l][i-1] if last[ord(s[i-1])-ord('a')] != -1: dp[l][i] -= dp[l][last[ord(s[i-1])-ord('a')]] last[ord(s[i-1])-ord('a')] = i i = 0 res = 0 while i <= n and k >= 0: c = min(k, dp[n-i][n]) k -= c res += c * i i += 1 if k > 0: print(-1) else: print(res) ```
instruction
0
15,169
0
30,338
No
output
1
15,169
0
30,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- # File : H.py # Author : JCHRYS <jchrys@me.com> # Date : 30.08.2019 # Last Modified Date: 30.08.2019 # Last Modified By : JCHRYS <jchrys@me.com> class const: size = 26; # size of lowercase alphabet n, k = map(int, input().split()); s = input(); maxpos = [[-1 for _ in range(const.size)] for _ in range(n)]; for i in range(n): for j in range(const.size): if i > 0: maxpos[i][j] = maxpos[i - 1][j] maxpos[i][ord(s[i]) - ord('a')] = i #print(*[row for row in maxpos], sep="\n") dp = [[0 for _ in range(n)] for _ in range(n)]; for i in range(n): dp[i][1] = 1 for length in range(2, n): for endswith in range(1, n): for before in range(const.size): if maxpos[endswith - 1][before] != -1: dp[endswith][length] = dp[endswith][length] + dp[maxpos[endswith - 1][before]][length - 1]; #print(*[row for row in dp], sep="\n") k -= 1; ans = 0; for length in range(n-1, 0, -1): temp = 0; for i in range(const.size): if (maxpos[n-1][i] != -1): temp += dp[maxpos[n-1][i]][length]; if temp >= k: ans += k * (n - length); k = 0; break; else: k -= temp; ans += temp * (n-length); if (k == 1): ans += n; if k > 0: print(-1) exit() print(ans) ```
instruction
0
15,170
0
30,340
No
output
1
15,170
0
30,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` from collections import deque n,k = map(int, input().split()) s = input() count = 1 clen = len(s) result = 0 squeue = deque() squeue.append((s,0)) while count < k and clen > 0: base, spos = squeue.popleft() if len(base) == clen: sset = set() clen -= 1 for i in range(spos,len(base)): candidate = base[:i] + base[i+1:] if candidate.__hash__() not in sset: sset.add(candidate.__hash__()) squeue.append((candidate,i)) result += n-clen count+=1 if count == k: break if count == k: print(result) else: print(-1) ```
instruction
0
15,171
0
30,342
No
output
1
15,171
0
30,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string s consisting of n lowercase Latin letters. In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so. Input The first line of the input contains two integers n and k (1 ≀ n, k ≀ 100) β€” the length of the string and the size of the set, correspondingly. The second line of the input contains a string s consisting of n lowercase Latin letters. Output Print one integer β€” if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it. Examples Input 4 5 asdf Output 4 Input 5 6 aaaaa Output 15 Input 5 7 aaaaa Output -1 Input 10 100 ajihiushda Output 233 Note In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4. Submitted Solution: ``` def check_word(word_list, word_need): unique = [] for i in range(len(word_list)): # Pick each word in the list if word_need == 0: break for j in range(len(word_list[i])): if word_need == 0: break # Check word without 1 char is in unique new_word = word_list[i][:j]+word_list[i][j+1:] if new_word not in unique: unique.append(new_word) word_need -= 1 return unique def check_all(): cost_sum = 0 cost_each = 1 [raw_length, raw_size] = input().split() size = int(raw_size) words = input().split() size -= 1 while size>0: if len(words[0]) == 0: return -1 words = check_word(words, size) print(len(words)) cost_sum += len(words)*cost_each cost_each += 1 size -= len(words) return cost_sum print(check_all()) ```
instruction
0
15,172
0
30,344
No
output
1
15,172
0
30,345
Provide tags and a correct Python 3 solution for this coding contest problem. This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Input The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10. Output Print the lexicographically largest palindromic subsequence of string s. Examples Input radar Output rr Input bowwowwow Output wwwww Input codeforces Output s Input mississipp Output ssss Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
instruction
0
15,341
0
30,682
Tags: binary search, bitmasks, brute force, greedy, implementation, strings Correct Solution: ``` I = lambda: int(input()) IL = lambda: list(map(int, input().split())) s = list(input()) print(max(s) * s.count(max(s))) ```
output
1
15,341
0
30,683
Provide tags and a correct Python 3 solution for this coding contest problem. This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Input The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10. Output Print the lexicographically largest palindromic subsequence of string s. Examples Input radar Output rr Input bowwowwow Output wwwww Input codeforces Output s Input mississipp Output ssss Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
instruction
0
15,342
0
30,684
Tags: binary search, bitmasks, brute force, greedy, implementation, strings Correct Solution: ``` ## necessary imports import sys input = sys.stdin.readline from math import ceil, floor, factorial; # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return int(res) ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### s = input().strip(); mx = -1; for i in s: mx = max(mx, ord(i)); this = chr(mx); ans = this*(s.count(this)); print(ans); ```
output
1
15,342
0
30,685
Provide tags and a correct Python 3 solution for this coding contest problem. This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Input The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10. Output Print the lexicographically largest palindromic subsequence of string s. Examples Input radar Output rr Input bowwowwow Output wwwww Input codeforces Output s Input mississipp Output ssss Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
instruction
0
15,343
0
30,686
Tags: binary search, bitmasks, brute force, greedy, implementation, strings Correct Solution: ``` def Filter(a, n): j = 0 ans = [] while n > 0: last_bit = n & 1 if last_bit: ans.append(a[j]) j += 1 n = n >> 1 return ans def subs(a): ultimate = [] for i in range(1 << len(a)): ultimate.append(Filter(a, i)) return ultimate def size(ch): return sum([ord(i) for i in ch]) s = input() res = [] for i in subs(s): if len(set(i)) == 1: res.append(i) res.sort() print("".join(res[-1])) ```
output
1
15,343
0
30,687
Provide tags and a correct Python 3 solution for this coding contest problem. This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Input The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10. Output Print the lexicographically largest palindromic subsequence of string s. Examples Input radar Output rr Input bowwowwow Output wwwww Input codeforces Output s Input mississipp Output ssss Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
instruction
0
15,344
0
30,688
Tags: binary search, bitmasks, brute force, greedy, implementation, strings Correct Solution: ``` c = input() ; print(max(c) * c.count(max(c))); ```
output
1
15,344
0
30,689
Provide tags and a correct Python 3 solution for this coding contest problem. This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Input The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10. Output Print the lexicographically largest palindromic subsequence of string s. Examples Input radar Output rr Input bowwowwow Output wwwww Input codeforces Output s Input mississipp Output ssss Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
instruction
0
15,345
0
30,690
Tags: binary search, bitmasks, brute force, greedy, implementation, strings Correct Solution: ``` s = input() maxn = max(s) for i in s: if i == maxn: print(i,end="") print("") ```
output
1
15,345
0
30,691