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. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,299
0
24,598
"Correct Solution: ``` N = int(input()) S = input() A = S.count("R") * S.count("G") * S.count("B") for i in range(N): for j in range(i+1, N): k = j-i+j if k<N and S[k] != S[j] and S[k] != S[i] and S[i] !=S[j]: A -= 1 print(A) ```
output
1
12,299
0
24,599
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,300
0
24,600
"Correct Solution: ``` n=int(input()) s=input() r=s.count("R") g=s.count("G") b=s.count("B") ans=0 for i in range(1,(n-1)//2+1): for k in range(n-2*i): if s[k]!=s[k+i] and s[k+i]!=s[k+2*i] and s[k+2*i]!=s[k]: ans-=1 print(r*g*b+ans) ```
output
1
12,300
0
24,601
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,301
0
24,602
"Correct Solution: ``` n=int(input()) s=input() r=s.count("R") g=s.count("G") b=n-r-g ans=r*g*b for i in range(1,n): for j in range(n): if(j+i*2>=n):break d1,d2,d3=s[j],s[j+i],s[j+i*2] if(not(d1==d2 or d2==d3 or d1==d3)):ans-=1 print(ans) ```
output
1
12,301
0
24,603
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,302
0
24,604
"Correct Solution: ``` n=int(input()) s=input() ans=s.count("R")*s.count("G")*s.count("B") for j in range(n): J=s[j] for i in range(j): k=2*j-i if k<n: I=s[i] K=s[k] if I!=J!=K!=I: ans-=1 print(ans) ```
output
1
12,302
0
24,605
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,303
0
24,606
"Correct Solution: ``` n=int(input());s=input();c=s.count;print(c('R')*c('G')*c('B')-sum(len(set(s[i:j+1:(j-i)//2]))>2for i in range(n-2)for j in range(i+2,n,2))) ```
output
1
12,303
0
24,607
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,304
0
24,608
"Correct Solution: ``` N=int(input()) S=list(input()) ans=S.count('R')*S.count('G')*S.count('B') i=0 while i<N-1: j=i+1 while 2*j-i<N: ans-=1*(S[i]!=S[j])*(S[j]!=S[2*j-i])*(S[2*j-i]!=S[i]) j+=1 i+=1 print(ans) ```
output
1
12,304
0
24,609
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,305
0
24,610
"Correct Solution: ``` n=int(input());s=[*input()] count=s.count('R')*s.count('G')*s.count('B') for i in range(n+1): for j in range(i,n+1): k=2*j-i if k<n: if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]: count-=1 print(count) ```
output
1
12,305
0
24,611
Provide a correct Python 3 solution for this coding contest problem. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800
instruction
0
12,306
0
24,612
"Correct Solution: ``` n = int(input()) s = input() ans = s.count('R') * s.count('G') * s.count('B') for i in range(n - 2): for j in range(1, (n - i + 1) // 2): if s[i] != s[i+j] and s[i+j] != s[i+j*2] and s[i+j*2] != s[i]: ans -= 1 print(ans) ```
output
1
12,306
0
24,613
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of `R`, `G`, and `B`. Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Examples Input 4 RRGB Output 1 Input 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Output 1800 Submitted Solution: ``` s=str(input()) s=list(s) import numpy as np def γͺんか(a,b,c): ans=0 for i in a: tmp_b = b[b>i] for j in tmp_b: if j+j-i in c: ans+=len(c[(c>j)])-1 else: ans+=len(c[(c>j)]) tmp_c = c[c>i] for j in tmp_c: if j+j-i in b: ans+=len(b[(b>j)])-1 else: ans+=len(b[(b>j)]) return ans r1=[] b1=[] g1=[] for i, v in enumerate(s): if v=="R": r1+=[i] elif v=="B": b1+=[i] else: g1+=[i] r1 = np.array(r1) b1 = np.array(b1) g1 = np.array(g1) print(γͺんか(r1, g1, b1)+γͺんか(g1, b1, r1)+γͺんか(b1,r1,g1)) ```
instruction
0
12,313
0
24,626
No
output
1
12,313
0
24,627
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,537
0
25,074
Tags: implementation Correct Solution: ``` import sys def countR(ip): c=0 for i in ip: if(i=='R'): c+=1 return c def countB(ip): c=0 for i in ip: if(i=='B'): c+=1 return c def countG(ip): c=0 for i in ip: if(i=='G'): c+=1 return c # sys.stdin.readline() t=int(sys.stdin.readline()) x='RGB'*680 y='GBR'*680 z='BRG'*680 for i in range(t): n,k=list(map(int,sys.stdin.readline().strip().split())) a=sys.stdin.readline().strip() xk=x[:k] yk=y[:k] zk=z[:k] # print(k,xk,zk) # xc=[] # yc=[] # zc=[] # xc.append(countR(xk)) # xc.append(countG(xk)) # xc.append(countB(xk)) # yc.append(countR(yk)) # yc.append(countG(yk)) # yc.append(countB(yk)) # zc.append(countR(zk)) # zc.append(countG(zk)) # zc.append(countB(zk)) op=2001 for j in range(n-k+1): b=a[j:j+k] # print(len(b),xc,zc) # bc=[] # bc.append(countR(b)) # bc.append(countG(b)) # bc.append(countB(b)) xd=0 yd=0 zd=0 # print(a,b,xc,yc,zc,bc) for jj in range(len(b)): if(b[jj]!=xk[jj]): xd+=1 if(b[jj]!=yk[jj]): yd+=1 if(b[jj]!=zk[jj]): zd+=1 # print(a,b,xd,yd,zd,z) op=min(op,xd,yd,zd) print(op) ```
output
1
12,537
0
25,075
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,538
0
25,076
Tags: implementation Correct Solution: ``` import sys for _ in range(int(sys.stdin.readline())): n, k = [int(i) for i in sys.stdin.readline().split()] word = sys.stdin.readline().strip() ans = 0 for col in ["RGB", "GBR", "BRG"]: cnt = 0 for i in range(k): # print(word[i],"VS",col[i%3]) if word[i] == col[i%3]: cnt += 1 mx = cnt for i in range(n-k): if word[i+k] == col[(i+k)%3]: cnt += 1 if word[i] == col[i%3]: cnt -= 1 if cnt > mx: mx = cnt ans = max(ans, mx) sys.stdout.write(str(k - ans) + "\n") ```
output
1
12,538
0
25,077
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,539
0
25,078
Tags: implementation Correct Solution: ``` for _ in range(int(input())): n,k=map(int,input().split()) s=input() s1="RGB" mini=10**9 for i in range(n-k+1): for j in range(3): curr=0 for x in range(k): curr+=(s[i+x]!=s1[(x+j)%3]) mini=min(mini,curr) print(mini) ```
output
1
12,539
0
25,079
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,540
0
25,080
Tags: implementation Correct Solution: ``` import sys input = lambda: sys.stdin.readline().strip() from math import ceil def mismatch(s1, s2): cnt = 0 for i in range(len(s1)): if s1[i]!=s2[i]: cnt+=1 return cnt T = int(input()) for _ in range(T): n, k = map(int, input().split()) check = '' for i in range(ceil((k+2)/3)): check+='RGB' ls = [] for i in range(3): ls.append(check[i:i+k]) s = input() m = n for i in range(n-k+1): for j in ls: m = min(m, mismatch(s[i:i+k], j)) print(m) ```
output
1
12,540
0
25,081
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,541
0
25,082
Tags: implementation Correct Solution: ``` import sys t=sys.stdin.buffer.readline() for g in range(int(t)): n,k=map(int,input().split()) s=input() t = 2000 for i in range(n-k+1): s1 = 'RGB' '''if s[i]=="R": var=0 elif s[i]=="G": var=1 elif s[i]=='B': var=2''' for h in range(3): var=h count = 0 for j in range(k): if s[i + j] == s1[var]: var += 1 var = var % 3 else: count += 1 var += 1 var = var % 3 t=min(count,t) print(t) ```
output
1
12,541
0
25,083
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,542
0
25,084
Tags: implementation Correct Solution: ``` a = int(input()) ot = '' for i in range(a): b, c = map(int,input().split()) s = input() pref = [0] * (b + 1) pref1 = [0] * (b + 1) pref2 = [0] * (b + 1) n = 'RGB' n1 = 'GBR' n2 = 'BRG' for j in range(b): pref[j + 1] = pref[j] if s[j] != n[j % 3]: pref[j + 1] += 1 pref1[j + 1] = pref1[j] if s[j] != n1[j % 3]: pref1[j + 1] += 1 pref2[j + 1] = pref2[j] if s[j] != n2[j % 3]: pref2[j + 1] += 1 mi = 1000000000 #print(*pref) #print(*pref1) #print(*pref2) for j in range(c, b + 1): mi = min(pref[j] - pref[j - c], pref1[j] - pref1[j - c], pref2[j] - pref2[j - c], mi) #print(mi, j) mi = min(pref[-1], pref1[-1], pref2[-1], mi) ot += str(mi) + '\n' print(ot) ```
output
1
12,542
0
25,085
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,543
0
25,086
Tags: implementation Correct Solution: ``` for _ in range(int(input())): n, k = [int(i) for i in input().split()] word = input() ans = 0 for col in ["RGB", "GBR", "BRG"]: cnt = 0 for i in range(k): if word[i] == col[i%3]: cnt += 1 mx = cnt for i in range(n-k): if word[i+k] == col[(i+k)%3]: cnt += 1 if word[i] == col[i%3]: cnt -= 1 if cnt > mx: mx = cnt ans = max(ans, mx) print(k - ans) ```
output
1
12,543
0
25,087
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
instruction
0
12,544
0
25,088
Tags: implementation Correct Solution: ``` q = int(input()) for _ in range(q): n, k = list(map(int, input().split())) s = input() rgb = 0 gbr = 0 brg = 0 RGB = "RGB" for i in range(k): if i % 3 == 0: rgb += int(s[i] != 'R') gbr += int(s[i] != 'G') brg += int(s[i] != 'B') if i % 3 == 1: rgb += int(s[i] != 'G') gbr += int(s[i] != 'B') brg += int(s[i] != 'R') if i % 3 == 2: rgb += int(s[i] != 'B') gbr += int(s[i] != 'R') brg += int(s[i] != 'G') minimum = min(rgb, brg, gbr) for i in range(k, n): if (i - k) % 3 == 0: rgb -= int(s[i - k] != 'R') gbr -= int(s[i - k] != 'G') brg -= int(s[i - k] != 'B') if (i - k) % 3 == 1: rgb -= int(s[i - k] != 'G') gbr -= int(s[i - k] != 'B') brg -= int(s[i - k] != 'R') if (i - k) % 3 == 2: rgb -= int(s[i - k] != 'B') gbr -= int(s[i - k] != 'R') brg -= int(s[i - k] != 'G') if i % 3 == 0: rgb += int(s[i] != 'R') gbr += int(s[i] != 'G') brg += int(s[i] != 'B') if i % 3 == 1: rgb += int(s[i] != 'G') gbr += int(s[i] != 'B') brg += int(s[i] != 'R') if i % 3 == 2: rgb += int(s[i] != 'B') gbr += int(s[i] != 'R') brg += int(s[i] != 'G') minimum = min(minimum, rgb, brg, gbr) print(minimum) ```
output
1
12,544
0
25,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase 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") # ------------------- fast io -------------------- from collections import deque #just calculate it for the first string length k for the 3 different states and from then on its just DP def conv(let): if let=="R": return 0 elif let=="G": return 1 else: return 2 for j in range(int(input())): n,k=map(int,input().split()) vals=[conv(i) for i in input()] store=[[0,(k-1)%3],[1,k%3],[2,(k+1)%3]] cost=[0,0,0];mincost=10**9 for s in range(3): for i in range(k): if vals[i]!=(s+i)%3: cost[s]+=1 mincost=min(cost) for i in range(1,n+1-k): for s in range(3): if vals[i-1]!=store[s][0]: cost[s]-=1 store[s][0]+=1;store[s][1]+=1 store[s][0]=store[s][0]%3;store[s][1]=store[s][1]%3 if vals[i+k-1]!=store[s][1]: cost[s]+=1 mincost=min(mincost,min(cost)) print(mincost) ```
instruction
0
12,545
0
25,090
Yes
output
1
12,545
0
25,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` from sys import stdin input=stdin.readline R = lambda:map(int,input().split()) q, = R() a = 'RGB' an =[0]*3 for q1 in range(q): n,k = R() s = input() for i2 in range(3): ans = [0]*n for i in range(n): ans[i]=s[i]==a[(i+i2)%3] m = ans[0:k].count(0) m1 = m for i in range(k,n): m1 += -ans[i]+ans[i-k] m = min(m,m1) an[i2]=m print(min(an),flush=False) ```
instruction
0
12,546
0
25,092
Yes
output
1
12,546
0
25,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` for _ in range(int(input())): n,k=map(int,input().split()) a=input() s=["RGB"*(k//3)+"RGB"[:k%3],"GBR"*(k//3)+"GBR"[:k%3],"BRG"*(k//3)+"BRG"[:k%3]] minn=n for i in range(n-k+1): for j in s: total=0 for l in range(k): total+=1 if a[i+l]!=j[l] else 0 minn=min(total,minn) if minn==0: break if minn==0: break print(minn) ```
instruction
0
12,547
0
25,094
Yes
output
1
12,547
0
25,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` t="RGB"*100000 q=int(input()) for _ in range(q): ans=999999999999999999999999999999999999999999999999999999999999999999999999999 n,k=map(int,input().split()) s=input() for i in range(3): to=t[i:i+n+1] dp=[0 for i in range(n)] for j in range(n): if s[j] !=to[j]: dp[j]+=1 for j in range(1,n): dp[j] += dp[j-1] ii=0 jj=k-1 while jj<n: if ii==0: cnt=dp[jj] else: cnt=dp[jj]-dp[ii-1] ans=min(ans,cnt) jj += 1 ii += 1 print(ans) ```
instruction
0
12,548
0
25,096
Yes
output
1
12,548
0
25,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` for _ in range(int(input())): n,k=map(int, input().split()) s=input() if(k==1): print(0) elif(k==2): if('RG' in s or 'GB' in s or 'BR' in s): print(0) else: print(1) elif(k==3): if('RGB' in s or 'GBR' in s or 'BRG' in s): print(0) elif('RG' in s or 'GB' in s or 'BR' in s): print(1) else: print(2) else: s1='RGB'*(k//3) s2='GBR'*(k//3) s3='BRG'*(k//3) if(k%3==1): s1+='R' s2+='G' s3+='B' elif(k%3==2): s1+='RG' s2+='GB' s3+='BR' m=2005 for i in range(n): if(i+k>n): break st=s[i:i+k] c=0 if(st[0]=='R'): for j in range(k): if(st[j]!=s1[j]): c+=1 elif(st[0]=='G'): for j in range(k): if(st[j]!=s2[j]): c+=1 elif(st[0]=='B'): for j in range(k): if(st[j]!=s3[j]): c+=1 if(c<m): m=c print(m) ```
instruction
0
12,549
0
25,098
No
output
1
12,549
0
25,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` #575_D1 import sys q = int(input()) r = ["R", "G", "B"] for i in range(0, q): ns = [int(j) for j in input().split(" ")] n = ns[0] k = ns[1] st = list(sys.stdin.readline().rstrip()) mx = 10000000000 for j in range(0, len(st)): if len(st) - j < k: break rinc = r.index(st[j]) strinc = rinc c = 0 for l in range(j + 1, len(st)): rinc += 1 if rinc - strinc >= k: break if r[rinc % 3] != st[l]: c += 1 mx = min(mx, c) print(mx) ```
instruction
0
12,550
0
25,100
No
output
1
12,550
0
25,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` q=int(input()) while(q>0): l=list(map(int,input().split())) n=l[0] k=l[1] s=input() min=1000 for j in range(0,n-k): x=list(s) w=1 c=0 i=j while(w<k): if x[i]=="R": if x[i+1]=="G": w+=1 else: w+=1 x[i+1]="G" c+=1 elif x[i]=="G": if x[i+1]=="B": w+=1 else: w+=1 x[i+1]="B" c+=1 elif x[i]=="B": if x[i+1]=="R": w+=1 else: w+=1 x[i+1]="R" c+=1 i+=1 if min>c: min=c print(min) q-=1 ```
instruction
0
12,551
0
25,102
No
output
1
12,551
0
25,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...". A string a is a substring of string b if there exists a positive integer i such that a_1 = b_i, a_2 = b_{i + 1}, a_3 = b_{i + 2}, ..., a_{|a|} = b_{i + |a| - 1}. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer q independent queries. Input The first line of the input contains one integer q (1 ≀ q ≀ 2000) β€” the number of queries. Then q queries follow. The first line of the query contains two integers n and k (1 ≀ k ≀ n ≀ 2000) β€” the length of the string s and the length of the substring. The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'. It is guaranteed that the sum of n over all queries does not exceed 2000 (βˆ‘ n ≀ 2000). Output For each query print one integer β€” the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG". Submitted Solution: ``` import sys n = int(input()) I = lambda : map(int,sys.stdin.readline().split()) for _ in range (n) : n , m = I() minn = float("inf") st = input() a1 = (m//3+1)*("RGB") a1 = a1[:m] a2 = (m//3+1)*("BRG") a2 = a2[:m] a3 = (m//3+1)*("GBR") a3 = a3[:m] print(a2) for i in range (n-m+1) : c1 = 0 ; c2 = 0 ; c3 =0 ; for j in range (m) : if st[i+j] == a1[j] : pass else : c1+=1 if st[i+j] == a2[j] : pass else : c2+=1 if st[i+j] == a3[j] : pass else : c3+=1 minn = min(minn , c1) minn = min(minn , c2) minn = min(minn , c3) print(minn) ```
instruction
0
12,552
0
25,104
No
output
1
12,552
0
25,105
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,553
0
25,106
Tags: brute force, constructive algorithms Correct Solution: ``` import sys def main(): n = int(sys.stdin.readline().strip()) s1 = sys.stdin.readline().strip() s2 = sys.stdin.readline().strip() for c1 in 'abc': for c2 in 'abc': for c3 in 'abc': if len(set(c1 + c2 + c3)) != 3: continue ss = (c1 + c2 + c3) * n if s1 in ss or s2 in ss: continue print('YES') print(ss) return for c1 in 'abc': for c2 in 'abc': for c3 in 'abc': if len(set(c1 + c2 + c3)) != 3: continue ss = c1 * n + c2 * n + c3 * n if s1 in ss or s2 in ss: continue print('YES') print(ss) return print('NO') main() ```
output
1
12,553
0
25,107
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,554
0
25,108
Tags: brute force, constructive algorithms Correct Solution: ``` def mp(): return map(int, input().split()) n = int(input()) s = input() t = input() if s[1] == t[1] and s[0] != t[0] and s[1] != s[0] and t[1] != t[0]: print('YES') print(s[1] * n + s[0] * n + t[0] * n) elif s[0] == t[0] and s[1] != t[1] and s[0] != s[1] and t[0] != t[1]: print('YES') print(s[1] * n + t[1] * n + s[0] * n) else: for w in ['a', 'b', 'c']: ans = [w] cnt = [0, 0, 0] cnt[ord(w) - 97] += 1 for i in range(3 * n - 1): p = ans[-1] may = [1] * 3 if s[0] == p: may[ord(s[1]) - 97] = 0 if t[0] == p: may[ord(t[1]) - 97] = 0 jj = -1 for j in range(3): if may[j] and cnt[j] < n and (jj == -1 or cnt[j] <= cnt[jj]): jj = j if jj == -1: break ans.append(chr(jj + 97)) cnt[jj] += 1 if len(ans) == 3 * n: print('YES') print(''.join(ans)) break else: for w in ['a', 'b', 'c']: ans = [w] cnt = [0, 0, 0] cnt[ord(w) - 97] += 1 for i in range(3 * n - 1): p = ans[-1] may = [1] * 3 if s[0] == p: may[ord(s[1]) - 97] = 0 if t[0] == p: may[ord(t[1]) - 97] = 0 jj = -1 for j in range(3): if may[j] and cnt[j] < n and (jj == -1 or cnt[j] < cnt[jj]): jj = j if jj == -1: break ans.append(chr(jj + 97)) cnt[jj] += 1 if len(ans) == 3 * n: print('YES') print(''.join(ans)) break else: print('NO') ```
output
1
12,554
0
25,109
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,555
0
25,110
Tags: brute force, constructive algorithms Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) l = ["abc" * n, "acb" * n, "bac" * n, "bca" * n, "cba" * n, "cab" * n, 'b' * n + 'c' * n + 'a' * n, 'c' * n + 'a' * n + 'b' * n, 'a' * n + 'b' * n + 'c' * n] s1 = input().strip() s2 = input().strip() print("YES") for x in l: if s1 not in x and s2 not in x: print(x) break ```
output
1
12,555
0
25,111
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,556
0
25,112
Tags: brute force, constructive algorithms Correct Solution: ``` n = int(input()) s = input() t = input() a = [[0,0,0],[0,0,0],[0,0,0]] b = ['a','b','c'] a[b.index(s[0])][b.index(s[1])] = 1 a[b.index(t[0])][b.index(t[1])] = 1 tr = a[0][0] + a[1][1] + a[2][2] print("YES") if (s == t): if (s[0] == s[1]): print("abc"*n) else: i = 0 while b[i] == s[0] or b[i] == s[1]: i += 1 print(s[0]*n + b[i]*n + s[1]*n) else: max_row_sum = 0 for i in range(3): if a[i][0] + a[i][1] + a[i][2] > max_row_sum: max_row_sum = a[i][0] + a[i][1] + a[i][2] index = i max_col_sum = 0 for i in range(3): if a[0][i] + a[1][i] + a[2][i] > max_col_sum: max_col_sum = a[0][i] + a[1][i] + a[2][i] index_col = i if tr == 2: print('abc'*n) elif tr == 0 and max_row_sum == 2: i = 0 while i == index: i += 1 j = 0 while i == j or index == j: j += 1 print(b[i]*n + b[j]*n + b[index]*n) elif tr == 1: for i in range(3): for j in range(3): if a[i][j] and i != j: for k in range(3): if k != i and k != j: print((b[j] + b[i] + b[k])*n) else: if max_col_sum == 2: i = 0 while i == index_col: i += 1 j = 0 while i == j or index_col == j: j += 1 print(b[index_col]*n + b[j]*n + b[i]*n) elif (s[0] == t[1] and s[1] == t[0]): i = 0 while b[i] == s[0] or b[i] == s[1]: i += 1 print(s[0]*n + b[i]*n + s[1]*n) else: i = 0 while a[index][i] != 0 or i == index: i += 1 for j in range(3): if j != index and j != i: print((b[index] + b[i] + b[j])*n) ```
output
1
12,556
0
25,113
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,557
0
25,114
Tags: brute force, constructive algorithms Correct Solution: ``` n=int(input()) a=['abc'*n,'acb'*n,'bac'*n,'bca'*n,'cab'*n,'cba'*n,'a'*n+'b'*n+'c'*n,'a'*n+'c'*n+'b'*n,'b'*n+'a'*n+'c'*n,'b'*n+'c'*n+'a'*n,'c'*n+'b'*n+'a'*n,'c'*n+'a'*n+'b'*n] b=input() c=input() for i in a: if b not in i and c not in i: print("YES") print(i) exit() print("NO") ```
output
1
12,557
0
25,115
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,558
0
25,116
Tags: brute force, constructive algorithms Correct Solution: ``` n = int(input()) a = {'a', 'b', 'c'} s, t = input(), input() if t[0] == t[1]: s, t = t, s print('YES') if s[0] == s[1]: if t[0] == t[1]: print('abc'*n) elif t[0] == s[0]: print((s[0]+next(iter(a-set(t)))+t[1])*n) elif t[1] == s[0]: print((s[0]+t[0]+next(iter(a-set(t))))*n) else: print((s[0]+t[::-1])*n) else: if t[0] == s[0]: print(''.join(a-{t[0]})*n+t[0]*n) elif t[1] == s[1]: print(t[1]*n+''.join(a-{t[1]})*n) elif s == t[::-1]: print(s[0]*n+next(iter(a-set(s)))*n+s[1]*n) elif s[1] == t[0]: print(t[1]*n+t[0]*n+s[0]*n) else: print(s[1]*n+s[0]*n+t[0]*n) ```
output
1
12,558
0
25,117
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,559
0
25,118
Tags: brute force, constructive algorithms Correct Solution: ``` '''input 1 cb ac ''' import sys from collections import defaultdict as dd from itertools import permutations as pp from itertools import combinations as cc from collections import Counter as ccd from random import randint as rd from bisect import bisect_left as bl from heapq import heappush as hpush from heapq import heappop as hpop mod=10**9+7 def ri(flag=0): if flag==0: return [int(i) for i in sys.stdin.readline().split()] else: return int(sys.stdin.readline()) n =ri(1) s = input() t = input() a="abc" for i in pp(a): new = "".join(i) one= new*n two= new[0]*n + new[1]*n + new[2]*n if s not in one and t not in one: print("YES") print(one) sys.exit(0) if s not in two and t not in two: print("YES") print(two) sys.exit(0) print("NO") ```
output
1
12,559
0
25,119
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc
instruction
0
12,560
0
25,120
Tags: brute force, constructive algorithms Correct Solution: ``` n = int(input()) s = input() t = input() ans = '' def gen_singular(c): global ans ans += (c * n) def gen_pairs(p, q): global ans ans += (p+q) * n def d(s): return s[0] != s[1] def e(p): return p[0] == p[1] def rest(s, t): return [el for el in ['a', 'b', 'c'] if el not in set(s+t)][0] def attempt(s, t): global ans #1 if s[0] == t[0] and s[1] != t[1] and d(s) and d(t): gen_singular(s[1]) gen_singular(t[1]) gen_singular(s[0]) #2 if s[1] == t[1] and s[0] != t[0] and d(s) and d(t): gen_singular(s[1]) gen_singular(t[0]) gen_singular(s[0]) #3 if e(s) and t[0] == s[0] and d(t): gen_singular(t[1]) gen_pairs(s[0], rest(s, t)) #4 if e(s) and t[1] == s[0] and d(t): gen_pairs(s[0], rest(s, t)) gen_singular(t[0]) #5 if d(s) and d(t) and s[1] == t[0] and s[0] != t[1]: gen_singular(t[1]) gen_singular(s[1]) gen_singular(s[0]) #6 if s[0] == s[1] == t[0] == t[1]: if s[0] == 'a': gen_pairs(s[0], 'c') gen_singular('b') else: gen_pairs(s[0], 'a') gen_singular(rest('a', s[0])) #7 if d(s) and d(t) and t == s[::-1]: gen_singular(s[0]) gen_singular(rest(s[0], s[1])) gen_singular(s[1]) #8 if e(s) and d(t) and s[0] != t[0] and s[0] != t[1]: gen_pairs(t[0], s[0]) gen_singular(t[1]) #9 if d(s) and s == t: gen_singular(s[0]) gen_singular(rest(s[0], s[1])) gen_singular(s[1]) #10 if e(s) and e(t) and s[0] != t[0]: gen_pairs(s[0], t[0]) gen_singular(rest(s[0], t[0])) attempt(s, t) if len(ans) == 0: attempt(t, s) print('YES') print(ans) ```
output
1
12,560
0
25,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` n = int(input()) s = input() t = input() print('YES') if len(set(s + t)) == 1: print('abc' * n) elif len(set(s + t)) == 2: if t[0] == t[1]: s, t = t, s if s[0] == s[1]: a = s[0] b = list(set(s + t) - {a})[0] c = list(set('abc') - {a, b})[0] if s[0] == t[1]: b, c = c, b print((b + a + c) * n) else: a, b = s c = list(set('abc') - {a, b})[0] print(a * n + c * n + b * n) else: if t[0] == t[1]: s, t = t, s if s[0] == s[1]: a = s[0] b, c = t print((b + a + c) * n) else: if s[0] == t[0]: c, a = s b = t[1] elif s[1] == t[1]: b, a = s c = t[0] elif s[0] == t[1]: c, b = s a = t[0] else: b, a = s c = t[1] print(a * n + b * n + c * n) ```
instruction
0
12,561
0
25,122
Yes
output
1
12,561
0
25,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` from itertools import permutations n = int(input()) s1, s2 = input() t1, t2 = input() print("YES") if s1==t2 and s2==t1 and s1 != s2: x3 = list(set("abc")-set([s1,s2]))[0] print(s1*n+x3*n+s2*n) else: for x1,x2,x3 in permutations("abc"): possible = True for a1, a2 in [(x1, x2), (x2, x3), (x3, x1)]: if (a1==s1 and a2==s2) or (a1==t1 and a2==t2): possible = False break if possible: print((x1+x2+x3)*n) exit() if s1==t1: print((s2+t2)*n+s1*n) exit() if s2==t2: print(s2*n+(s1+t1)*n) exit() ```
instruction
0
12,562
0
25,124
Yes
output
1
12,562
0
25,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` n = int(input()) s = input() t = input() characters = ['a', 'b', 'c'] print('YES') if s[0] == s[1] and t[0] == t[1]: x = 'abc' print(x*n) elif s[0] == s[1]: if 'a' not in t: x = t[0] + 'a' + t[1] elif 'b' not in t: x = t[0] + 'b' + t[1] else: x = t[0] + 'c' + t[1] print(x*n) elif t[0] == t[1]: if 'a' not in s: x = s[0] + 'a' + s[1] elif 'b' not in s: x = s[0] + 'b' + s[1] else: x = s[0] + 'c' + s[1] print(x*n) elif (s[1] == t[1]) or (s[0] == t[0]): if s[1] == t[1]: characters.remove(s[1]) print(s[1]*n + ''.join(characters)*n) else: characters.remove(s[0]) print(''.join(characters)*n + s[0]*n) elif s[0] == t[1] and s[1] == t[0]: characters.remove(s[0]) characters.remove(s[1]) print(s[0]*n + characters[0]*n + s[1]*n) else: if 'a' not in s: x = s[0] + 'a' + s[1] elif 'b' not in s: x = s[0] + 'b' + s[1] else: x = s[0] + 'c' + s[1] print(x*n) ```
instruction
0
12,563
0
25,126
Yes
output
1
12,563
0
25,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` n=int(input()) a=n s1=input() s2=input() print('YES') def solver(x,n,r): s1='' s2='' for i in range(n): s1+=x s2+=r[0] s2+=r[1] print(s1+s2,end="") exit(0) def rolver(x,s1,s2,n): if(x=='a'): r='bc' if(x=='b'): r='ca' if(x=='c'): r='ab' if(r==s1 or r==s2): r=r[::-1] x1='' g1='' for i in range(n): x1+=x g1+=r if(str(r[1]+x)!=s1 and str(r[1]+x)!=s2): print(g1+x1) exit(0) print(x1+g1) exit(0) g1=['abc','acb','bac','bca','cab','cba'] if(s1[0]!=s1[1] and s2[0]!=s2[1]): for i in range(6): t=g1[i] if(s1 not in t and s2 not in t): for x in range(3): for j in range(n): print(g1[i][x],end="") exit(0) for i in range(6): t=g1[i]+g1[i] if(s1 not in t and s2 not in t): for j in range(n): print(g1[i],end="") exit(0) ```
instruction
0
12,564
0
25,128
Yes
output
1
12,564
0
25,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` n = int(input()) s = input() s1 = input() p = ['a', 'b', 'c'] if s[0] == s1[0] or s1[1] == s[1] : if s == s1: for i in range(3): for j in range(3): for k in range(3): ss = p[i] + p[j] + p[k] if p[i] != p[j] and p[i] != p[k] and p[j] != p[k]: if s not in ss and s1 not in ss: if n == 1: print("YES") print(ss) exit() if ss[-1] + ss[0] != s and ss[-1] + ss[0] != s1: print("YES") print(ss * n) exit() print("NO") exit() else: if s[0] == s[1] or s1[0] == s1[1]: if s[0] == s[1]: k = ['a', 'b', 'c'] k.pop(k.index(s1[0])) k.pop(k.index(s1[1])) print("YES") print((s1[0] + k[0] + s1[1]) * n) else: k = ['a', 'b', 'c'] k.pop(k.index(s[0])) k.pop(k.index(s[1])) print("YES") print((s[0] + k[0] + s[1]) * n) exit() print('YES') if s[0] == s1[0]: print(s[1] * n + s1[1] * n + s[0] * n) else: print(s[1] * n + s[0] * n + s1[0] * n) exit() if s == s1[::-1]: if n == 1: print("YES") p.pop(p.index(s[0])) p.pop(p.index(s[1])) print(s[0] + p[0] + s[1]) exit() print("NO") exit() for i in range(3): for j in range(3): for k in range(3): ss = p[i] + p[j] + p[k] if p[i] != p[j] and p[i] != p[k] and p[j] != p[k]: if s not in ss and s1 not in ss: if n == 1: print("YES") print(ss) exit() if ss[-1] + ss[0] != s and ss[-1] + ss[0] != s1: print("YES") print(ss * n) exit() print("NO") ```
instruction
0
12,565
0
25,130
No
output
1
12,565
0
25,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` import math import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input().strip() return(list(s[:len(s)])) def invr(): return(map(int,input().split())) n=inp() s=input().strip() t=input().strip() l=['abc','acb','bac','bca','cab','cba'] templ=[] for each in l: if s in each : continue elif t in each: continue else: templ.append(each) res='' flag=1 l=templ for i in range(n): count=0 temp=res+l[count] while(s in temp or t in temp): if count>=len(l): print('NO') flag=0 break else: temp=res+l[count] count+=1 res=temp if flag: print('YES') print(res) ```
instruction
0
12,566
0
25,132
No
output
1
12,566
0
25,133
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` n = int(input()) s1, s2 = input(), input() print("YES") if s1[0] != s1[1] and s1 == s2[::-1]: print(s1[0]*n + (({'a','b','c'}-set(s1)).pop())*n + s1[1]*n) exit() if s1[0] != s1[1] and s2[0] != s2[1]: if s1[1] == s2[0]: print(s2[1]*n + s2[0]*n + (({'a','b','c'}-set(s2)).pop())*n) else: print(s1[1]*n + s1[0]*n + (({'a','b','c'}-set(s1)).pop())*n) exit() if s1[0] == s1[1] and s2[0] == s2[1]: print('abc'*n) exit() if s1[0] == s1[1]: s = s2[::-1] else: s = s1[::-1] s += ({'a', 'b', 'c'} - set(s)).pop() print(s*n) ```
instruction
0
12,567
0
25,134
No
output
1
12,567
0
25,135
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t both of length 2 and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings s and t: "ab", "ca", "bb". You have to find a string res consisting of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer n (1 ≀ n ≀ 10^5) β€” the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string s of length 2 consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string t of length 2 consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string res on the second line. res should consist of 3n characters, n characters should be 'a', n characters should be 'b' and n characters should be 'c' and s and t should not occur in res as substrings. If there are multiple answers, you can print any of them. Examples Input 2 ab bc Output YES acbbac Input 3 aa bc Output YES cacbacbab Input 1 cb ac Output YES abc Submitted Solution: ``` import math import sys from collections import defaultdict # input = sys.stdin.readline rt = lambda: map(int, input().split()) ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) def main(): n = ri() s = input() t = input() trios = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] if n == 1: found = None for trio in trios: if s not in trio and t not in trio: found = trio break if found: print('YES') print(found) else: print('NO') else: found = None for t1 in trios: x = t1 + t1 if s not in x and t not in x: found = t1 break if found: print('YES') print(found*n) else: print('NO') if __name__ == '__main__': main() ```
instruction
0
12,568
0
25,136
No
output
1
12,568
0
25,137
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,634
0
25,268
Tags: implementation, strings Correct Solution: ``` t=int(input()) for _ in range(t): n,k=map(int,input().split()) a=list(input()) flag=1 for i in range(n): if(a[i]=="?"): if(i+k<n): a[i]=a[i+k] else: if(a[i]=="0"): if(i+k<n): if(a[i]!=a[i+k] and a[i+k]=="1"): flag=0 break a[i+k]=a[i] if(i-k>=0 ): if(a[i]!=a[i-k]and a[i-k]=="1"): flag=0 break a[i-k]=a[i] else: if(i+k<n): if(a[i]!=a[i+k]and a[i+k]=="0"): flag=0 break a[i+k]=a[i] #print(i+k) if(i-k>=0): if(a[i]!=a[i-k]and a[i-k]=="1"): flag=0 break a[i-k]=a[i] #print(a) one=0 zero=0 doubt=0 #print(a) for i in range(k): if(a[i]=="1"): one+=1 elif(a[i]=="0"): zero+=1 else: doubt=0 if(one>k/2 or zero>k/2): print("NO") continue oneneed=k/2-one zeroneed=k/2-zero #print("YES",flag) #print(a) for i in range(k,n): if(a[i-k]=='1'): one-=1 elif(a[i-k]=='0'): zero-=1 if(a[i]=='1'): one+=1 if(a[i]=="0"): zero+=1 if(one>k/2 or zero>k/2): #print(i,zero) flag=0 break if(flag==0): print("NO") else: print("YES") ```
output
1
12,634
0
25,269
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,635
0
25,270
Tags: implementation, strings Correct Solution: ``` def Test(): t=int(input()) while t>0: t-=1 soln() def soln(): nk=list(map(int,input().strip().split(" "))) n=nk[0] k=nk[1] b=input() a=[] for i in range(len(b)): a.append(b[i]) if k%2!=0: print("NO") return c1=0 c0=0 cnt=0 for i in range(k): if a[i]=="0": c0+=1 elif a[i]=="1": c1+=1 else: cnt+=1 hl=k//2 if c0>hl or c1>hl: print("NO") return # while "?" in a: i=0 while(i+k<n): if a[i]!="?" and a[i+k]!="?" and a[i]!=a[i+k]: print("NO") return elif a[i]!="?" and a[i+k]=="?": a[i+k]=a[i] elif a[i]=="?" and a[i+k]!="?": a[i]=a[i+k] if a[i]=='0': c0+=1 else: c1+=1 if c0>hl or c1>hl: print("NO") return i+=1 print("YES") Test() ```
output
1
12,635
0
25,271
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,636
0
25,272
Tags: implementation, strings Correct Solution: ``` def sol(): n,k = map(int,input().split()) s = list(input()) gg= False one=0 zero=0 #100110 for i in range(k): ck =0 for y in range(i,n,k): if s[y]=='1': ck|=2 if s[y]=='0': ck|=1 if ck==3: return "NO" if ck==2: one+=1 elif ck==1: zero+=1 if max(zero,one)> k/2: gg=True if gg:return "NO" else: return "YES" t = int(input()) while t: t-=1 print(sol()) ```
output
1
12,636
0
25,273
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,637
0
25,274
Tags: implementation, strings Correct Solution: ``` for _ in range(int(input())): n,k=map(int,input().split()) s=input() def solve(n,k,s): l=list(s) for i in range(k): t=s[i] for j in range(i,n,k): if s[j]!='?': if t!='?' and s[j]!=t: return False t=s[j] for j in range(i,n,k): l[j]=t return max(l[:k].count('1'),l[:k].count('0')) <= k//2 if solve(n,k,s): print('YES') else: print('NO') ```
output
1
12,637
0
25,275
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,638
0
25,276
Tags: implementation, strings Correct Solution: ``` import sys for _ in range(int(input())): n,k= map(int,input().split()) s=input() cnt0=0 cnt1=0 f=0 for i in range(k): S=set() for j in range(i,n,k): if s[j]=='?': continue S.add(s[j]) if len(S)>1: f=1 print("NO") break if '1' in S: cnt1+=1; elif '0' in S: cnt0+=1 if(not f): if cnt0>k//2: print("NO") elif cnt1>k//2: print("NO") else: print("YES") ```
output
1
12,638
0
25,277
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,639
0
25,278
Tags: implementation, strings Correct Solution: ``` import os from io import BytesIO, IOBase import sys import math from math import ceil from collections import Counter def main(): for t in range(int(input())): n,k=map(int,input().split()) s=list(input().rstrip()) ans="YES" o=0 z=0 for i in range(k): if s[i]=="1": o+=1 if s[i]=="0": z+=1 if o>(k//2) or z>(k//2): ans="NO" if ans=="YES": for i in range(0,n-k): if s[i]=="0": if s[i+k]=="1": ans="NO" break else: if s[i+k]=="?": s[i+k]="0" elif s[i]=="1": if s[i+k]=="0": ans="NO" break else: if s[i+k]=="?": s[i+k]="1" o,z=0,0 for i in range(n-k,n): if s[i]=="1": o+=1 if s[i]=="0": z+=1 if o > (k // 2) or z > (k // 2): ans = "NO" print(ans) # 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") if __name__ == "__main__": main() ```
output
1
12,639
0
25,279
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,640
0
25,280
Tags: implementation, strings Correct Solution: ``` for __ in range(int(input())): n, k = list(map(int, input().split())) ar = list(input()) kek = [-1] * k ans = 'YES' for j in range(0, n, k): for i in range(j, j + k): if i < n: if ar[i] == '0': if kek[i % k] == 1: ans = 'NO' kek[i % k] = 0 elif ar[i] == '1': if kek[i % k] == 0: ans = 'NO' kek[i % k] = 1 for i in range(n): if ar[i] == '?' and kek[i % k] != -1: ar[i] = str(kek[i % k]) a, b = 0, 0 for i in range(k): if ar[i] == '0': a += 1 elif ar[i] == '1': b += 1 if a > k // 2 or b > k // 2: ans = 'NO' for i in range(k): if ar[i] == '?': if a < k // 2: a += 1 ar[i] = '0' kek[i] = 0 elif b < k // 2: b += 1 ar[i] = '1' kek[i] = 1 else: ans = 'NO' for i in range(n): if ar[i] == '?' and kek[i % k] != -1: ar[i] = str(kek[i % k]) a, b = 0, 0 for i in range(k): if ar[i] == '0': a += 1 elif ar[i] == '1': b += 1 if a != b: ans = 'NO' for i in range(k, n): if ar[i] != ar[i - k]: ans = 'NO' print(ans) ```
output
1
12,640
0
25,281
Provide tags and a correct Python 3 solution for this coding contest problem. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110.
instruction
0
12,641
0
25,282
Tags: implementation, strings Correct Solution: ``` for _ in range(int(input())): n,ws=map(int,input().split()) st=input() s=list(st) f=0 start,end=0,ws while(end<n): if(s[end]!='?' and s[start]!='?'): if(s[end]!=s[start]): f=1 break elif(s[end]=='?' and s[start]!='?'): s[end]=s[start] elif(s[end]!='?' and s[start]=='?'): s[start]=s[end] end+=1 start+=1 if(f==1): print('NO') continue #print(s) start=n-ws-1 end=n-1 while(start>=0): if(s[start]=='?' and s[end]!='?'): s[start]=s[end] elif(s[start]!='?' and s[end]=='?'): s[end]=s[start] start-=1 end-=1 #print(s) f=0 one,zero=0,0 for i in range(ws): if(s[i]=='1'): one+=1 elif(s[i]=='0'): zero+=1 if(one>ws//2 or zero>ws//2): print('NO') else: start,end=0,ws f=0 while(end<n): if(s[start]=='1'): one-=1 elif(s[start]=='0'): zero-=1 if(s[end]=='0'): zero+=1 elif(s[end]=='1'): one+=1 if(one>ws//2 or zero>ws//2): f=1 break start+=1 end+=1 if(f==0): print('YES') else: print('NO') ```
output
1
12,641
0
25,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called k-balanced if every substring of size k of this bitstring has an equal amount of 0 and 1 characters (k/2 of each). You are given an integer k and a string s which is composed only of characters 0, 1, and ?. You need to determine whether you can make a k-balanced bitstring by replacing every ? characters in s with either 0 or 1. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and k (2 ≀ k ≀ n ≀ 3 β‹… 10^5, k is even) β€” the length of the string and the parameter for a balanced bitstring. The next line contains the string s (|s| = n). It is given that s consists of only 0, 1, and ?. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, print YES if we can replace every ? in s with 0 or 1 such that the resulting bitstring is k-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a 4-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110. Submitted Solution: ``` ###################################################### ############Created by Devesh Kumar################### #############devesh1102@gmail.com#################### ##########For CodeForces(Devesh1102)################# #####################2020############################# ###################################################### import sys input = sys.stdin.readline # import sys import heapq import copy import math import decimal # import sys.stdout.flush as flush # from decimal import * #heapq.heapify(li) # #heapq.heappush(li,4) # #heapq.heappop(li) # # & Bitwise AND Operator 10 & 7 = 2 # | Bitwise OR Operator 10 | 7 = 15 # ^ Bitwise XOR Operator 10 ^ 7 = 13 # << Bitwise Left Shift operator 10<<2 = 40 # >> Bitwise Right Shift Operator # '''############ ---- Input Functions ---- #######Start#####''' def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def insr2(): s = input() return((s[:len(s) - 1])) def invr(): return(map(int,input().split())) ############ ---- Input Functions ---- #######End # ##### ans = 0 def pr_list(a): print( *a , sep=" ") def swap(a,b): temp = a a = b b = temp return [ a,b] # return [b,a] def main(): tests = inp() # tests = 1 mod = 1000000007 limit = 10**18 ans = 0 stack = [] hashm = {} arr = [] heapq.heapify(arr) for test in range(tests): [n,k] = inlt() s = insr() hashm= {} hashm["1"] = 0 hashm["0"] = 0 hashm["?"] = 0 present = [] # for i in range(k): # hashm[s[i]] = hashm[s[i]] +1 # if s[i] == "?": # present.append(i) flag =0 h = k//2 for i in range(n-k): if s[i] != s[i+k]: if s[i] == "?": s[i] = s[i+k] elif s[i+k] == "?": s[i+k] = s[i] else: flag = 1 break if flag==1: print("NO") continue for i in range(k): hashm[s[i]] = hashm[s[i]] +1 if abs( hashm["1"] - hashm["0"]) > hashm["?"]: flag = 1 for i in range(n-k): if abs( hashm["1"] - hashm["0"]) > hashm["?"]: flag = 1 hashm[s[i]] = hashm[s[i]] - 1 hashm[s[i+k]] = hashm[s[i+k]] + 1 if flag==1: print("NO") continue else: print("YES") if __name__== "__main__": main() ```
instruction
0
12,642
0
25,284
Yes
output
1
12,642
0
25,285