message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw"
instruction
0
41,455
0
82,910
Tags: dp, greedy, implementation, strings Correct Solution: ``` s = input() cs = [''] ans = [] last = [''] poped = False for c in s[::-1]: if not poped and c == cs[-1] and c > last[-2]: cs.pop() if (not cs) or c != cs[-1]: last.pop() poped = True else: poped = False if c != cs[-1]: last.append(c) cs.append(c) if len(cs) <= 11: ans.append((len(cs) - 1, ''.join(cs[::-1]))) else: ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5:]))[::-1])) for a, b in ans[::-1]: print(a, b) ```
output
1
41,455
0
82,911
Provide tags and a correct Python 3 solution for this coding contest problem. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw"
instruction
0
41,456
0
82,912
Tags: dp, greedy, implementation, strings Correct Solution: ``` import os import sys import io import functools import copy import math # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # 神奇快读,无法运行调试 GANS = [] # def print(*args): # 神奇快写,最后得写上os.write # global GANS # for i in args: # GANS.append(f'{i}'.encode()) def cmp(x,y): if x[1]==y[1]: return -(y[0]-x[0]) return -(x[1]-y[1]) s = input() ans = [] # s = s[::-1] cur = [] ctr = [] pos = [] pv = [chr(0)] pvc = chr(1) for p,i in enumerate(s[::-1]): if cur and cur[-1] == i: f = True if len(cur)>=2: if cur[-2] > i: f = False elif cur[-2]==i: if pos[-2]==p-2: f=False if pv[-1]>=i: f = False # elif len(cur)>=3: # if cur[-3]>=i: # f = False if f and p==pos[-1]+1: cur.pop() pos.pop() pv.pop() pvc = pv[-1] else: cur.append(i) pos.append(p) pv.append(pvc) else: if cur:pvc = cur[-1] cur.append(i) pos.append(p) pv.append(pvc) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1])+'...'+''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i-1],ans[-i-1]) # print() # print(len(i),end=' ') # if len(i)>10: # print(i[:5]+'...'+i[-2:]) # else: # print(i) ```
output
1
41,456
0
82,913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` s = input().strip();N = len(s) if len(s) == 1:print(1, s[0]);exit() X = [s[-1], s[-2]+s[-1] if s[-2]!=s[-1] else ""];Y = [1, 2 if s[-2]!=s[-1] else 0] for i in range(N-3, -1, -1): c = s[i];k1 = c+X[-1];ng = Y[-1]+1 if ng > 10:k1 = k1[:5] + "..." + k1[-2:] if c == s[i+1] and k1 > X[-2]:k1 = X[-2];ng = Y[-2] X.append(k1);Y.append(ng) for i in range(N-1, -1, -1):print(Y[i], X[i]) ```
instruction
0
41,457
0
82,914
Yes
output
1
41,457
0
82,915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def print_list(l): print(' '.join(map(str,l))) # import heapq as hq # import bisect as bs # from collections import deque as dq # from collections import defaultdict as dc # from math import ceil,floor,sqrt # from collections import Counter s = input() n = len(s) res = ['',s[-1]] l = [1] flag = True last = [''] for i in range(n-2,-1,-1): tmp = res[-1] if flag and s[i]==tmp[0] and s[i]>last[-1]: res.append(res[-2]) l.append(l[-1]-1) flag = False if l[-1]>0 and res[-1][0]!=s[i]: last.pop() else: l.append(l[-1]+1) tmp = s[i]+tmp flag = True if len(tmp)>10: res.append(tmp[:5]+'...'+tmp[-2:]) else: res.append(tmp) if len(res[-1])>1 and res[-1][0]!=res[-1][1]: last.append(res[-1][1]) res.pop(0) for i in range(n-1,-1,-1): print(l[i],res[i]) ```
instruction
0
41,458
0
82,916
Yes
output
1
41,458
0
82,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` s = input().strip();N = len(s) if len(s) == 1:print(1, s[0]);exit() X = [s[-1], s[-2]+s[-1] if s[-2]!=s[-1] else ""];Y = [1, 2 if s[-2]!=s[-1] else 0] for i in range(N-3, -1, -1): c = s[i];k1 = c+X[-1];ng = Y[-1]+1 if ng > 10:k1 = k1[:5] + "..." + k1[-2:] if c == s[i+1] and k1 > X[-2]:k1 = X[-2];ng = Y[-2] X.append(k1);Y.append(ng) for i in range(N-1, -1, -1):print(Y[i], X[i]) #miau ```
instruction
0
41,459
0
82,918
Yes
output
1
41,459
0
82,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): s = input() curs = [] n = len(s) ans = [] ansNum = [] blocked = False hint = 'A' for i in range(n-1,-1,-1): elem = s[i] if not curs: curs.append(elem) blocked = False elif elem == curs[-1] and (len(curs) <= 1 or ord(curs[-2]) < ord(elem)) and not blocked: curs.pop() blocked = True elif elem == curs[-1] and (len(curs) <= 1 or ord(curs[-2]) == ord(elem)) and not blocked: if ord(hint) < ord(elem): curs.pop() blocked = True else: curs.append(elem) blocked = False else: if ord(curs[-1]) == ord(elem): if len(curs) >= 2 and curs[-2] != elem: hint = curs[-2] elif len(curs) >= 2: pass else: hint = 'A' curs.append(elem) blocked = False ansNum.append(len(curs)) if ansNum[-1] > 10: ans.append(curs[-1] + curs[-2] + curs[-3] + curs[-4] + curs[-5] + "..." + curs[1] + curs[0]) else: ans.append("".join(reversed(curs))) for i in range(n-1,-1,-1): print(str(ansNum[i]) + " " + ans[i]) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
41,460
0
82,920
Yes
output
1
41,460
0
82,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` import io import os from collections import deque def solve(S): N = len(S) ans = [] suff = deque() curr = None last = None count = 0 for i in reversed(range(N)): x = S[i] if x != curr: count = 1 last = suff[0] if suff else None curr = x else: count += 1 if last is not None and curr < last: # Want to keep all repeats of this character suff.appendleft(S[i]) else: # Want to keep as few repeats of this character as possible if count % 2 == 1: suff.appendleft(S[i]) else: suff.popleft() if len(suff) <= 10: ans.append(str(len(suff)) + " " + "".join(suff)) else: line = [str(len(suff)), " "] for i in range(5): line.append(suff[i]) line.append("...") line.append(suff[-2]) line.append(suff[-1]) ans.append("".join(line)) return "\n".join(ans[::-1]) if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline S = input().decode().rstrip() ans = solve(S) print(ans) ```
instruction
0
41,461
0
82,922
No
output
1
41,461
0
82,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` import os import sys import io import functools import copy import math # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # 神奇快读,无法运行调试 GANS = [] # def print(*args): # 神奇快写,最后得写上os.write # global GANS # for i in args: # GANS.append(f'{i}'.encode()) def cmp(x,y): if x[1]==y[1]: return -(y[0]-x[0]) return -(x[1]-y[1]) s = input() ans = [] # s = s[::-1] cur = [] ctr = [] for i in s[::-1]: if cur and cur[-1] == i: f = True if len(cur)>=2: if cur[-2] >= i: f=False if f: cur.pop() else: cur.append(i) else: cur.append(i) ctr.append(len(cur)) if ctr[-1] > 10: ans.append(''.join(cur[-5:][::-1])+'...'+''.join(cur[:2][::-1])) else: ans.append(''.join(cur[::-1])) for i in range(len(ans)): print(ctr[-i-1],ans[-i-1]) # print() # print(len(i),end=' ') # if len(i)>10: # print(i[:5]+'...'+i[-2:]) # else: # print(i) ```
instruction
0
41,462
0
82,924
No
output
1
41,462
0
82,925
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` import io import os from collections import deque def solve(S): N = len(S) ans = [] suff = deque() last = None count = 0 for i in reversed(range(N)): x = S[i] if i == N - 1 or S[i] != S[i + 1]: count = 1 last = suff[0] if suff else None else: count += 1 if last is not None and x <= last: # Want to keep all repeats of this character suff.appendleft(S[i]) else: # Want to keep as few repeats of this character as possible if count % 2 == 1: suff.appendleft(S[i]) else: suff.popleft() if len(suff) <= 10: ans.append(str(len(suff)) + " " + "".join(suff)) else: line = [str(len(suff)), " "] for i in range(5): line.append(suff[i]) line.append("...") line.append(suff[-2]) line.append(suff[-1]) ans.append("".join(line)) return "\n".join(ans[::-1]) if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline S = input().decode().rstrip() ans = solve(S) print(ans) ```
instruction
0
41,463
0
82,926
No
output
1
41,463
0
82,927
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions (i, i + 1) in such a way that the following conditions are satisfied: * for each pair (i, i + 1) the inequality 0 ≤ i < |s| - 1 holds; * for each pair (i, i + 1) the equality s_i = s_{i + 1} holds; * there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string s (1 ≤ |s| ≤ 10^5) — the initial string consisting of lowercase English letters only. Output In |s| lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than 10 characters, instead print the first 5 characters, then "...", then the last 2 characters of the answer. Examples Input abcdd Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. * The longest suffix is the whole string "abcdd". Choosing one pair (4, 5), Lesha obtains "abc". * The next longest suffix is "bcdd". Choosing one pair (3, 4), we obtain "bc". * The next longest suffix is "cdd". Choosing one pair (2, 3), we obtain "c". * The next longest suffix is "dd". Choosing one pair (1, 2), we obtain "" (an empty string). * The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs (11, 12), (16, 17), (23, 24) and we obtain "abbcdddeaadfortytw" Submitted Solution: ``` import sys s = input().strip() N = len(s) if len(s) == 1: print(1, s[0]) sys.exit() X = [s[-1], s[-2]+s[-1] if s[-2]!=s[-1] else ""] Y = [1, 2 if s[-2]!=s[-1] else 0] for i in range(N-3, -1, -1): c = s[i] k1 = c+X[-1] ng = Y[-1]+1 if c == s[i+1] and k1 > X[-1]: k1 = X[-2] ng = Y[-2] elif ng >= 10: k1 = k1[:5] + "..." + k1[-2:] X.append(k1) Y.append(ng) for i in range(N-1, -1, -1): print(Y[i], X[i]) ```
instruction
0
41,464
0
82,928
No
output
1
41,464
0
82,929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You should process m queries over a set D of strings. Each query is one of three kinds: 1. Add a string s to the set D. It is guaranteed that the string s was not added before. 2. Delete a string s from the set D. It is guaranteed that the string s is in the set D. 3. For the given string s find the number of occurrences of the strings from the set D. If some string p from D has several occurrences in s you should count all of them. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. Input The first line contains integer m (1 ≤ m ≤ 3·105) — the number of queries. Each of the next m lines contains integer t (1 ≤ t ≤ 3) and nonempty string s — the kind of the query and the string to process. All strings consist of only lowercase English letters. The sum of lengths of all strings in the input will not exceed 3·105. Output For each query of the third kind print the only integer c — the desired number of occurrences in the string s. Examples Input 5 1 abc 3 abcabc 2 abc 1 aba 3 abababc Output 2 2 Input 10 1 abc 1 bcd 1 abcd 3 abcd 2 abcd 3 abcd 2 bcd 3 abcd 2 abc 3 abcd Output 3 2 1 0 Submitted Solution: ``` def chet(chto,gde): nom = 1 b = -1 vhoj = 0 dlin = len(gde) while nom<=dlin: b = gde.find(chto) if b==0 and gde !="": gde = gde[b+1:] vhoj +=1 elif b>0: gde = gde[b+1:] vhoj +=1 nom +=1 return vhoj m = int(input()) k = 1 d = "" res = [] stroka = "" while k<=m: st = input() zapros = int(st[0]) stroka = st[2:] if zapros == 1: d = d + stroka print(d) if zapros == 2: d = d.replace(stroka,"") print(d) if zapros == 3: res.append(chet(d,stroka)) k+=1 kl = 0 while kl <len(res): print(res[kl]) kl +=1 ```
instruction
0
41,689
0
83,378
No
output
1
41,689
0
83,379
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,736
0
83,472
Tags: brute force, implementation, strings Correct Solution: ``` lens = input().split() s1 = input() s2 = input() min_count = int(lens[0]) min_answer = list() for start in range(int(lens[1]) - int(lens[0]) + 1): diff = 0 answer = list() for i in range(int(lens[0])): if (s1[i] != s2[start+i]): diff += 1 answer.append(str(i+1)) if diff <= min_count: min_count = diff min_answer = answer print(min_count) if min_count > 0: print(' '.join(min_answer)) ```
output
1
41,736
0
83,473
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,737
0
83,474
Tags: brute force, implementation, strings Correct Solution: ``` I = lambda: map(int, input().split()) n, m = I() s, t = input(), input() res = 0 res_details = [] for i in range(m-n+1): #print(t[i:i+n]) eq = 0 detail = [] for j in range(i,i+n): if not (ord(s[j-i]) - ord(t[j])): eq += 1 else: detail.append(j-i+1) if eq >= res: res = eq res_details = list(detail) print(n-res) print(*res_details) ```
output
1
41,737
0
83,475
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,738
0
83,476
Tags: brute force, implementation, strings Correct Solution: ``` n, m = map(int, input().split()) a = input() b = input() L = len(a) res1 = [] res2 = 9999999999 for i in range(0, m - n + 1): ia = iter(a) ib = iter(b[i:i + L]) ans1 = [] ans2 = 0 try: j = 1 while True: if next(ia) != next(ib): ans1.append(j) ans2 += 1 j += 1 except StopIteration: if ans2 < res2: res1 = ans1 res2 = ans2 print(res2) print(" ".join(str(i) for i in res1)) ```
output
1
41,738
0
83,477
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,739
0
83,478
Tags: brute force, implementation, strings Correct Solution: ``` I = lambda: list(map(int, input().split())) n, m = I() s = input() t = input() def diff(s, t, tpos): ans = [] for e, [i, j] in enumerate(zip(s, t[tpos:][:len(s)])): if i!='?' and i!=j: ans.append(e + 1) return ans repl = [diff(s, t, i) for i in range(m-n+1)] repl.sort(key=lambda x: len(x)) print(len(repl[0])) print(" ".join(map(str, repl[0]))) ```
output
1
41,739
0
83,479
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,740
0
83,480
Tags: brute force, implementation, strings Correct Solution: ``` import sys def main(): n,m = map(int,sys.stdin.readline().split()) s = sys.stdin.readline().rstrip() t = sys.stdin.readline().rstrip() r = 10000 res = [] for i in range(m-n+1): p = [] c = 0 for j in range(n): if s[j]!= t[i+j]: c+=1 p.append(j+1) if c < r: r = c res = p print(r) if r!=0: print(" ".join(map(str,res))) main() ```
output
1
41,740
0
83,481
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,741
0
83,482
Tags: brute force, implementation, strings Correct Solution: ``` n, m = input().split() s = input() t = input() n = int(n) m = int(m) k = n + 1 pos = [] aux = [] for i in range(m - n + 1): x = n aux=[] for j in range(len(s)): if(t[i + j] == s[j]): x -= 1 else: aux.append(str(j+1)) j += 1 if(x < k): k = x pos = aux print(k) print(' '.join(pos)) ```
output
1
41,741
0
83,483
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,742
0
83,484
Tags: brute force, implementation, strings Correct Solution: ``` n, m = map(int, input().split()) s = input() t = input() ans = [i for i in range(1, n + 1)] for i in range(m - n + 1): pos = [] for j in range(n): if s[j] != t[i + j]: pos.append(j + 1) if len(pos) < len(ans): ans = pos print(len(ans)) print(' '.join(map(str, ans))) ```
output
1
41,742
0
83,485
Provide tags and a correct Python 3 solution for this coding contest problem. Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t. Output In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. Examples Input 3 5 abc xaybz Output 2 2 3 Input 4 10 abcd ebceabazcd Output 1 2
instruction
0
41,743
0
83,486
Tags: brute force, implementation, strings Correct Solution: ``` a,b=map(int,input().split()) c=input() d=input() maxi=a ko=[] for i in range(b-a+1): premax=0 po=[] for j in range(a): if c[j]!=d[j+i]: premax+=1 po.append("%s"%(j+1)) if premax<=maxi: maxi=premax ko=po print(maxi) print(" ".join(ko)) ```
output
1
41,743
0
83,487
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
42,157
0
84,314
Tags: dp, strings Correct Solution: ``` n, a, b = map(int, input().split()) s = input() lcp = [[0]*n for _ in ' '*n] for i in range(n-1, -1, -1): for r in range(n-1, -1, -1): if s[i] == s[r]: if i == n - 1 or r == n - 1: lcp[i][r] = 1 else: lcp[i][r] = lcp[i+1][r+1]+1 d = [10**10]*(n+1) d[0] = 0 for i in range(n): d[i+1] = min(d[i+1], d[i]+a) for j in range(i): k = min(lcp[i][j], i-j) d[i+k] = min(d[i+k], d[i]+b) print(d[-1]) ```
output
1
42,157
0
84,315
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
42,158
0
84,316
Tags: dp, strings Correct Solution: ``` import collections # The first line contains three positive integers, separated by spaces: # n, a and b (1≤n,a,b≤5000) — the length of the string, the cost to compress a one-character string # and the cost to compress a string that appeared before. n, a, b = map(int, input().split()) # The second line contains a single string s, consisting of n lowercase English letters. # s = collections.deque(input()) # s.appendleft(0) s = " " + input() # maxn = 5010 maxn = n + 1 # dp[0]=0, # dp[p]=min(a+dp[p−1], # min(b+dp[k−1] where s[k,p] is a substring of s[1,k−1])) dp = [0x3f for _ in range(maxn)] v = [[0 for _c in range(maxn)] for _r in range(maxn)] Next = [0 for _n in range(maxn)] # for (int i = 1; i <= n; i++) # for (int j = i; j <= n; j++) { for i in range(1, n+1): for j in range(i, n+1): if s[i] == s[j]: v[i][j] = v[i - 1][j - 1] + 1 # for row in v: print(row) dp[0] = 0 for i in range(1, n+1): dp[i] = dp[i - 1] + a for j in range(1, i): t = min(i - j, v[j][i]) if t : dp[i] = min(dp[i], dp[i - t] + b) print(dp[n]) ```
output
1
42,158
0
84,317
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
42,159
0
84,318
Tags: dp, strings Correct Solution: ``` def f(x): z=[0 for i in range(len(x))] l=0 r=0 for i in range(1,len(x)): if i<=r: z[i]=min(z[i-l],r-i+1) while i+z[i]<len(x) and x[z[i]]==x[i+z[i]]: z[i]+=1 if i+z[i]-1>r: l,r=i,i+z[i]-1 return z a=list(map(int,input('').split())) n,a,b=a[0],a[1],a[2] s=input('') dp=[0 for i in range(n)] dp[0]=a for i in range(1,n): t=s[:i+1] dp[i]=dp[i-1]+a q=f(t[::-1]) maxs=[0 for j in range(i+1)] maxs[0]=q[i] for j in range(1,i): maxs[j]=max(maxs[j-1],q[i-j]) for j in range(i): if maxs[j]>=i-j: dp[i]=min(dp[i],dp[j]+b) print(dp[len(dp)-1]) ```
output
1
42,159
0
84,319
Provide tags and a correct Python 3 solution for this coding contest problem. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
instruction
0
42,160
0
84,320
Tags: dp, strings Correct Solution: ``` import sys sys.setrecursionlimit(10 ** 8) n, a, b = map(int,input().split()) s = input() def calc(j): if (j >= n): return 0 if (dp[j] != -1): return dp[j] dp[j] = a + calc(j + 1) lo = j hi = n farthest = -1 # finding best string to reach from a given j such that it eist before i using binary search while (lo < hi): mid = (lo + hi) // 2 if (s[j:mid + 1] in s[0:j]): farthest = mid lo = mid + 1 else: hi = mid if (farthest != -1): dp[j] = min(dp[j], b + calc(farthest + 1)) return dp[j] dp = [-1 for i in range(n + 5)] print(calc(0)) ```
output
1
42,160
0
84,321
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}. Submitted Solution: ``` n,a,b=map(int,input().split()) st=input() ans=a i=1 while i<n: t=False for k in range(i,0,-1): if st[i:i+k] in st[:i]: ans+=b i+=k t=True break if not t:ans+=a;i+=1 print(ans) ```
instruction
0
42,161
0
84,322
No
output
1
42,161
0
84,323
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}. Submitted Solution: ``` n,a,b=map(int,input().split()) num = input() ans=0 i = n-1 while i>=0: if num[i] in num[:i]: temp=i while num[temp:i+1] in num[:temp]: temp-=1 i = temp ans+=b else: ans+=a i-=1 print(ans) ```
instruction
0
42,162
0
84,324
No
output
1
42,162
0
84,325
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}. Submitted Solution: ``` a1=list(map(int,input().split())) a2=input() n=a1[0] a=a1[1] b=a1[2] str="" cost=0 i=0 breaker=False while i<n: if breaker and i<n-1: i+=1 elif breaker: break temp=i if a2[temp] in str: temp+=1 while a2[i:temp] in str and temp<=n: temp+=1 str+=a2[i:temp-1] if i==temp-1: breaker=True cost+=b i=temp-1 else: str+=a2[i] cost+=a i+=1 print(cost) ```
instruction
0
42,163
0
84,326
No
output
1
42,163
0
84,327
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins. To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways: * if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins; * if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins. A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s. Input The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before. The second line contains a single string s, consisting of n lowercase English letters. Output Output a single integer — the smallest possible number of coins you need to spend to compress s. Examples Input 3 3 1 aba Output 7 Input 4 1 1 abcd Output 4 Input 4 10 1 aaaa Output 12 Note In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}. In the second sample, you just need to compress every character by itself. In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}. Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = SR() return l mod = 1000000007 #A """ a,b,c = LI() print(min(b//a,c)) """ #B """ a,b,k = LI() i = 1 ans = [1] while i <= min(a,b): i += 1 if a%i == 0 and b%i == 0: ans.append(i) print(ans[-k]) """ #C """ s = list(map(int,S())) print(2*min(sum(s),len(s)-sum(s))) """ #D """ def root(x): if par[x] == x: return x par[x] = root(par[x]) return par[x] def unite(x,y): x = root(x) y = root(y) if x == y: ans.append(ans[-1]) return sx = s[x] sy = s[y] if rank[x] < rank[y]: par[x] = y s[y] += sx else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 s[x] += sy ans.append(ans[-1]+sx*sy) n,m = LI() v = LIR(m) v = v[::-1] par = [i for i in range(n)] rank = [0 for i in range(n)] s = [1 for i in range(n)] ans = [0] k = n*(n-1)//2 v.pop(-1) for a,b in v: if ans[0] == k: ans.append(k) continue a -= 1 b -= 1 unite(a,b) for i in ans[::-1]: print(k-i) """ #E n,a,b = LI() s = input() ans = 0 i = 0 while i < n: j = i+1 if s[i:j] in s[:i]: while s[i:j] in s[:i]: j += 1 if j >= n-1:break ans += min(b,a*(j-i)) else: ans += a*(j-i) i = j print(ans) #F #G #H #I #J #K #L #M #N #O #P #Q #R #S #T ```
instruction
0
42,164
0
84,328
No
output
1
42,164
0
84,329
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,237
0
84,474
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` t=int(input()) while t: t=t-1 s=input() se=set(s) if len(se)<3: print(0) continue a1=-1 a2=-1 a3=-1 out=len(s) for i in range(len(s)): if s[i]=='1': a1=i elif s[i]=='2': a2=i elif s[i]=='3': a3=i if a1!=-1 and a2!=-1 and a3!=-1: out=min(out,max(a1,max(a2,a3))-min(a1,min(a2,a3))+1) print(out) ```
output
1
42,237
0
84,475
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,238
0
84,476
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` t = int(input()) for i1 in range(t): s = [int(i) for i in input()] pointer = [0, 0] advance = 1 result = len(s) + 1 if len(set(s)) < 3: print(0) continue c = [0, 0, 0, 0] c[s[0]] += 1 while True: if c[1] > 0 and c[2] > 0 and c[3] > 0: if result > pointer[1] - pointer[0] + 1: result = pointer[1] - pointer[0] + 1 advance = 0 else: advance = 1 if advance == 1 and pointer[1] >= len(s) - 1: break if advance == 1: pointer[1] += 1 c[s[pointer[1]]] += 1 else: c[s[pointer[0]]] -= 1 pointer[0] += 1 print(result) ```
output
1
42,238
0
84,477
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,239
0
84,478
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` tes=int(input()) def check(arr): for i in arr: if i<=0: return False return True for q in range(tes): a=input() s={'1':0,'2':0,'3':0} left=right=0 for c,i in enumerate(a): s[i]+=1 if 0 not in s.values(): right=c break if right-left<1: print(0) else: m=right-left+1 s[a[left]]-=1 while(right<len(a)): while check(s.values()): # print(s.values()) s[a[left+1]]-=1 left+=1 m=min(m,right-left+1) right+=1 if right<len(a): s[a[right]]+=1 print(m) ```
output
1
42,239
0
84,479
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,240
0
84,480
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` import math as m t=int(input()) for _ in range(t): a=input() n=len(a) b=[] i=-1 j=-1 k=-1 for l in range(n): if a[l]=="1": i=l elif a[l]=="2": j=l elif a[l]=="3": k=l d=[i,j,k] if -1 not in d: b.append(max(d)-min(d)+1) if len(b)==0: print(0) else: print(min(b)) ```
output
1
42,240
0
84,481
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,241
0
84,482
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` T = int(input()) def findMin(s): k = 3 n = len(s) st = 0 end = 0 cnt = {'1':0, '2':0, '3': 0} di = 0 currlen =0 minlen = n startInd = -1 while (end < n): cnt[s[end]] += 1 if (cnt[s[end]] == 1): di += 1 if (di > k): while (st < end and di > k): if (cnt[s[st]] == 1): di -= 1 cnt[s[st]] -= 1 st += 1 if (di == k): while (st < end and cnt[s[st]] > 1): cnt[s[st]] -= 1 st += 1 currlen = end - st + 1 if (currlen < minlen): minlen = currlen startInd = st end += 1 if di != k: return 0 return minlen for t in range(T): string = input() print(findMin(string)) ```
output
1
42,241
0
84,483
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,242
0
84,484
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` def find(arr, ind): s = 0 e = len(arr)-1 ans = -1 while s <= e: # print(s, e) mid = (s+e)//2 if arr[mid] > ind: ans = arr[mid] e = mid-1 else: s = mid+1 return ans t = int(input()) for _ in range(t): s = input() a, b, c = [], [], [] for i in range(len(s)): if s[i] == '1': a += [i] elif s[i] == '2': b += [i] else: c += [i] ans = 9999999999 # print(a, b, c) for i in range(len(s)): if s[i] == '1': x = find(b, i) y = find(c, i) if s[i] == '2': x = find(a, i) y = find(c, i) if s[i] == '3': x = find(a, i) y = find(b, i) # print(i, x, y, max(x, y), max(x, y)-i+1) if x == -1 or y == -1: continue ans = min(ans, max(x, y)-i+1) if ans == 9999999999: print(0) else: print(ans) ```
output
1
42,242
0
84,485
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,243
0
84,486
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` t=int(input()) for i in range(t): s=input() a=-1 b=-1 c=-1 ans=len(s) cur=[] if s.count('1')==0 or s.count('2')==0 or s.count('3')==0: print(0) else: for i in range(len(s)): if s[i]=='1': a=i elif s[i]=='2': b=i else: c=i cur=[a,b,c] if min(cur)>=0: ans=min(ans,max(cur)-min(cur)+1) print(ans) ```
output
1
42,243
0
84,487
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used.
instruction
0
42,244
0
84,488
Tags: binary search, dp, implementation, two pointers Correct Solution: ``` from sys import stdin,stdout from collections import Counter from itertools import permutations import bisect import math I=lambda: map(int,stdin.readline().split()) I1=lambda: stdin.readline() #(a/b)%m =((a%m)*pow(b,m-2)%m)%m for _ in range(int(I1())): s=I1().strip() i=1 a=[] n=len(s) c=0 while i<n: if s[i]==s[i-1]: c+=1 else: a.append(list([s[i-1],c+1])) c=0 i+=1 a.append(list([s[i-1],c+1])) l=len(a) i=0 m=2000000 #print(a) while i<l-2: if a[i][0]!=a[i+1][0] and a[i+1][0]!=a[i+2][0] and a[i][0]!=a[i+2][0]: m=min(m,2+a[i+1][1]) i+=1 if m==2000000: print(0) else: print(m) ```
output
1
42,244
0
84,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` import sys import math def I(): return int(sys.stdin.readline()) def IL(): return list(map(int,sys.stdin.readline().strip().split())) t = I() for tt in range(t): s = sys.stdin.readline().strip() st = 0;en=0 d = {1:0,2:0,3:0} d[int(s[st])]+=1 res = 0 while(en<len(s)): # print(st,en,d) if st <= en and d[1]>0 and d[2]>0 and d[3] >0: if res == 0 : res = en-st+1 else : res = min(res,en-st+1) d[int(s[st])]-=1 # print("++",st,en,d) st+=1 else : en+=1 if en < len(s) : d[int(s[en])]+=1 # print("--",st,en,d) if st <= en and d[1]>0 and d[2]>0 and d[3] >0: if res == 0 : res = en-st+1 else : res = min(res,en-st+1) d[int(s[st])]-=1 st+=1 print(res) ```
instruction
0
42,245
0
84,490
Yes
output
1
42,245
0
84,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` for _ in range(int(input())): s=input();n=len(s);p,q,r=-1,-1,-1;ans=n+1 for i in range(n): if s[i]=="1":p=i elif s[i]=="2":q=i else:r=i if p>=0 and q>=0 and r>=0: mi=min(p,q,r) ma=max(p,q,r) ans=min(ans,ma-mi+1) if ans==n+1: print(0) else: print(ans) ```
instruction
0
42,246
0
84,492
Yes
output
1
42,246
0
84,493
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` for i in range(int(input())): s=input() if len(s)<3: print(0) continue d=[] l=1 seen=set() for i in range(len(s)-1): seen.add(s[i]) if s[i]!=s[i+1]: d.append((s[i],l)) l=1 else: l+=1 d.append((s[i+1],l)) seen.add(s[i+1]) if len(seen)!=3: print(0) continue ml=len(s) for i in range(1,len(d)-1): if len(set((d[i][0],d[i-1][0],d[i+1][0])))==3 and d[i][1]+2<ml: ml=d[i][1]+2 print(ml) ```
instruction
0
42,247
0
84,494
Yes
output
1
42,247
0
84,495
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` def solve(s): i, j = 0, 0 n = len(s) cnt = n+1 while i < n: while i+1 < n and s[i] == s[i+1]: i += 1 j = i+1 while j+1 < n and s[j] == s[j+1]: j += 1 if j+1 >= n: break # print(i, j) if s[i] != s[j+1]: cnt = min(cnt, j-i+2) i = j cnt = cnt if cnt != n+1 else 0 return cnt def main(): inp = lambda: [int(x) for x in input().split()] tc = int(input()) for _ in range(tc): s = input() print(solve(s)) if __name__ == '__main__': main() ```
instruction
0
42,248
0
84,496
Yes
output
1
42,248
0
84,497
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write mod=10**9+7 def ni(): return int(raw_input()) def li(): return map(int,raw_input().split()) def pn(n): stdout.write(str(n)+'\n') def pa(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code n=200000 pre=[[-1 for i in range(3)] for j in range(n)] suf=[[-1 for i in range(3)] for j in range(n)] for t in range(ni()): s=map(int,raw_input().strip()) n=len(s) for i in range(n): for j in range(3): pre[i][j]=-1 suf[i][j]=-1 ans=10**9 pre[0][s[0]-1]=0 for i in range(1,n): for j in range(3): pre[i][j]=pre[i-1][j] pre[i][s[i]-1]=i suf[n-1][s[n-1]-1]=n-1 for i in range(n-2,-1,-1): for j in range(3): suf[i][j]=suf[i+1][j] suf[i][s[i]-1]=i for i in range(n): p1=(s[i]-2)%3 p2=(s[i])%3 temp=10**9 #print i,p1,p2 if pre[i][p1]>=0 and pre[i][p2]>=0: #print 1,i temp=min(temp,i-min(pre[i][p1],pre[i][p2])+1) if suf[i][p1]>=0 and suf[i][p2]>=0: #print 2,i temp=min(temp,-i+max(suf[i][p1],suf[i][p2])+1) if pre[i][p1]>=0 and suf[i][p2]>=0: #print 3,i temp=min (temp,suf[i][p2]-pre[i][p1]+1) if suf[i][p1]>=0 and pre[i][p2]>=0: #print 4,i temp=min(temp,suf[i][p1]-pre[i][p2]+1) ans=min(ans,temp) if ans==10**9: pn(0) else: pn(ans) ```
instruction
0
42,249
0
84,498
Yes
output
1
42,249
0
84,499
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` def solve(): n = input() l = list(n) ans = float('inf') check = [-1 , 0, 0, 0] for i in range(len(l)): check[int(l[i])]+=1 if check[1] == 0 or check[2] ==0 or check[3] == 0: print(0) elif check[1] == 1 and check[2] == 1 and check[3] == 1: print(3) else: i, j = 0, 0 count = [-1, 0, 0, 0] while i < len(l) and j < len(l): if count[1] > 0 and count[2] > 0 and count[3] > 0: ans = min(ans, j-i) count[int(l[i])] -= 1 i += 1 elif count[1] == 0 or count[2] == 0 or count[3] == 0: count[int(l[j])] += 1 j += 1 print(ans) for _ in range(int(input())): solve() ```
instruction
0
42,250
0
84,500
No
output
1
42,250
0
84,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` def check(s): n = len(s) ss = s res = 0 for a in range(0,n-1): for b in range (a,n): if '1' in s[a:b+1] and '2' in s[a:b+1] and '3' in s[a:b+1] : s1 =s[a:b+1] if len(s1)==3: res= 1 print('3') return if len(s1)<len(ss): res=1 ss = s1 if res ==1: print(len(ss)) if res ==0: print('0') s = str(input()) check(s) ```
instruction
0
42,251
0
84,502
No
output
1
42,251
0
84,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` import atexit import io import sys from collections import defaultdict _INPUT_LINES = sys.stdin.read().splitlines() input = iter(_INPUT_LINES).__next__ _OUTPUT_BUFFER = io.StringIO() sys.stdout = _OUTPUT_BUFFER @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) def substring_triplet(in_str): first = 0 last = 0 set_substr = set(in_str[first]) min_substr_len = len(in_str) once = False for i in range(1, len(in_str)): set_substr.add(in_str[i]) last = i if last - first > 0: if in_str[first] == in_str[last]: if last - first > 1: first = last - 1 else: first = last set_substr = set(in_str[first: last + 1]) if len(set_substr) == 3: once = True min_substr_len = min(min_substr_len, last - first + 1) fist = last - 2 return min_substr_len if once else 0 iters = int(input()) for i in range(iters): in_str = input() print(substring_triplet(in_str)) ```
instruction
0
42,252
0
84,504
No
output
1
42,252
0
84,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s. Input The first line contains one integer t (1 ≤ t ≤ 20000) — the number of test cases. Each test case consists of one line containing the string s (1 ≤ |s| ≤ 200000). It is guaranteed that each character of s is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed 200000. Output For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead. Example Input 7 123 12222133333332 112233 332211 12121212 333333 31121 Output 3 3 4 4 0 0 4 Note Consider the example test: In the first test case, the substring 123 can be used. In the second test case, the substring 213 can be used. In the third test case, the substring 1223 can be used. In the fourth test case, the substring 3221 can be used. In the fifth test case, there is no character 3 in s. In the sixth test case, there is no character 1 in s. In the seventh test case, the substring 3112 can be used. Submitted Solution: ``` from collections import defaultdict test = int(input()) while test: test -= 1 s = input() l = len(s) shortes = 200000 i, j = 0, 0 seen = defaultdict(int) while j < l: seen[s[j]] += 1 if seen['1'] >= 1 and seen['2'] >= 1 and seen['3'] >= 1: shortes = min(shortes, j-i + 1) while i < j-1: seen[s[i]] -= 1 if seen[s[i]] >= 1: shortes -= 1 else: break i += 1 j += 1 if shortes == 200000: print(0) continue print(shortes) ```
instruction
0
42,253
0
84,506
No
output
1
42,253
0
84,507
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,261
0
84,522
Tags: greedy, strings Correct Solution: ``` t = int(input()) for _ in range(t): n = int(input()) lst = [] for i in range(n): lst.append(input()) cnt = [0 for i in range(27)] # print(lst) for i in range(len(lst)): s1 = lst[i] for j in range(len(s1)): # print("hey",ord(s1[j]) - ord('a')) cnt[ord(s1[j]) - ord('a')] += 1 # if n == 4: # print(cnt) flag = True for i in range(len(cnt)): if(cnt[i] % n != 0): # if n==4: # print(cnt[i]) flag = False break if flag != False: print("YES") else: print("NO") # print(cnt) ```
output
1
42,261
0
84,523
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,262
0
84,524
Tags: greedy, strings Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) for tt in range(t): n = int(input()) count = [0 for i in range(1000)] for i in range(n): s = input() for i in range(len(s)): count[ord(s[i])] += 1 flag = True for i in range(len(count)): if count[i] % n != 0: flag = False break if flag: print("YES") else: print("NO") ```
output
1
42,262
0
84,525
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,263
0
84,526
Tags: greedy, strings Correct Solution: ``` n = int(input()) def helper(m): arr = [0] * 26 for i in range(m): my_str = input() for tmp in my_str: arr[ord(tmp) - 97] += 1 for i in range(26): if (arr[i] % m != 0): return "NO" return "YES" for _ in range(n): m = int(input()) print(helper(m)) ```
output
1
42,263
0
84,527
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,264
0
84,528
Tags: greedy, strings Correct Solution: ``` for _ in range(int(input())): n = int(input()) dic = {} for sentence in range(n): s = list(input()) for i in s: if i not in dic: dic[i] = 0 dic[i] += 1 flag = True for i in dic: if dic[i]%n != 0: flag = False break if not flag: print('NO') else: print('YES') ```
output
1
42,264
0
84,529
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,265
0
84,530
Tags: greedy, strings Correct Solution: ``` import sys import math from collections import Counter,defaultdict LI=lambda:list(map(int,input().split())) MAP=lambda:map(int,input().split()) IN=lambda:int(input()) S=lambda:input() def case(): n = IN() d = {} for i in range(n): s=S() for i in s: if i in d:d[i]+=1 else:d[i]=1 for i in d: if d[i]%n: print("NO") return print("YES") for _ in range(IN()): case() ```
output
1
42,265
0
84,531
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n strings s_1, s_2, …, s_n consisting of lowercase Latin letters. In one operation you can remove a character from a string s_i and insert it to an arbitrary position in a string s_j (j may be equal to i). You may perform this operation any number of times. Is it possible to make all n strings equal? Input The first line contains t (1 ≤ t ≤ 10): the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1000): the number of strings. n lines follow, the i-th line contains s_i (1 ≤ \lvert s_i \rvert ≤ 1000). The sum of lengths of all strings in all test cases does not exceed 1000. Output If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. Example Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO Note In the first test case, you can do the following: * Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. * Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all n strings equal.
instruction
0
42,266
0
84,532
Tags: greedy, strings Correct Solution: ``` for t in range(int(input())): a=int(input()) d={} for i in range(a): s1=input() for j in s1: if j not in d: d[j]=1 else: d[j]+=1 flag=0 for i in d.values(): if i%a>0: flag=1 break if flag==1: print("NO") else: print("YES") ```
output
1
42,266
0
84,533