message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.) Snuke wants to satisfy the following condition: * For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome. Find the minimum possible value of N when the partition satisfies the condition. Constraints * 1 \leq |s| \leq 2 \times 10^5 * s consists of lowercase English letters. Input Input is given from Standard Input in the following format: s Output Print the minimum possible value of N when the partition satisfies the condition. Examples Input aabxyyzz Output 2 Input byebye Output 1 Input abcdefghijklmnopqrstuvwxyz Output 26 Input abcabcxabcx Output 3 Submitted Solution: ``` from heapq import heappush, heappop from itertools import accumulate s = input() N = len(s) cnt = [[0] * (N+1) for _ in range(26)] for i in range(N): cnt[ord(s[i]) - ord('a')][i+1] += 1 for c in range(26): cnt[c] = list(accumulate(cnt[c])) dp = [(0, 0)] for i in range(N): tmp = dp[:] res = 0 while tmp: cost, j = heappop(tmp) odd = 0 for c in range(26): if (cnt[c][i+1] - cnt[c][j]) % 2 == 1: odd += 1 if odd <= 1: res = cost + 1 break heappush(dp, (res, i + 1)) for cost, j in dp: if j == N: print(cost) break ```
instruction
0
78,126
0
156,252
No
output
1
78,126
0
156,253
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,143
0
156,286
"Correct Solution: ``` s = list(input()) t = s[::-1] for i, k in enumerate(t): if k == "b": t[i] = "d" elif k == "d": t[i] = "b" elif k == "p": t[i] = "q" elif k == "q": t[i] = "p" if s == t: print("Yes") else: print("No") ```
output
1
78,143
0
156,287
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,144
0
156,288
"Correct Solution: ``` s=list(input());print(['No','Yes'][s==[{'b':'d','d':'b','p':'q','q':'p'}[i]for i in s[::-1]]]) ```
output
1
78,144
0
156,289
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,145
0
156,290
"Correct Solution: ``` def main(): S = input() m = dict() m['d'] = 'b' m['b'] = 'd' m['p'] = 'q' m['q'] = 'p' T = "" for i in range(len(S)-1, -1, -1): T += m[S[i]] if T == S: return "Yes" return "No" if __name__ == '__main__': print(main()) ```
output
1
78,145
0
156,291
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,146
0
156,292
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return input() def main(): s=S() t=s s=list(s) s.reverse() s=''.join(s) s=s.replace('b','1') s=s.replace('d','2') s=s.replace('p','3') s=s.replace('q','4') s=s.replace('1','d') s=s.replace('2','b') s=s.replace('3','q') s=s.replace('4','p') if s==t: return 'Yes' return 'No' # main() print(main()) ```
output
1
78,146
0
156,293
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,147
0
156,294
"Correct Solution: ``` s=list(input()) n=len(s) if n%2==1: print('No') else: k=n//2 for i in range(k): if s[i]=='b' and not s[n-1-i]=='d': print('No') exit() if s[i]=='d' and not s[n-i-1]=='b': print('No') exit() if s[i]=='p' and not s[n-1-i]=='q': print('No') exit() if s[i]=='q' and not s[n-i-1]=='p': print('No') exit() print('Yes') ```
output
1
78,147
0
156,295
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,148
0
156,296
"Correct Solution: ``` r=str.replace s=input() print(['No','Yes'][s==''.join(reversed(r(r(r(r(r(r(r(r(s,'b','0'),'d','1'),'p','2'),'q','3'),'0','d'),'1','b'),'2','q'),'3','p')))]) ```
output
1
78,148
0
156,297
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,149
0
156,298
"Correct Solution: ``` # -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 S = input() N = len(S) D = {'b': 'd', 'd': 'b', 'p': 'q', 'q': 'p'} T = list(S)[::-1] for i in range(N): T[i] = D[T[i]] T = ''.join(T) if S == T: Yes() else: No() ```
output
1
78,149
0
156,299
Provide a correct Python 3 solution for this coding contest problem. You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d` by `b`, `p` by `q`, and `q` by `p`, simultaneously. Constraints * 1 \leq |S| \leq 10^5 * S consists of letters `b`, `d`, `p`, and `q`. Input The input is given from Standard Input in the following format: S Output If S is a mirror string, print `Yes`. Otherwise, print `No`. Examples Input pdbq Output Yes Input ppqb Output No
instruction
0
78,150
0
156,300
"Correct Solution: ``` import string s = input() s2 = s[::-1] dst = s2.translate(str.maketrans('bdpq', 'dbqp')) if (s == dst): print('Yes') else: print('No') ```
output
1
78,150
0
156,301
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,397
0
156,794
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` #longest prefix which is also suffix #return same prefic which is also suffix they both shl't overlap #get longest prefix front string (0) string+'#'+string[::-1] #get longest palindrome form last string[::-1]+'#'+string def LPS(s): s=s+'#'+s[::-1] n=len(s) dp=[0]*n j=0 i=1 while i<n: if s[i]==s[j]: dp[i]=j+1 i+=1 j+=1 else: if j!=0:j=dp[j-1] else: dp[i]=0 i+=1 return s[:dp[-1]] for _ in range(int(input())): s=input() l=0 n=len(s) while l<n//2 and s[l]==s[-(l+1)]: l+=1 pp=LPS(s[l:n-l]) ss=LPS(s[l:n-l][::-1]) if len(ss)>len(pp): print(s[:l]+ss+s[:l][::-1]) else: print(s[:l]+pp+s[:l][::-1]) ```
output
1
78,397
0
156,795
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,398
0
156,796
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` import sys;input=sys.stdin.readline def manacher(S): N=len(S) r,p=0,0 A=[0]*N for i in range(N): if i<=r: A[i]=min(A[2*p-i],r-i) else: A[i]=0 while (i-A[i]-1>=0) and (i+A[i]+1<N) and (S[i-A[i]-1]==S[i+A[i]+1]): A[i]+=1 if r<i+A[i]: r=i+A[i] p=i return A def f(S): return ''.join([i for i in S if i !='#']) for i in range(int(input())): b=input().strip() n=len(b) if n%2==0: c='' v=b[:n//2] else: c=b[n//2] v=b[:n//2] for i in range(n//2): if b[i]!=b[n-1-i]: c=b[i:n-i] v=b[:i] break a='#'.join(c) a='#'+a+'#' A=manacher(a) ans1='' for i in range(len(c),0,-1): if i==A[i]: ans1=a[:i*2] break ans1=f(ans1) ans2='' for i in range(len(c),0,-1): if i==A[-1-i]: ans2=a[-i*2-1:] break ans2=f(ans2) if len(ans1)>len(ans2): ans=ans1 else: ans=ans2 print(v+ans+v[::-1]) ```
output
1
78,398
0
156,797
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,399
0
156,798
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` # -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') # sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 def Manacher(S): N = len(S) R = [0] * N i = j = 0 while i < N: while i-j >= 0 and i+j < N and S[i-j] == S[i+j]: j += 1 R[i] = j k = 1 while i-k >= 0 and i+k < N and k+R[i-k] < j: R[i+k] = R[i-k] k += 1 i += k j -= k return R def Manacher_even(_S): _N = len(_S) S = [] for i in range(_N-1): S.append(_S[i]) S.append('$') S.append(_S[-1]) N = len(S) R = [0] * N i = j = 0 while i < N: while i-j >= 0 and i+j < N and S[i-j] == S[i+j]: j += 1 R[i] = j k = 1 while i-k >= 0 and i+k < N and k+R[i-k] < j: R[i+k] = R[i-k] k += 1 i += k j -= k res = [0] * (_N-1) j = 0 for i in range(1, N, 2): res[j] = R[i] // 2 j += 1 return res for _ in range(INT()): S = input() N = len(S) i = 0 j = N - 1 T1 = [] while i < j: if S[i] == S[j]: T1.append(S[i]) i += 1 j -= 1 else: break else: print(S) continue T3 = T1[::-1] R = Manacher(S[i:j+1]) mx = a = b = 0 for k, r in enumerate(R): k += i ln = r * 2 - 1 if k-r+1 == i: if ln > mx: mx = ln a, b = i, i+ln if k+r-1 == j: if ln > mx: mx = ln a, b = j+1-ln, j+1 R = Manacher_even(S[i:j+1]) for k, r in enumerate(R): k += i + 1 ln = r * 2 if k-r == i: if ln > mx: mx = ln a, b = i, i+ln if k+r-1 == j: if ln > mx: mx = ln a, b = j+1-ln, j+1 T2 = list(S[a:b]) ans = T1 + T2 + T3 print(''.join(ans)) ```
output
1
78,399
0
156,799
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,400
0
156,800
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` ####################################################################################################################### # Author: BlackFyre # Language: PyPy 3.7 ####################################################################################################################### from sys import stdin, stdout, setrecursionlimit from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, log2 from random import seed, randint from datetime import datetime from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect from collections import defaultdict as dd mod = pow(10, 9) + 7 mod2 = 998244353 # setrecursionlimit(3000) def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var) + "\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)] def remadd(x, y): return 1 if x % y else 0 def ceil(a, b): return (a + b - 1) // b def def_value(): return 0 def def_inf(): return inf def atoi(s): return ord(s)-ord('a') def inc(s, n=1): return chr((ord(s)-ord('a')+n)%26+ord('a')) class XY: def __init__(self, x, y): self.x = x self.y = y def rotate(a,b,c): ang = (b.y-a.y)*(c.x-b.x) - (b.x-a.x)*(c.y-b.y) if ang == 0: return 0 #0coll, +-clock, --aclock else: return ang/abs(ang) for _ in range(iinp()): s = inp() n, k = len(s),0 for i in range(n//2): if s[i]!=s[n-1-i]: break k += 1 ss = s[k:n-k] sss = ss[::-1] temp = ss + "?" + sss nn = len(temp) lps = l1d(nn,0) for i in range(1,nn): ln = lps[i-1] while ln>0 and temp[ln]!=temp[i]: ln = lps[ln-1] if temp[i]==temp[ln]: ln+=1 lps[i]=ln pre = temp[0:lps[nn-1]] temp = sss + "?" + ss lps = l1d(nn,0) for i in range(1,nn): ln = lps[i-1] while ln>0 and temp[ln]!=temp[i]: ln = lps[ln-1] if temp[i]==temp[ln]: ln+=1 lps[i]=ln suf = temp[0:lps[nn-1]] if len(suf)>len(pre): print(s[:k] + suf + s[n-k:]) else: print(s[:k] + pre + s[n-k:]) ```
output
1
78,400
0
156,801
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,401
0
156,802
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` """ Template written to be used by Python Programmers. Use at your own risk!!!! Owned by enraged(rating - 5 star at CodeChef and Specialist at Codeforces). """ import sys import heapq from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt from collections import defaultdict as dd, deque, Counter as c from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect # sys.setrecursionlimit(2*pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(var): sys.stdout.write(str(var)) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] def post_process(string, ml, c): result = [] for i in string[c-ml:c+ml+1]: if i == '#': continue result.append(i) return ''.join(result) def manacher(string): arr = ['#'] for i in string: arr.append(i) arr.append('#') left = l1d(len(arr)) r, c = 0, 0 for i in range(len(arr)): mirror = 2 * c - i if r > i: left[i] = min(r - i, left[mirror]) try: while arr[i + 1 + left[i]] == arr[i - 1 - left[i]]: left[i] += 1 except: pass if i + left[i] > r: c = i r = i + left[i] maxlength = 0 center = 0 for i in range(len(left)): if left[i] > maxlength and (i == left[i] or (i + left[i]) >= len(left)-1): maxlength = left[i] center = i return post_process(arr, maxlength, center) for _ in range(int(data())): s = data() if s[::-1] == s: out(s + "\n") continue i, j = 0, len(s) - 1 while i < j: if s[i] == s[j]: i += 1 j -= 1 else: break if i == 0: out(manacher(s)+"\n") else: out(s[:i]+manacher(s[i:j+1])+s[j+1:]+"\n") ```
output
1
78,401
0
156,803
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,402
0
156,804
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` def Z_Array(l): left=0 right=0 n=len(l) Z=[0]*n for i in range(1,n): if i>right: left=i right=i while (right<n and l[right]==l[right-left]): right+=1 Z[i]=right-left right-=1 else: k=i-left if (Z[k]+i-1)<right: Z[i]=Z[k] else: left=i while (right<n and l[right]==l[right-left]): right+=1 Z[i]=right-left right-=1 return Z def Z_Algorithm(pattern, text): l=[p for p in pattern] l.append('#') l.extend([t for t in text]) Z=Z_Array(l) return Z def maximum_Length_Palindrome(Z,n): #print(Z) for i in range(n): if Z[i]==n-i: return Z[i] for _ in range(int(input())): s=input() n=len(s) N=2*n+1 if n==1 or s==s[::-1]: print(s) elif s[0]!=s[-1]: ans1=Z_Algorithm(s, s[::-1]) ans2=Z_Algorithm(s[::-1], s) #print(ans1,ans2) ans1=maximum_Length_Palindrome(ans1[N//2+1:],n) ans2=maximum_Length_Palindrome(ans2[N//2+1:],n) #print(ans1,ans2) if ans1>=ans2: print(s[:ans1]) else: s=s[::-1] print(s[:ans2]) else: i=0 j=n-1 s1=s[0:n] while i<j: if s[i]!=s[j]: break else: i+=1 j-=1 s=s[i:j+1] n=len(s) N=2*n+1 ans1=Z_Algorithm(s, s[::-1]) ans2=Z_Algorithm(s[::-1], s) #print(ans1,ans2) ans1=maximum_Length_Palindrome(ans1[N//2+1:],n) ans2=maximum_Length_Palindrome(ans2[N//2+1:],n) #print(ans1,ans2) if ans1>=ans2: s=s[:ans1] if s==s[::-1]: print(s1[0:i] + s[:ans1] + s1[0:i][::-1]) else: s=s[::-1] s=s[:ans2] if s==s[::-1]: print(s1[0:i] + s[:ans2] + s1[0:i][::-1]) ```
output
1
78,402
0
156,805
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,403
0
156,806
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` import sys import math from collections import defaultdict,deque import heapq def kmp(s): n=len(s) lps=[0 for _ in range(n)] l=0 lps[0]=0 i=1 while i<n: if s[i]==s[l]: l+=1 lps[i]=l i+=1 else: if l!=0: l=lps[l-1] else: lps[i]=0 i+=1 return lps[-1] t=int(sys.stdin.readline()) for _ in range(t): s=sys.stdin.readline()[:-1] n=len(s) i,j=0,n-1 while i<j: if s[i]==s[j]: i+=1 j-=1 else: break left=s[:i] right=s[j+1:] #print(left,'left') #print(right,'right') mid=s[i:j+1] #print(mid,'mid') new=mid+'#'+mid[::-1] #print(new,'new') x=kmp(new) nnew=mid[::-1]+'#'+mid[::-1][::-1] #print(nnew,'nenw') y=kmp(nnew) #print(x,'x',y,'y',y) if x>y: ans=left+new[:x]+right else: ans=left+nnew[::-1][:y][::-1]+right print(ans) ```
output
1
78,403
0
156,807
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".
instruction
0
78,404
0
156,808
Tags: binary search, greedy, hashing, string suffix structures, strings Correct Solution: ``` from sys import stdin from collections import defaultdict, Counter from math import gcd #wow def solver(s): # s is input string # start by finding common suffix prefix n = len(s) i = 0 for i in range(n//2): if s[i] != s[-(i + 1)]: break if i == 0: sub = s else: sub = s[i:-i] l = long_palindrome_ps(sub) return s[:i] + l + s[::-1][:i][::-1] def long_palindrome_ps(s): # given a string s, returns the longest palindrome that is the suffix or prefix of s. n = len(s) lps = [] ss = s + '#' + s[::-1] lps = lps_(ss) m_p = lps[-1] m_pp = s[:m_p] ss = s[::-1] + '#' + s lps = lps_(ss) m_s = lps[-1] m_sp = s[::-1][:m_s] if m_p > m_s: return m_pp else: return m_sp def lps_(s): # returns the pi or lps (longest prefix sum) array n = len(s) tr = [0] j = 0 for i in range(1, n): if s[i] == s[j]: j += 1 tr.append(j) else: while (s[j] != s[i]) and j > 0: j = tr[j - 1] if s[j] == s[i]: j += 1 tr.append(j) elif j == 0: tr.append(j) return tr T = int(input()) for _ in range(T): s = stdin.readline().strip() print(solver(s)) ```
output
1
78,404
0
156,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): string=input() n=len(string) if n==1: yield string[0] else: s,e=0,n-1 while s<e: if string[s]==string[e]: s+=1 e-=1 else: break form=[string[:s]] back=[string[e+1:]] srem=string[s:e+1] #print(srem) mid = [""] for i in srem: mid.append(i) mid.append("") R = [None] * len(mid) i = 0 j = 0 #print(mid) while i < len(mid): while i-j >= 0 and i+j < len(mid) and mid[i-j] == mid[i+j]: j+=1 R[i] = j k = 1 while i-k >= 0 and i+k < len(mid) and k+R[i-k] < j : R[i+k] = R[i-k] k+=1 i += k j -= k maxind = 0 for i in range(len(mid)): if ( i+1 == R[i] or R[i] == len(mid)-i) and R[maxind] < R[i]: maxind = i #print (mid,R) #print (maxind) for i in range(maxind - R[maxind] + 1 , maxind + R[maxind]): form.append(mid[i]) ans=form+back #print(form,back) yield "".join(ans) if __name__ == '__main__': t= int(input()) ans = gift() print(*ans,sep='\n') ```
instruction
0
78,405
0
156,810
Yes
output
1
78,405
0
156,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` import sys input = sys.stdin.readline T = int(input()) for _ in range(T): S = input().rstrip() N = len(S) L = 0 R = N - 1 while L < R and S[L] == S[R]: L += 1 R -= 1 if L == R: print(S) continue SUB = S[L: R+1] M = len(SUB) fail1 = [0] * M j = 0 for i in range(1, M): while j > 0 and SUB[i] != SUB[j]: j = fail1[j - 1] if SUB[i] == SUB[j]: j += 1 fail1[i] = j SUB2 = SUB[::-1] fail2 = [0] * M j = 0 for i in range(1, M): while j > 0 and SUB2[i] != SUB2[j]: j = fail2[j - 1] if SUB2[i] == SUB2[j]: j += 1 fail2[i] = j j = 0 for i in range(M): while j > 0 and SUB[i] != SUB2[j]: j = fail2[j - 1] if SUB[i] == SUB2[j]: j += 1 ans1 = S[:L] + S[R-j+1:R+1] + S[R+1:] j = 0 for i in range(M): while j > 0 and SUB2[i] != SUB[j]: j = fail1[j - 1] if SUB2[i] == SUB[j]: j += 1 ans2 = S[:L] + S[L:L+j] + S[R + 1:] if len(ans1) < len(ans2): print(ans2) else: print(ans1) ```
instruction
0
78,406
0
156,812
Yes
output
1
78,406
0
156,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` import math from bisect import bisect_left, bisect_right from sys import stdin, stdout, setrecursionlimit from collections import Counter input = lambda: stdin.readline().strip() print = stdout.write def LPS(s): n = len(s) lps = [0] * n l = 0 i = 1 while i<n: if s[i]==s[l]: l+=1 lps[i] = l i+=1 else: if l!=0: l = lps[l-1] else: lps[i] = 0 i+=1 res = lps[n-1] if res>n/2: return n//2 else: return res t = int(input()) for _ in range(t): s = input() n = len(s) i = 0 while i<n-i-1: if s[i]==s[n-i-1]: i+=1 else: break if i>n-i-1: print(s+'\n') else: string = s[i:n-i] right_index = i+LPS(string+'$'+string[::-1])-1 left_index = n-i-LPS(string[::-1]+'$'+string) if right_index>(n-left_index-1): print(s[:i]+s[i:right_index+1]+s[n-i:]+'\n') else: print(s[:i]+s[left_index:n-i]+s[n-i:]+'\n') ```
instruction
0
78,407
0
156,814
Yes
output
1
78,407
0
156,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` def z_function(s,z): n=len(s) l,r,k=0,0,0 for i in range(1,n): if(i>r): l,r=i,i while r<n and s[r-l]==s[r]: r+=1 z[i]=r-l r-=1 else: k=i-l if(z[k]<r-i+1): z[i]=z[k] else: l=i while r<n and s[r-l]==s[r]: r+=1 z[i]=r-l r-=1 import sys input=sys.stdin.readline t=int(input()) def solve(ss,res): mx=0 #print(ss) z=[0]*len(ss) z_function(ss,z) #print(ss) #print(z) ls=len(z) for i in range(len(z)-1,-1,-1): #print(z[i],ls-i+1) if(z[i]==ls-i): mx=max(mx,z[i]) #print(mx) for i in range(mx): res.append(ss[i]) nn=len(res) for i in range(nn-mx-1,-1,-1): res.append(res[i]) return "".join(res) while t: res1=[] res2=[] s=input().split()[0] n=len(s) if(n==1): print(s) else: i=0 j=n-1 while i<j: if(s[i]==s[j]): res1.append(s[i]) res2.append(s[i]) else: break i+=1 j-=1 ss=s[i:j] ss=ss+'*'+ss[::-1] s1=solve(ss,res1) ss=s[i:j+1] ss=ss[::-1]+'*'+ss s2=solve(ss,res2) if(len(s1)>=len(s2)): print(s1) else: print(s2) t-=1 ```
instruction
0
78,408
0
156,816
Yes
output
1
78,408
0
156,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` import sys import math input=sys.stdin.readline n=int(input()) def check(s): i=-1 j=len(s) while((i+1)<=(j-1)): if(s[i+1]==s[j-1]): i+=1 j-=1 else: i=-1 j-=1 # print(i,j) if(i==j): x=s[:i+1] return x+s[j+1:j+1+len(x)-1] else: # print("lol") x=s[:i+1] return x+s[j:j+len(x)] def fun(s): z1=check(s) z2=check(s[::-1]) # print(s,z1,z2,"hhhhhhh") if(len(z1)>=len(z2)): return z1 else: return z2 for t1 in range(n): t=input() t=t.strip() i=0 j=len(t)-1 # print(len(t)) # print(i,j) if(t[i]==t[j]): while(i<=j and t[i]==t[j]): i+=1 j-=1 # print(i,j,"lol") i-=1 j+=1 m="" if(i+1<=j-1): s=t[i+1:j] # print(s,"lol") ans=fun(s) m=ans # print(m,"mmmm") if(i==j): print(t[:i+1]+m+t[j+1:]) else: print(t[:i+1]+m+t[j:]) else: if(i==j): print(t[:i+1]+t[j+1:]) else: print(t[:i+1]+t[j:]) else: m=fun(t) print(m) ```
instruction
0
78,409
0
156,818
No
output
1
78,409
0
156,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` def longest(suf,pre,string): if pre == 1: s1 = string s2 = string[::-1] k = 0 for i in range(len(s2)): if s2[i] == s1[k]: k+=1 else: k=0 return s1[0:k] if suf == 1: s1 = string[::-1] s2 = string k = 0 for i in range(len(s2)): if s2[i] == s1[k]: k+=1 else: k=0 return s1[0:k] for _ in range(int(input())): s = input() n = len(s) i = 0 for i in range(len(s)//2): if s[i] == s[n-i-1]: continue else: break a = longest(0,1,s[i:n-i]) b = longest(1,0,s[i:n-i]) #print(a,b) c = '' if len(a) > len(b): c = a else: c = b print(s[0:i] + c + (s[0:i])[::-1]) ```
instruction
0
78,410
0
156,820
No
output
1
78,410
0
156,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): string=input() n=len(string) if n==1: yield string[0] else: ans="" s,e=0,n-1 while s<e: if string[s]==string[e]: ans+=string[s] s+=1 e-=1 else: break if s>e: yield ans+ans[::-1] continue elif s==e: yield ans+string[s]+ans[::-1] continue elif s==e-1: yield ans+string[s]+ans[::-1] continue countsame=1 for i in range(s+1,e+1): if string[i]==string[s]: countsame+=1 else: break curr=string[s:s+countsame] mid=s+countsame currlen=countsame while mid+currlen<=e: tempcur=string[s:mid] #print(curr,tempcur,mid,string[mid:mid+currlen],tempcur==string[mid+1:mid+1+currlen][::-1]) if tempcur==string[mid+1:mid+1+currlen][::-1]: #print(curr,1) curr=string[s:mid+currlen+1] mid=mid+currlen currlen=mid-s elif tempcur==string[mid:mid+currlen][::-1]: #print(curr,2) curr=string[s:mid+currlen] mid+=currlen currlen=mid-s else: mid+=1 currlen+=1 revcountsame=1 for i in range(1,e-s+1): #print(e,e-i,string[e-i],string[e]) if string[e-i]==string[e]: revcountsame+=1 else: break revcurr=string[e-revcountsame+1:e+1] mid=e-revcountsame currlen=revcountsame #print(revcurr,mid,currlen) while mid-currlen>=s: tempcur=string[mid+1:e+1] #print(tempcur,string[mid-currlen:mid],string[mid-currlen:e+1]) if tempcur==string[mid-currlen:mid][::-1]: #print(tempcur,string[mid-currlen:mid],2) revcurr=string[mid-currlen:e+1] mid-=currlen currlen=e-mid elif tempcur==string[mid-currlen+1:mid+1][::-1]: #print(tempcur,string[mid-currlen+1:mid+1],1) revcurr=string[mid-currlen+1:e+1] mid-=currlen currlen=e-mid else: mid-=1 currlen+=1 #print(revcurr ) len1,len2=len(curr),len(revcurr) print(curr,revcurr) if len1>=len2: yield ans+curr+ans[::-1] else: yield ans+revcurr+ans[::-1] if __name__ == '__main__': t= int(input()) ans = gift() print(*ans,sep='\n') ```
instruction
0
78,411
0
156,822
No
output
1
78,411
0
156,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: * The length of t does not exceed the length of s. * t is a palindrome. * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case. Each test case is a non-empty string s, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string s = "a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: * Its length is 9, which does not exceed the length of the string s, which equals 11. * It is a palindrome. * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s". Submitted Solution: ``` def solve(): a = input() b = a[::-1] j = 0 while a[j] == b[j] and j * 2 < len(a) - 1: j += 1 c = a[:j] b = b[j:len(b) - j] a = a[j:len(a) - j] a += '#' + a[::-1] b += '#' + b[::-1] al = [0] * len(a) bl = [0] * len(b) for i in range(1, len(a)): j = al[i - 1] while j > 0 and a[i] != a[j]: j = bl[j - 1] if a[i] == a[j]: j += 1 al[i] = j aa = a[0:al[len(a) - 1]] for i in range(1, len(b)): j = bl[i - 1] while j > 0 and b[i] != b[j]: j = al[j - 1] if b[i] == b[j]: j += 1 bl[i] = j bb = b[0:bl[len(b) - 1]] if len(bb) > len(aa): aa = bb print(c + aa + c[::-1]) n = int(input()) for i in range(n): solve() ```
instruction
0
78,412
0
156,824
No
output
1
78,412
0
156,825
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,604
0
157,208
Tags: data structures, implementation Correct Solution: ``` def readln(): return tuple(map(int, input().split())) q = input() cnt = [[0] * 3 for _ in range(len(q) + 1)] for i, v in enumerate(list(q)): for j in range(3): cnt[i + 1][j] = cnt[i][j] if v == 'x': cnt[i + 1][0] += 1 elif v == 'y': cnt[i + 1][1] += 1 else: cnt[i + 1][2] += 1 ans = [] for _ in range(readln()[0]): l, r = readln() tmp = [cnt[r][i] - cnt[l - 1][i] for i in range(3)] ans.append('YES' if r - l < 2 or max(tmp) - min(tmp) < 2 else 'NO') print('\n'.join(ans)) ```
output
1
78,604
0
157,209
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,605
0
157,210
Tags: data structures, implementation Correct Solution: ``` s = input() n = int(input()) tests = [] for _ in range(n): a, b = map(int, input().split()) a -= 1 b -= 1 tests.append((a, b)) count_left = {} count_left[-1] = (0, 0, 0) x, y, z = 0, 0, 0 for i, c in enumerate(s): if c == 'x': x += 1 elif c == 'y': y += 1 elif c == 'z': z += 1 count_left[i] = (x, y, z) def count(a, b): x2, y2, z2 = count_left[b] x1, y1, z1 = count_left[a - 1] x = x2 - x1 y = y2 - y1 z = z2 - z1 return x,y,z def is_valid(s, a, b): if b - a < 2: return True x,y,z = count(a, b) diffs = map(abs, [x-y, x-z, y-z]) return not any(d > 1 for d in diffs) result = [] for a, b in tests: result.append("YES" if is_valid(s, a, b) else "NO") print('\n'.join(result)) ```
output
1
78,605
0
157,211
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,606
0
157,212
Tags: data structures, implementation Correct Solution: ``` def main(stdin): string = next(stdin).rstrip() m = int(next(stdin)) letters = {'x': {0: 0}, 'y': {0: 0}, 'z': {0: 0}} for i, letter in enumerate(list(string), 1): letters['x'][i] = letters['x'][i - 1] letters['y'][i] = letters['y'][i - 1] letters['z'][i] = letters['z'][i - 1] letters[letter][i] += 1 while m: l, r = next(stdin).split() l, r = int(l), int(r) m -= 1 if r - l < 2: print('YES') else: letters_loc = [ letters['x'][r] - letters['x'][l-1], letters['y'][r] - letters['y'][l-1], letters['z'][r] - letters['z'][l-1] ] letters_loc.sort() if (letters_loc[2]) - letters_loc[0] <= 1: print('YES') else: print('NO') if __name__ == '__main__': import sys main(sys.stdin) ```
output
1
78,606
0
157,213
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,607
0
157,214
Tags: data structures, implementation Correct Solution: ``` input=__import__('sys').stdin.readline s = input() nn=len(s) cx=[0]*(nn+1) cy=[0]*(nn+1) cz=[0]*(nn+1) for i in range(1,nn): if s[i-1]=='x': cx[i]=cx[i-1]+1 cy[i]=cy[i-1] cz[i]=cz[i-1] elif s[i-1]=='y': cx[i]=cx[i-1] cy[i]=cy[i-1]+1 cz[i]=cz[i-1] else: cx[i]=cx[i-1] cy[i]=cy[i-1] cz[i]=cz[i-1]+1 n = int(input()) #print(cx) #print(cy) #print(cz) for i in range(n): l,r = map(int,input().split()) if r-l+1<3: print("YES") else: x = cx[r]-cx[l-1] y = cy[r]-cy[l-1] z = cz[r]-cz[l-1] # print(x,y,z) # print(s[l:r+1],cx[r],cy[r],cz[r]) if max(x,y,z)-min(x,y,z)<=1: print("YES") else: print("NO") ```
output
1
78,607
0
157,215
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,608
0
157,216
Tags: data structures, implementation Correct Solution: ``` '''input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 ''' # practicing a skill right after sleep improves it a lot quickly from sys import stdin, setrecursionlimit def pre_process(string, xarr, yarr, zarr): if string[0] == 'x': xarr[0] = 1 elif string[0] == 'y': yarr[0] = 1 else: zarr[0] = 1 for i in range(1, len(string)): if string[i] == 'x': xarr[i] += 1 elif string[i] == 'y': yarr[i] += 1 else: zarr[i] += 1 xarr[i] += xarr[i - 1] yarr[i] += yarr[i - 1] zarr[i] += zarr[i - 1] def check(l, r): if r - l + 1 < 3: return False count_x = xarr[r] count_y = yarr[r] count_z = zarr[r] if l > 0: count_x -= xarr[l - 1] count_y -= yarr[l - 1] count_z -= zarr[l - 1] mn = min(count_x, count_y, count_z) if mn < 1: return True mx = max(count_x, count_y, count_z) if mx - mn >= 2: return True return False # main starts string = list(stdin.readline().strip()) xarr = [0] * len(string) yarr = [0] * len(string) zarr = [0] * len(string) pre_process(string, xarr, yarr, zarr) m = int(stdin.readline().strip()) for _ in range(m): l, r = list(map(int, stdin.readline().split())) if check(l - 1, r - 1): print("NO") else: print("YES") ```
output
1
78,608
0
157,217
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,609
0
157,218
Tags: data structures, implementation Correct Solution: ``` def f(t): return t[2] - t[0] > 1 t = input() n, m, p = len(t), int(input()), {'x': 0, 'y': 1, 'z': 2} s = [[0] * (n + 1) for i in range(3)] for i, c in enumerate(t, 1): s[p[c]][i] = 1 for i in range(3): for j in range(1, n): s[i][j + 1] += s[i][j] a, b, c = s q, d = [map(int, input().split()) for i in range(m)], ['YES'] * m for i, (l, r) in enumerate(q): if r - l > 1 and f(sorted([a[r] - a[l - 1], b[r] - b[l - 1], c[r] - c[l - 1]])): d[i] = 'NO' print('\n'.join(d)) # Made By Mostafa_Khaled ```
output
1
78,609
0
157,219
Provide tags and a correct Python 3 solution for this coding contest problem. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
instruction
0
78,610
0
157,220
Tags: data structures, implementation Correct Solution: ``` #!/usr/bin/python3 def readln(): return tuple(map(int, input().split())) q = input() cnt = [[0] * 3 for _ in range(len(q) + 1)] for i, v in enumerate(list(q)): for j in range(3): cnt[i + 1][j] = cnt[i][j] if v == 'x': cnt[i + 1][0] += 1 elif v == 'y': cnt[i + 1][1] += 1 else: cnt[i + 1][2] += 1 ans = [] for _ in range(readln()[0]): l, r = readln() tmp = [cnt[r][i] - cnt[l - 1][i] for i in range(3)] ans.append('YES' if r - l < 2 or max(tmp) - min(tmp) < 2 else 'NO') print('\n'.join(ans)) ```
output
1
78,610
0
157,221
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. Submitted Solution: ``` def main(stdin): string = next(stdin).rstrip() m = int(next(stdin)) letters = {'x': {0: 0}, 'y': {0: 0}, 'z': {0: 0}} for i, letter in enumerate(list(string), 1): letters['x'][i] = letters['x'][i - 1] letters['y'][i] = letters['y'][i - 1] letters['z'][i] = letters['z'][i - 1] letters[letter][i] += 1 while m: l, r = next(stdin).split() l, r = int(l), int(r) m -= 1 if r - l < 2: print('YES') else: letters_loc = [ letters['x'][r] - letters['x'][l-1], letters['y'][r] - letters['y'][l-1], letters['z'][r] - letters['z'][l-1] ] letters_loc.sort() print(letters_loc) if (letters_loc[2]) - letters_loc[0] <= 1: print('YES') else: print('NO') if __name__ == '__main__': import sys main(sys.stdin) ```
instruction
0
78,611
0
157,222
No
output
1
78,611
0
157,223
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. Submitted Solution: ``` def main(stdin): line = next(stdin) m = int(next(stdin)) while m: l, r = next(stdin).split() l, r = int(l) - 1, int(r) - 1 m -= 1 slice_ = line[l:r] if len(slice_) < 3: print('YES') else: letters = {'x': 0, 'y': 0, 'z': 0} for letter in slice_: letters[letter] = 1 if all(letters.values()) and len(slice_) < 5: print('YES') else: print('NO') if __name__ == '__main__': import sys main(sys.stdin) ```
instruction
0
78,612
0
157,224
No
output
1
78,612
0
157,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. Submitted Solution: ``` s = input() n = int(input()) tests = [] for _ in range(n): a, b = map(int, input().split()) a -= 1 b -= 1 tests.append((a, b)) count_left = {} count_left[-1] = (0, 0, 0) x, y, z = 0, 0, 0 for i, c in enumerate(s): if c == 'x': x += 1 elif c == 'y': y += 1 elif c == 'z': z += 1 count_left[i] = (x, y, z) def count(a, b): x2, y2, z2 = count_left[b] x1, y1, z1 = count_left[a - 1] x = x2 - x1 y = y2 - y1 z = z2 - z1 return x,y,z def is_valid(s, a, b): x,y,z = count(a, b) diffs = map(abs, [x-y, x-z, y-z]) return not any(d > 1 for d in diffs) for a, b in tests: print("YES" if is_valid(s, a, b) else "NO") ```
instruction
0
78,613
0
157,226
No
output
1
78,613
0
157,227
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,945
0
157,890
"Correct Solution: ``` print(("Yes" if "".join(sorted(input()))=="abc" else "No")) ```
output
1
78,945
0
157,891
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,946
0
157,892
"Correct Solution: ``` s = input() print('Yes' if len(s)==len(set(s)) else 'No') ```
output
1
78,946
0
157,893
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,947
0
157,894
"Correct Solution: ``` print('Yes' if len(set(input())) ==3 else 'No') ```
output
1
78,947
0
157,895
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,948
0
157,896
"Correct Solution: ``` a = sorted(input()) a ="".join(a) print("Yes" if a =="abc" else "No") ```
output
1
78,948
0
157,897
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,949
0
157,898
"Correct Solution: ``` S = input() print('Yes' if len(set(S)) == 3 else 'No') ```
output
1
78,949
0
157,899
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,950
0
157,900
"Correct Solution: ``` print("Yes" if "".join(sorted(input())) == "abc" else "No") ```
output
1
78,950
0
157,901
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,951
0
157,902
"Correct Solution: ``` print("No" if len(set(input()))!=3 else "Yes") ```
output
1
78,951
0
157,903
Provide a correct Python 3 solution for this coding contest problem. You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`. Constraints * |S|=3 * S consists of `a`, `b` and `c`. Input Input is given from Standard Input in the following format: S Output If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`. Examples Input bac Output Yes Input bab Output No Input abc Output Yes Input aaa Output No
instruction
0
78,952
0
157,904
"Correct Solution: ``` print('Yes' if len(list(set(input())))==3 else 'No') ```
output
1
78,952
0
157,905
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,009
0
158,018
"Correct Solution: ``` S = input() for i in range(len(S)-1): if S[i] == S[i+1]: print(i+1, i+2) break elif i < len(S)-2: if S[i] == S[i+2]: print(i+1, i+3) break else: print(-1, -1) ```
output
1
79,009
0
158,019
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,010
0
158,020
"Correct Solution: ``` def main(): s = input() for i in range(len(s)-1): if s[i] == s[i+1]: print("{} {}".format(i+1,i+2)) return elif i+2<len(s) and s[i] == s[i+2]: print("{} {}".format(i+1, i+3)) return print("-1 -1") return main() ```
output
1
79,010
0
158,021
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,011
0
158,022
"Correct Solution: ``` s = input() is_unbalanced = False for i in range(len(s) - 1): if s[i] == s[i + 1]: print(i + 1, i + 2) is_unbalanced = True break if is_unbalanced == False: for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) is_unbalanced = True break if is_unbalanced == False: print(-1, -1) ```
output
1
79,011
0
158,023
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,012
0
158,024
"Correct Solution: ``` s=input() u=len(s) a=s[0] b=s[1] if(u==2 or a==b): print("1 2" if a==b else "-1 -1") exit(0) for i in range(2,u): c = s[i] if a==c: print("{} {}".format(i-1,i+1)) exit(0) if b==c: print("{} {}".format(i,i+1)) exit(0) a=b b=c print("-1 -1") ```
output
1
79,012
0
158,025
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,013
0
158,026
"Correct Solution: ``` def main(): s = input().strip() for i in range(1, len(s)): if s[i] == s[i - 1]: print(i, i + 1) break else: for i in range(2, len(s)): if s[i] == s[i - 2]: print(i - 1, i + 1) break else: print(-1, -1) if __name__ == '__main__': main() ```
output
1
79,013
0
158,027
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,014
0
158,028
"Correct Solution: ``` s=input() n=len(s) ans=[-1,-1] for i in range(n-1): if s[i]==s[i+1]: ans[0],ans[1]=i+1,i+1+1 if n>=3: for i in range(n-2): if s[i]==s[i+2]: ans[0],ans[1]=i+1,i+2+1 print(ans[0],ans[1]) ```
output
1
79,014
0
158,029
Provide a correct Python 3 solution for this coding contest problem. Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is. You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s. Constraints * 2 ≦ |s| ≦ 10^5 * s consists of lowercase letters. Input The input is given from Standard Input in the following format: s Output If there exists no unbalanced substring of s, print `-1 -1`. If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted. Examples Input needed Output 2 5 Input atcoder Output -1 -1
instruction
0
79,015
0
158,030
"Correct Solution: ``` s = input() n = len(s) for i in range(n-1): if i < n-2: if s[i] == s[i+1]: print(i+1,i+2) break elif s[i] == s[i+2]: print(i+1,i+3) break else: continue else: if s[i] == s[i+1]: print(i+1,i+2) else: print(-1,-1) ```
output
1
79,015
0
158,031