message
stringlengths
2
23.4k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
129
108k
cluster
float64
6
6
__index_level_0__
int64
258
216k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word. Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure. All the m leaflets that are brought by the spy are numbered from 1 to m. After that it's needed to get the answer to q queries of the following kind: "In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?". The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him! Input The first line contains the string s (1 ≤ |s| ≤ 5·105) — the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters. The second line contains the only integer m (1 ≤ m ≤ 5·104) — the number of texts of leaflets for expertise. Each of the next m lines contains the only string ti — the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5·104. The text of the leaflets consists of only lowercase English letters. The next line contains integer q (1 ≤ q ≤ 5·105) — the number of queries for expertise. Finally, each of the last q lines contains four integers l, r, pl, pr (1 ≤ l ≤ r ≤ m, 1 ≤ pl ≤ pr ≤ |s|), where |s| is the length of the Absolutely Inadmissible Swearword. Output Print q lines. The i-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one. Examples Input suffixtree 3 suffixtreesareawesome cartesiantreeisworsethansegmenttree nyeeheeheee 2 1 2 1 10 1 3 9 10 Output 1 1 3 4 Submitted Solution: ``` s = input() m = int(input()) t = [] for i in range(m): t.append(input()) m = int(input()) data = [] for i in range(m): data.append(list(map(int,input().split()))) for q in data: sub = s[q[2]-1: q[3]] maximo = 0 lo = len(s) index = q[0]-1 for i in range(q[0]-1,q[1]): temp = t[i] contador = 0 for j in range(len(temp)+1 - len(sub)): if sub == temp[j:j+len(sub)]: contador+=1 if contador == 0: maximo = contador index = i + 1 contador = 0 lo = len(temp) elif contador==maximo and len(temp)<=lo: maximo = contador index = i + 1 contador = 0 lo = len(temp) elif contador>= maximo: maximo = contador index = i + 1 contador = 0 lo = len(temp) print(index, maximo) ```
instruction
0
21,637
6
43,274
No
output
1
21,637
6
43,275
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word. Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure. All the m leaflets that are brought by the spy are numbered from 1 to m. After that it's needed to get the answer to q queries of the following kind: "In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?". The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him! Input The first line contains the string s (1 ≤ |s| ≤ 5·105) — the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters. The second line contains the only integer m (1 ≤ m ≤ 5·104) — the number of texts of leaflets for expertise. Each of the next m lines contains the only string ti — the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5·104. The text of the leaflets consists of only lowercase English letters. The next line contains integer q (1 ≤ q ≤ 5·105) — the number of queries for expertise. Finally, each of the last q lines contains four integers l, r, pl, pr (1 ≤ l ≤ r ≤ m, 1 ≤ pl ≤ pr ≤ |s|), where |s| is the length of the Absolutely Inadmissible Swearword. Output Print q lines. The i-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one. Examples Input suffixtree 3 suffixtreesareawesome cartesiantreeisworsethansegmenttree nyeeheeheee 2 1 2 1 10 1 3 9 10 Output 1 1 3 4 Submitted Solution: ``` import re leaflets = [] queries = [] def handleString(data): """String data to compare in the leaflets""" for query in queries: count = 0 nomin = 1 iterval = 0 tdata = data[query[2]-1:query[3]] for strdata in leaflets[query[0]-1:query[1]]: temp = start = 0 iterval = iterval + 1 while True: start = strdata.find(tdata, start) + 1 if start > 0: temp = temp + 1 else: break if temp > count: count = temp nomin = iterval print("{0} {1}".format(nomin, count)) def main(): data = input() leaflet_count = int(input()) for i in range(leaflet_count): leaflets.append(input()) query_count = int(input()) for j in range(query_count): query_data = input() queries.append(list(map(int, query_data.split()))) handleString(data) if __name__ == '__main__': main() ```
instruction
0
21,638
6
43,276
No
output
1
21,638
6
43,277
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,661
6
43,322
Tags: binary search, greedy, strings Correct Solution: ``` a=input() b=input() c=[int(i)-1 for i in input().split()] l=0;r=len(a) while r-l>1: m=l+(r-l)//2 d=list(a) j=0 for i in range(m): d[c[i]]='' # print(d,i) for i in range(len(a)): if d[i]==b[j]: # print(d) j+=1 if j==len(b): l=m;break if j!=len(b):r=m print(l) ```
output
1
21,661
6
43,323
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,662
6
43,324
Tags: binary search, greedy, strings Correct Solution: ``` import math as mt import sys,string input=sys.stdin.readline import random from collections import deque,defaultdict L=lambda : list(map(int,input().split())) Ls=lambda : list(input().split()) M=lambda : map(int,input().split()) I=lambda :int(input()) def isSubseq(s,b,v): i=0 j=0 while(i<len(s) and j<len(b)): if(s[i]==b[j] and v[i]==1): j+=1 i+=1 else: i+=1 if(j==len(b)): return True else: return False a=input().strip() b=input().strip() s=L() l=0 ans=0 h=len(a) while(l<=h): t=mt.ceil((l+h)/2) v=[1 for j in range(len(a))] for i in range(t): v[s[i]-1]=0 if(isSubseq(a,b,v)): #print(t,"P") ans=t l=t+1 else: h=t-1 print(ans) ```
output
1
21,662
6
43,325
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,663
6
43,326
Tags: binary search, greedy, strings Correct Solution: ``` a = input() b = input() c = [i-1 for i in list(map(int,input().split()))] l = 0 r = len(a) while r-l>1: m = l + (r-l) // 2 d = list(a) j = 0 for i in range(m):d[c[i]] = '' for i in range(len(a)): if d[i]==b[j]: j += 1 if j == len(b): l = m break if j!=len(b): r = m print(l) ```
output
1
21,663
6
43,327
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,664
6
43,328
Tags: binary search, greedy, strings Correct Solution: ``` s = input() t = input() a = [int(x) - 1 for x in input().split()] def ok(n): bad = set(a[:n]) it = (a for i, a in enumerate(s) if i not in bad) return all(c in it for c in t) low = 0 high = len(s) while low <= high: mid = (high + low) // 2 if (ok(mid)): low = mid + 1 poss = mid else: high = mid - 1 print (poss) ```
output
1
21,664
6
43,329
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,665
6
43,330
Tags: binary search, greedy, strings Correct Solution: ``` def subset(S,s): i = 0 j = 0 while i<len(S) and j<len(s): if S[i]==s[j]: i+=1 j+=1 else: i+=1 if j==len(s): return True return False n = input() a = input() arr = [int(x)-1 for x in input().split()] new = [] l = 0 r = len(n) while l<r: mid = (l+r+1)//2 sa = arr[:mid] new = list(n) for i in sa: new[i] = '' if subset(''.join(new),a): l = mid else: r = mid-1 print(r) ```
output
1
21,665
6
43,331
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,666
6
43,332
Tags: binary search, greedy, strings Correct Solution: ``` s = (input()) p = (input()) a = [i-1 for i in list(map(int,input().split()))] left = 0 right = len(s) while right - left > 1: d = list(s) mid = int((left + right)/2) for i in range(mid): d[a[i]] = '' j = 0 for i in range(len(s)): if p[j] == d[i]: j += 1 if j == len(p): left = mid break if j != len(p): right = mid print (left) ```
output
1
21,666
6
43,333
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,667
6
43,334
Tags: binary search, greedy, strings Correct Solution: ``` """ NTC here """ import sys inp = sys.stdin.readline def input(): return inp().strip() # flush= sys.stdout.flush # import threading # sys.setrecursionlimit(10**6) # threading.stack_size(2**26) def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): p, t = input(), input() a = lin() n = len(p) def check(x): rm = set(a[:x]) #print(rm) p1 = [p[i] for i in range(n) if i+1 not in rm] #print(p1, t) dc = {} for i, j in enumerate(p1): try: dc[j].append(i) except: dc[j] = [i] for i in dc: dc[i] = dc[i][::-1] pv = -1 for i in t: if i in dc: while dc[i]: ch = dc[i].pop() if ch > pv: pv = ch break else: return False else: return False return True ans = 0 left, right = 0, n-1 while left < right: #print(left, right) md = (left+right+1) >> 1 if check(md): #print("SOL - ", md) ans = max(md, ans) left = md else: #print('wrong ', md) right = md - 1 print(ans) main() # threading.Thread(target=main).start() ```
output
1
21,667
6
43,335
Provide tags and a correct Python 3 solution for this coding contest problem. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters.
instruction
0
21,668
6
43,336
Tags: binary search, greedy, strings Correct Solution: ``` from sys import stdin t = input().strip() p = input().strip() def possible(pos): it = 0 ip = 0 while it < len(t) and ip < len(p): if it in d: it += 1 continue if t[it] == p[ip]: it += 1 ip += 1 else: it += 1 if ip == len(p): return True else: return False a = [int(x)-1 for x in stdin.readline().split()] l = 0 r = len(t) - len(p) d = set() while l < r: mid = (l + r) // 2 if len(d) < (mid + 1): for i in range(len(d), mid + 1): d.add(a[i]) else: for i in range(mid + 1, len(d), 1): d.discard(a[i]) if possible(mid): l = mid + 1 else: r = mid print(l) ```
output
1
21,668
6
43,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` #from bisect import bisect_left as bl #c++ lowerbound bl(array,element) #from bisect import bisect_right as br #c++ upperbound br(array,element) #from __future__ import print_function, division #while using python2 def modinv(n,p): return pow(n,p-2,p) def is_present(parent, child): m = 0 i = 0 j = 0 lc = len(child) lp = len(parent) while i < len(child) and j < len(parent): while i < lc and j < lp and child[i] == parent[j]: i += 1 j += 1 m += 1 while i < lc and j < lp and parent[j] != child[i]: j += 1 return m == len(child) def main(): #sys.stdin = open('input.txt', 'r') #sys.stdout = open('output.txt', 'w') t = input() p = input() moves = [int(x) for x in input().split()] l = 0 r = len(moves) - 1 ans_s = [] while r > l: m = (r+l)//2 removed = set(moves[:m+1]) # print(l, r, m) # print(removed) # input() new_s = [] for i in range(len(t)): if i+1 not in removed: new_s.append(t[i]) if is_present(new_s, p): ans_s = new_s l = m+1 else: r = m if(is_present(ans_s, p)): print(len(t) - len(ans_s)) else: print(0) #------------------ Python 2 and 3 footer by Pajenegod and c1729----------------------------------------- py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') if __name__ == '__main__': main() ```
instruction
0
21,669
6
43,338
Yes
output
1
21,669
6
43,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` a=input() b=input() c=[i-1 for i in list(map(int,input().split()))] l=0 r=len(a) while r-l>1 : m=l+(r-l)//2#two pointer d=list(a) j=0 for o in range(m):#first half delete value replace with "" d[c[o]]="" for i in range(int(len(a))): if(d[i]==b[j]): j+=1 if(j==len(b)): l=m break if(j!=len(b)):r=m print(l) ```
instruction
0
21,670
6
43,340
Yes
output
1
21,670
6
43,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') 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") ALPHA='abcdefghijklmnopqrstuvwxyz/' M=1000000007 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def sub(x): have=set(a[:x]) i=0 j=0 while(i<len(s) and j<len(t)): if(i+1 not in have and s[i]==t[j]): j+=1 i+=1 return j==len(t) s=input() t=input() a=array() low=0 high=len(a) # print(sub(3)) while(low<=high): mid=low+(high-low)//2 if(sub(mid)): ans=mid low=mid+1 else: high=mid-1 print(ans) ```
instruction
0
21,671
6
43,342
Yes
output
1
21,671
6
43,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` def binaryFind(): c = [0]*lenp maxlen = 0 for i in range(0,mid): c[a[i]-1]=1 for i in range(0,lenp): if c[i]==0: if p[i]==t[maxlen]: maxlen+=1 if maxlen==lent: return True return False p = input() t = input() a = list(map(int,input().split())) lenp = len(p) lent = len(t) l=0 r=lenp while l<=r: mid = (l+r)//2 isok = binaryFind() if isok==True: l = mid+1 else : r = mid-1 print(l-1) ```
instruction
0
21,672
6
43,344
Yes
output
1
21,672
6
43,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` #!/usr/bin/env python3 from sys import stdin,stdout import re import bisect bisect def ri(): return map(int, input().split()) t = input() p = input() a = list(ri()) p = ".*".join(p) ptrn = re.compile(p) tt = list(t[:]) tt[a[0]] = '' if not ptrn.search("".join(tt)): print(0) exit() s = 0 e = len(a)-1 while True: m = s + (e-s)//2 ii = [0]*len(t) tt = [] for i in range(m+1): ii[a[i]-1] = 1 for i in range(len(t)): if ii[i] == 0: tt.append(t[i]) if ptrn.search("".join(tt)): s = m else: e = m if e-s == 1: print(s+1) exit() ```
instruction
0
21,673
6
43,346
No
output
1
21,673
6
43,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` def mi(): return map(int, input().split()) s = list(input()) t = input() a = list(map(int, input().split())) def isSubSequence(str1,str2,m,n): j = 0 # Index of str1 i = 0 # Index of str2 # Traverse both str1 and str2 # Compare current character of str2 with # first unmatched character of str1 # If matched, then move ahead in str1 while j<m and i<n: if str1[j] == str2[i]: j = j+1 i = i + 1 # If all characters of str1 matched, then j is equal to m return j==m l = len(a) high = l low = 0 bestans = -1 while low<high: mid = (low+high)//2 b = a.copy() s1 = s.copy() for i in range(mid): s1[b[i]-1] = '' s1 = ''.join(s1) if isSubSequence(t,s1,len(t),len(s1)): low = mid+1 bestans = max(bestans,mid) else: high = mid-1 print (bestans) ```
instruction
0
21,674
6
43,348
No
output
1
21,674
6
43,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` #!/usr/bin/env python3 from sys import stdin,stdout import re import bisect bisect def ri(): return map(int, input().split()) t = input() p = input() a = list(ri()) p = ".*".join(p) ptrn = re.compile(p) tt = list(t[:]) tt[a[0]] = '' if not ptrn.search("".join(tt)): print("come here") print(0) exit() s = 0 e = len(a)-1 while True: m = s + (e-s)//2 ii = [0]*len(t) tt = [] for i in range(m+1): ii[a[i]-1] = 1 for i in range(len(t)): if ii[i] == 0: tt.append(t[i]) if ptrn.search("".join(tt)): s = m else: e = m if e-s == 1: print(s+1) exit() ```
instruction
0
21,675
6
43,350
No
output
1
21,675
6
43,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word p can be obtained by removing the letters from word t. Input The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t. Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct). Output Print a single integer number, the maximum number of letters that Nastya can remove. Examples Input ababcba abb 5 3 4 1 7 6 2 Output 3 Input bbbabb bb 1 6 3 4 2 5 Output 4 Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. Submitted Solution: ``` def mi(): return map(int, input().split()) s = list(input()) t = input() a = list(map(int, input().split())) def isSubSequence(str1,str2,m,n): j = 0 # Index of str1 i = 0 # Index of str2 # Traverse both str1 and str2 # Compare current character of str2 with # first unmatched character of str1 # If matched, then move ahead in str1 while j<m and i<n: if str1[j] == str2[i]: j = j+1 i = i + 1 # If all characters of str1 matched, then j is equal to m return j==m l = len(a) high = l low = 0 bestans = -1 while low<high: mid = (low+high)//2 b = a.copy() s1 = s.copy() for i in range(mid): s1[b[i]-1] = '' s1 = ''.join(s1) if s1.find(t)>-1: low = mid+1 bestans = max(bestans,mid) else: if isSubSequence(t,s1,len(t),len(s1)): low = mid+1 else: high = mid print (bestans) ```
instruction
0
21,676
6
43,352
No
output
1
21,676
6
43,353
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,705
6
43,410
Tags: binary search, dfs and similar Correct Solution: ``` f0 = "What are you doing at the end of the world? Are you busy? Will you save us?" f1 = "What are you doing while sending \"{0}\"? Are you busy? Will you send \"{0}\"?" a = list(f1.split("{0}")) b = list(map(len, a)) q = int(input()) f = lambda n: 143 * 2**min(n, 54) - 68 for _ in range(q): n, k = map(int, input().split()) ans = "" while n > 0 and b[0] < k < b[0] + f(n-1): k -= b[0] n -= 1 while not ans: w = f(n-1) if k > f(n): ans = "." elif n == 0: ans = f0[k-1] elif k <= b[0]: ans = a[0][k-1] elif k <= b[0] + w: k -= b[0] n -= 1 elif k <= b[0] + w + b[1]: k -= b[0] + w ans = a[1][k-1] elif k <= b[0] + w + b[1] + w: k -= b[0] + w + b[1] n -= 1 else: k -= b[0] + w + b[1] + w ans = a[2][k-1] print(ans, end="") ```
output
1
21,705
6
43,411
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,706
6
43,412
Tags: binary search, dfs and similar Correct Solution: ``` s0 = 'What are you doing at the end of the world? Are you busy? Will you save us?' s1 = 'What are you doing while sending "' s2 = '"? Are you busy? Will you send "' l0 = len(s0) l1 = len(s1) l2 = len(s2) def get(h): if h > 55: return 10**20 return (l0 + l1 + l2 + 2 << h) - l1 - l2 - 2 def solve(n, k): if get(n) <= k: return '.' while True: if n == 0: return s0[k] if k < l1: return s1[k] k -= l1 if k < get(n-1): n -= 1 continue k -= get(n-1) if k < l2: return s2[k] k -= l2 if k < get(n-1): n -=1 else: return '"?'[k - get(n - 1)] q = int(input()) for i in range(q): n,k=list(map(int,input().split())) print(solve(n,k-1),end='') ```
output
1
21,706
6
43,413
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,707
6
43,414
Tags: binary search, dfs and similar Correct Solution: ``` import sys sys.setrecursionlimit(1500) s1 = "What are you doing at the end of the world? Are you busy? Will you save us?" a = "What are you doing while sending \"" b = "\"? Are you busy? Will you send \"" c = "\"?" ans = "" def solve(n, k): if n == 0: if k >= len(s1): return "." else: return s1[k] if k < len(a): return a[k] k -= len(a) prev_len = (2 ** (n - 1) - 1) * (len(a) + len(b) + len(c)) + (2 ** (n - 1)) * len(s1) if k < prev_len: return solve(n - 1, k) k -= prev_len if k < len(b): return b[k] k -= len(b) if k < prev_len: return solve(n - 1, k) k -= prev_len if k < len(c): return c[k] else: return "." for _ in range(int(input())): n, k = list(map(int, input().split())) k -= 1 if n > 65: m = n - 65 if k < len(a) * m: ans += a[k % len(a)] continue k -= len(a) * m n = n - m ans += solve(n, k) print(ans) ```
output
1
21,707
6
43,415
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,708
6
43,416
Tags: binary search, dfs and similar Correct Solution: ``` def get_len(ind): if ind > 63: ind = 63 k = (1 << ind) - 1 return k * (len1 + len2 + len3) + (k + 1) * len_f0 f0 = 'What are you doing at the end of the world? Are you busy? Will you save us?' s1 = 'What are you doing while sending "' s2 = '"? Are you busy? Will you send "' s3 = '"?' len_f0 = len(f0) len1 = len(s1) len2 = len(s2) len3 = len(s3) q = int(input()) for i in range(q): n, k = map(int, input().split()) k -= 1 l = 0; ind = n - 1 while True: if k < l + len_f0 and ind == -1: print(f0[k - l], end='') break elif k < l + len1: print(s1[k - l], end='') break elif ind >= 0 and k < l + len1 + get_len(ind): l += len1 ind = ind - 1 elif ind >= 0 and k < l + len1 + get_len(ind) + len2: print(s2[k - l - len1 - get_len(ind)], end='') break elif ind >= 0 and k < l + len1 + get_len(ind) + len2 + get_len(ind): l += len1 + get_len(ind) + len2 ind = ind - 1 elif ind >= 0 and k < l + len1 + get_len(ind) + len2 + get_len(ind) + len3: print(s3[k - l - len1 - get_len(ind) - len2 - get_len(ind)], end='') break else: print('.', end='') break ```
output
1
21,708
6
43,417
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,709
6
43,418
Tags: binary search, dfs and similar Correct Solution: ``` f0 = "What are you doing at the end of the world? Are you busy? Will you save us?" p1 = "What are you doing while sending \"" p2 = "\"? Are you busy? Will you send \"" p3 = "\"?" f = [] def dfs(n, k): if k > f[n]: return '.' if n == 0: return f0[k-1] if k <= 34: return p1[k-1] if k <= f[n-1] + 34: return dfs(n - 1, k - 34) if k <= f[n-1] + 66: return p2[k - f[n-1] - 34 - 1] if k <= 2 * f[n-1] + 66: return dfs(n - 1, k - f[n-1] - 66) return p3[k - f[n-1] * 2 - 66 - 1] f.append(75) cnt = 0 for i in range(0, 100): f.append(f[cnt] * 2 + 68) cnt += 1 def solve(): n, k =map(int, input().split(' ')) while n > 56 and k > 34: k -= 34 n -= 1 if k <= 34: print(p1[k - 1], end='') else: print(dfs(n, k), end='') def main(): case = 1 case = int(input()) for case in range(case): solve() main() ```
output
1
21,709
6
43,419
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,710
6
43,420
Tags: binary search, dfs and similar Correct Solution: ``` import sys sys.setrecursionlimit(1001000) ants = "What are you doing while sending \"" ant = len(ants) meios = "\"? Are you busy? Will you send \"" meio = len(meios) fims = "\"?" fim = len(fims) base = "What are you doing at the end of the world? Are you busy? Will you save us?" f = [0] f[0] = len(base) for i in range(1,100001): f.append(2*f[i-1] + ant + meio + fim) if(f[i] >= int(1e18) + 10): f[i] = int(1e18) + 10 def busca(n,k): while True: if n == 0: if k > len(base): return "." else: return base[k-1] if k <= ant: return ants[k-1] k -= ant if k <= f[n-1]: n-=1 continue k -= f[n-1] if k <= meio: return meios[k-1] k -= meio if k <= f[n-1]: n -= 1 continue k -= f[n-1] if k <= fim: return fims[k-1] return "." def main(): q = int(input()) for i in range(0,q): n,k = map(int, input().split()) print (busca(n,k), end='') print() main() ```
output
1
21,710
6
43,421
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,711
6
43,422
Tags: binary search, dfs and similar Correct Solution: ``` tmp = [""] * 3 tmp[0] = "What are you doing while sending \"" tmp[1] = "\"? Are you busy? Will you send \"" tmp[2] = "\"?" def fun(s): return tmp[0] + s + tmp[1] + s + tmp[2] MAX = 60 f = [""] * 3 f[0] = "What are you doing at the end of the world? Are you busy? Will you save us?" f[1] = fun(f[0]) f[2] = fun(f[1]) l = [0] * (MAX + 1) l[0] = len(f[0]) l[1] = len(f[1]) l[2] = len(f[2]) for i in range(3, MAX + 1): l[i] = l[i - 1] * 2 + 68 def solve(n, k): if n <= MAX and l[n] < k: return "." if n <= 2: return f[n][k - 1] if n > MAX: if k > 34 * (n - MAX): return solve(MAX, k - 34 * (n - MAX)) return tmp[0][(k - 1) % 34] if k <= 34: return tmp[0][k - 1] k -= 34 if k <= l[n - 1]: return solve(n - 1, k) k -= l[n - 1] if k <= 32: return tmp[1][k - 1] k -= 32 if k <= l[n - 1]: return solve(n - 1, k) k -= l[n - 1] return tmp[2][k - 1] q = int(input()) for i in range(q): nn, kk = map(int, input().split()) print(solve(nn, kk), end="") print() ```
output
1
21,711
6
43,423
Provide tags and a correct Python 3 solution for this coding contest problem. What are you doing at the end of the world? Are you busy? Will you save us? <image> Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will you save us?". She wants to let more people know about it, so she defines fi = "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1. For example, f1 is "What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1. It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces. Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes). Can you answer her queries? Input The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions. Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018). Output One line containing q characters. The i-th character in it should be the answer for the i-th query. Examples Input 3 1 1 1 2 1 111111111111 Output Wh. Input 5 0 69 1 194 1 139 0 47 1 66 Output abdef Input 10 4 1825 3 75 3 530 4 1829 4 1651 3 187 4 584 4 255 4 774 2 474 Output Areyoubusy Note For the first two examples, refer to f0 and f1 given in the legend.
instruction
0
21,712
6
43,424
Tags: binary search, dfs and similar Correct Solution: ``` s = [ "What are you doing at the end of the world? Are you busy? Will you save us?", "What are you doing while sending \"", "\"? Are you busy? Will you send \"", "\"?" ] l = list(map(len, s)) ls = [l[0]] while ls[-1] <= int(1e18): ls.append(2 * ls[-1] + sum(l[1:])) ans = "" q = int(input()) for _ in range(q): n, k = map(int, input().split()) c = '' while c == '': if n < len(ls) and ls[n] < k: c = '.' elif n == 0: c = s[0][k - 1] elif k <= l[1]: c = s[1][k - 1] elif l[1] < k and (len(ls) <= n or k <= l[1] + ls[n - 1]): k -= l[1] n -= 1 elif l[1] + ls[n - 1] < k <= l[1] + ls[n - 1] + l[2]: c = s[2][k - l[1] - ls[n - 1] - 1] elif l[1] + ls[n - 1] + l[2] < k <= l[1] + ls[n - 1] + l[2] + ls[n - 1]: k -= l[1] + ls[n - 1] + l[2] n -= 1 else: c = s[3][k - ls[n] - 1] ans += c print(ans) ```
output
1
21,712
6
43,425
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not. Input The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field. Output Print "YES" if the situation is dangerous. Otherwise, print "NO". Examples Input 001001 Output NO Input 1000000001 Output YES
instruction
0
21,742
6
43,484
Tags: implementation, strings Correct Solution: ``` position=input() if len(position) <=7: print("NO") else: flag=False for i in range(len(position)): if position[i:i+7]=="0000000" or position[i:i+7]=="1111111": flag=True break elif i > len(position)-7: break if flag: print("YES") else: print("NO") ```
output
1
21,742
6
43,485
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not. Input The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field. Output Print "YES" if the situation is dangerous. Otherwise, print "NO". Examples Input 001001 Output NO Input 1000000001 Output YES
instruction
0
21,744
6
43,488
Tags: implementation, strings Correct Solution: ``` s=input() pres,prev,c=0,0,0 for i in s: pres=int(i) if pres==prev: c+=1 prev=pres if c==7: print("YES") break else: c=1 prev=pres if c<7: print("NO") ```
output
1
21,744
6
43,489
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not. Input The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field. Output Print "YES" if the situation is dangerous. Otherwise, print "NO". Examples Input 001001 Output NO Input 1000000001 Output YES
instruction
0
21,746
6
43,492
Tags: implementation, strings Correct Solution: ``` if __name__ == "__main__": s = list(char for char in input()) l_old=s[0] count_one=0 count_zero=0 yes=False for l in s: if l == l_old: if l=="0": count_zero+=1 count_one=1 #print(f"zeros:{count_zero} ones:{count_one} l:{l} l_old:{l_old}") else: count_zero=1 count_one+=1 #print(f"zeros:{count_zero} ones:{count_one} l:{l} l_old:{l_old}") else: count_zero=1 count_one=1 l_old=l if (count_one==7) or (count_zero==7): yes=True break if yes: print("YES") else: print("NO") ```
output
1
21,746
6
43,493
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves football very much. One day, as he was watching a football match, he was writing the players' current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not. Input The first input line contains a non-empty string consisting of characters "0" and "1", which represents players. The length of the string does not exceed 100 characters. There's at least one player from each team present on the field. Output Print "YES" if the situation is dangerous. Otherwise, print "NO". Examples Input 001001 Output NO Input 1000000001 Output YES
instruction
0
21,747
6
43,494
Tags: implementation, strings Correct Solution: ``` pat1 = '0000000' pat2 = '1111111' string = str(input()) if((pat1 in string) or (pat2 in string)): print("YES") else: print("NO") ```
output
1
21,747
6
43,495
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,487
6
44,974
Tags: implementation, strings Correct Solution: ``` from sys import * from collections import * def inp(n): if n == 1: return map(int, stdin.readline().split()) elif n == 2: return map(float, stdin.readline().split()) else: return map(str, stdin.readline().split()) def main(): n, m = inp(1) org, mem = deque(input()), defaultdict(int) for i in range(m): u, v = inp(3) flag1, flag2 = 0, 0 for i, j in mem.items(): if not flag1 and j == u: mem[i] = v flag1 = 1 if not flag2 and j == v: mem[i] = u flag2 = 1 if flag1 and flag2: break if not flag1: mem[u] = v if not flag2: mem[v] = u # print(mem) for i in range(n): if mem[org[i]]: org[i]=mem[org[i]] print(''.join(org)) if __name__ == '__main__': main() ```
output
1
22,487
6
44,975
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,488
6
44,976
Tags: implementation, strings Correct Solution: ``` n, m = [int(i) for i in input().split()] s = input() st = [chr(ord('a')+i) for i in range(26)] for i in range (m) : a, b = input().split() for i in range(26): if st[i] == a: st[i] = b elif st[i] == b: st[i] = a print(''.join([ st[ord(i)-ord('a')] for i in s])) '''RATHER THAN CHANGING LONG STRING WE WILL ONLY CHANG ETHE ALPHABETS AND THEN CHNAGE THE STRING ONLY ONCE ACCORDING TO ALPHABETS ''' ```
output
1
22,488
6
44,977
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,489
6
44,978
Tags: implementation, strings Correct Solution: ``` alf = list('abcdefghijklmnopqrstuvwxyz') n, m = map(int, input().split()) s = input() for i in range(m): x, y = input().split() index1 = alf.index(x) index2 = alf.index(y) alf[index1], alf[index2] = alf[index2], alf[index1] for i in s: print(alf[ord(i) - ord('a')], end='') ```
output
1
22,489
6
44,979
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,490
6
44,980
Tags: implementation, strings Correct Solution: ``` import string def mp(): return map(int,input().split()) def lt(): return list(map(int,input().split())) def pt(x): print(x) n,m = mp() s = input() s1 = string.ascii_lowercase for i in range(m): L = input().split() a,b = L[0],L[1] s1 = s1.translate(str.maketrans(a+b,b+a)) pt(s.translate(str.maketrans(string.ascii_lowercase,s1))) ```
output
1
22,490
6
44,981
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,491
6
44,982
Tags: implementation, strings Correct Solution: ``` import sys size,swaps=map(int,sys.stdin.readline().split()) word=sys.stdin.readline() alp='abcdefghijklmnopqrstuvwxyz' alp2='abcdefghijklmnopqrstuvwxyz' for i in range(swaps): a,b=map(str,sys.stdin.readline().split()) alp2=alp2.replace(a,b.upper()) alp2=alp2.replace(b,a) alp2=alp2.lower() for i in range(26): word=word.replace(alp[i],alp2[i].upper()) sys.stdout.write(word.lower().strip('\n')) ```
output
1
22,491
6
44,983
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,492
6
44,984
Tags: implementation, strings Correct Solution: ``` n,m=map(int,input().split()) x=list(input()) d={} y=list(set(x)) z=list(set(x)) for i in x: d[i]=i for t in range(m): a,b=map(str,input().split()) for i in range(len(y)): if y[i]==a: y[i]=b elif y[i]==b: y[i]=a for i in range(len(y)): d[z[i]]=y[i] # print(d) for i in range(n): x[i]=d[x[i]] print("".join(x)) ```
output
1
22,492
6
44,985
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,493
6
44,986
Tags: implementation, strings Correct Solution: ``` import math from decimal import Decimal def na(): n = int(input()) b = [int(x) for x in input().split()] return n,b def nab(): n = int(input()) b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] return n,b,c def dv(): n, m = map(int, input().split()) return n,m def dva(): n, m = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] return n,m,b def eratosthenes(n): sieve = list(range(n + 1)) for i in sieve: if i > 1: for j in range(i + i, len(sieve), i): sieve[j] = 0 return sorted(set(sieve)) def nm(): n = int(input()) b = [int(x) for x in input().split()] m = int(input()) c = [int(x) for x in input().split()] return n,b,m,c def dvs(): n = int(input()) m = int(input()) return n, m n, m = map(int, input().split()) s = input() d = {} for i in range(97, 123): d[chr(i)] = chr(i) for i in range(m): x, y = input().split() d[x], d[y] = d[y], d[x] new = '' t = {} for i in d: t[d[i]] = i for i in s: new += t[i] print(new) ```
output
1
22,493
6
44,987
Provide tags and a correct Python 3 solution for this coding contest problem. The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi. Output Print the new name of the corporation. Examples Input 6 1 police p m Output molice Input 11 6 abacabadaba a b b c a d e g f a b b Output cdcbcdcfcdc Note In the second sample the name of the corporation consecutively changes as follows: <image> <image> <image> <image> <image> <image>
instruction
0
22,494
6
44,988
Tags: implementation, strings Correct Solution: ``` n,m=map(int,input().split()) st=input() dic={} xr="abcdefghijklmnopqrstuvwxyz" for x in xr: dic[x]=x for x in range(m): g,h=input().split() dic[g],dic[h]=dic[h],dic[g] #print(dic) t={} for k in dic: t[dic[k]]=k #print(t) ls=[ t[x] for x in st] print(''.join(ls)) ```
output
1
22,494
6
44,989
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,198
6
46,396
Tags: data structures, greedy, trees Correct Solution: ``` # https://codeforces.com/contest/37/problem/C import sys sys.setrecursionlimit(100000) class Node: def __init__(self): self.child = dict() self.isEnd = 0 def DFS(stack, level, mark, arr, m, pos): if len(mark) == level: arr.append((pos, mark)) stack.isEnd = 1 return for i in ['0', '1']: if len(arr) != m: return if not stack.child.get(i): stack.child[i] = Node() DFS(stack.child[i], level, mark + i, arr, m, pos) elif not stack.child[i].isEnd: DFS(stack.child[i], level, mark + i, arr, m, pos) def addWord(root, level, arr): temp = root mark = len(arr) pos = level[0] level = level[1] DFS(temp, level, '', arr, mark, pos) if temp.isEnd or mark == len(arr): return False return True n = int(input()) l = list(map(int, input().split())) for i in range(n): l[i] = (i, l[i]) l.sort(key=lambda x: x[1]) root = Node() ans = [] for i in l: if not addWord(root, i, ans): print('NO') exit() ans.sort(key=lambda x: x[0]) print('YES') for i in ans: print(i[1]) ```
output
1
23,198
6
46,397
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,199
6
46,398
Tags: data structures, greedy, trees Correct Solution: ``` class Node(): def __init__(self): self.child = dict() self.count = 0 self.weight = 0 def addWord(root, s): s = "" tmp = root length = 0 flag = False for i in range(s): if len(tmp.child) == 0: tmp.child[0] = Node() tmp = tmp.child[0] elif len(tmp.child) == 1: if tmp.child[0].count > 0: tmp.child[1] = Node() tmp = tmp.child[1] tmp.count+=1 else: tmp = tmp.child[0] elif len(tmp.child) == 2: if tmp.child[0].count > 0 and tmp.child[1].count > 0: return True elif tmp.child[0].count == 0: tmp = tmp.child[0] else: tmp = tmp.child[1] tmp.count+=1 return False for ch in s: length += 1 if ch in tmp.child: tmp = tmp.child[ch] if tmp.count > 0: flag = True if length == len(s): flag = True else: tmp.child[ch] = Node() tmp = tmp.child[ch] tmp.weight+=1 tmp.count += 1 return flag def find(root,s): tmp = root for ch in s: if ch in tmp.child: tmp = tmp.child[ch] else: return 0 return tmp.weight def isWord(node): return node.count != 0 def isEmpty(node): return len(node.child) == 0 def removeWord(root, s, level, size): if root == None: return False if level == size: if root.count > 0: root.count-=1 return True return False ch = s[level] flag = removeWord(root.child[ch],s,level+1,size) if flag == True and isWord(root.child[ch]) == False and isEmpty(root.child[ch]): tmp = root.child[ch] del tmp del root.child[ch] return flag n = int(input()) cur = 0 word = ["" for _ in range(n)] def dfs(string, depth): global cur, n, ar, word if cur >= n: return if ar[cur][0] == depth: word[ar[cur][1]] = string cur += 1 return string += '0' dfs(string, depth + 1) string = string[:-1] + '1' dfs(string, depth + 1) string = string[:-1] length = [int(i) for i in input().split()] ar = [[length[i],i] for i in range(n)] ar.sort() dfs("",0) if cur < n: print("NO") else: print("YES") for i in range(n): print(word[i]) ```
output
1
23,199
6
46,399
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,200
6
46,400
Tags: data structures, greedy, trees Correct Solution: ``` import itertools t = int e = input I = list r = sorted A = enumerate X = bin m = len u = print o = range p = itertools.groupby n = t(e()) L = [t(_)for _ in e().split()] M = I(r(A(L), key=lambda x: x[1])) N = [0 for x in L] l = 0 b = 0 for k, U in p(M, key=lambda x: x[1]): l <<= k-b b = k for g in U: s = X(l)[2:] N[g[0]] = '0'*(k-m(s))+s l += 1 if l >= (1 << k)+1: u("NO") break else: u("YES") for i in o(n): u(N[i]) ```
output
1
23,200
6
46,401
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,201
6
46,402
Tags: data structures, greedy, trees Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) for i in range(n): l[i] = (l[i], i) l.sort(key=lambda x : x[0]) currentWord = "" firstL = l[0][0] for i in range(firstL): currentWord += '0' ans = ['' for _ in range(n)] ans[l[0][1]] = currentWord for i in range(1, n): haveNext = False while True: lastChar = currentWord[-1] currentWord = currentWord[:-1] if lastChar == '0': currentWord += '1' haveNext = True break elif len(currentWord) == 0: break if not haveNext: print("NO") import sys sys.exit() while len(currentWord) < l[i][0]: currentWord += '0' ans[l[i][1]] = currentWord print('YES') for s in ans: print(s) ```
output
1
23,201
6
46,403
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,202
6
46,404
Tags: data structures, greedy, trees Correct Solution: ``` import sys class input_tokenizer: tokens = None def __init__(self): self.tokens = sys.stdin.read().split()[::-1] def has_next(self): return self.tokens != [] and self.tokens != None def next(self): token = self.tokens[-1] self.tokens.pop() return token inp = input_tokenizer() class Node: def __init__(self, index, depth): self.index = index self.depth = depth def __lt__(self, other): return self.depth < other.depth def dfs(s, depth, n, curr, word, arr): stack = [[s, depth]] while True: if len(stack) == 0: break s, depth = stack.pop() if curr >= n: return curr, word if arr[curr].depth == depth: word[arr[curr].index] = s curr += 1 continue next_s = s + '0' stack.append([next_s, depth + 1]) next_s = s + '1' stack.append([next_s, depth + 1]) return curr, word try: n = int(inp.next()) arr = [] for i in range(n): word_len = int(inp.next()) arr.append(Node(i, word_len)) arr.sort() word = ['' for i in range(n)] curr = 0 results = dfs('', 0, n, curr, word, arr) word = results[1] curr = results[0] if curr < n: print('NO') else: print('YES') print(*word, sep='\n') except Exception as ex: print(ex) ```
output
1
23,202
6
46,405
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,203
6
46,406
Tags: data structures, greedy, trees Correct Solution: ``` import sys sys.setrecursionlimit(1000000) class Node(): def __init__(self, val): self.child = dict() self.blocked = False self.val = val class Trie(): def __init__(self): self.root = Node("") def addLength(self, length, id, res): cur = self.root stk = [cur] while length > 0 and len(stk) > 0: length -= 1 if 0 not in cur.child : cur.child[0] = Node("0") if not cur.child[0].blocked: cur = cur.child[0] else: if 1 not in cur.child: cur.child[1] = Node("1") if not cur.child[1].blocked: cur = cur.child[1] else: cur.blocked = True stk.pop() if len(stk) > 0: cur = stk[-1] length += 2 continue stk.append(cur) if len(stk) == 0: return False cur.blocked = True res[id] = ''.join(x.val for x in stk) #print(res[id]) return True n = int(input()) a = list(map(int, input().split())) a = sorted([(a[i], i) for i in range(n)]) trie = Trie() res = [""] * n #print(*a) for length, i in a: if not trie.addLength(length, i, res): print("NO") exit(0) print("YES") print(*res, sep = "\n") ```
output
1
23,203
6
46,407
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,204
6
46,408
Tags: data structures, greedy, trees Correct Solution: ``` n, ls = int(input()), list(map(int, input().split())) l = [(ls[i], i) for i in range(n)] l.sort(key = lambda x: x[0]) c, a = 0, ['0'*l[0][0]] for i in range(1, n): if c == 2**l[i-1][0]-1: break c = (c+1) * 2**(l[i][0] - l[i-1][0]) a.append(format(c, "0" + str(l[i][0]) + "b")) if len(a) == n: print("YES") ans = [0] * n for i in range(n): ans[l[i][1]] = a[i] for i in range(n): print(ans[i]) else: print("NO") # Made By Mostafa_Khaled ```
output
1
23,204
6
46,409
Provide tags and a correct Python 3 solution for this coding contest problem. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO
instruction
0
23,205
6
46,410
Tags: data structures, greedy, trees Correct Solution: ``` """ https://codeforces.com/contest/37/problem/C """ curr, n = 0, 0 a = [] word = [] def dfs(string, depth): global curr, n, a, word if curr >= n: return if a[curr][0] == depth: word[a[curr][1]] = string curr += 1 return string += '0' dfs(string, depth + 1) string = string[:-1] + '1' dfs(string, depth + 1) string = string[:-1] def main(): global curr, n, a, word n = int(input()) length = list(map(int, input().split())) a = [[length[i], i] for i in range(n)] word = ["" for _ in range(n)] a.sort() curr = 0 dfs("", 0) if curr < n: print("NO") else: print("YES") for i in range(n): print(word[i]) return 0 if __name__ == "__main__": main() ```
output
1
23,205
6
46,411
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO Submitted Solution: ``` import sys import operator import queue sys.setrecursionlimit(100000) # global constant INF = int(1e7+1) MAX = 2 # For testing #sys.stdin = open("INP.txt", 'r') #sys.stdout = open("OUT.txt", 'w') # global variables ans = 1000*[None] # classes class Pair: def __init__(self, first, second): self.first = first self.second = second class node: def __init__(self): self.countLeaf = 0 self.child = MAX*[None] self.isValid = True # functions def addWord(root, length, string): if length == 0: return True, string if not root.child[0] or root.child[0].isValid: string+='0' if not root.child[0]: root.child[0] = node() if length==1: root.child[0].isValid = False ret, string = addWord(root.child[0], length-1, string) if root.child[0] and root.child[1] and not root.child[0].isValid and not root.child[1].isValid: root.isValid = False return ret, string elif not root.child[1] or root.child[1].isValid: string+='1' if not root.child[1]: root.child[1] = node() if length==1: root.child[1].isValid = False ret, string = addWord(root.child[1], length-1, string) if root.child[0] and root.child[1] and not root.child[0].isValid and not root.child[1].isValid: root.isValid = False return ret, string return False, string # main function def main(): n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = Pair(a[i], i) a.sort(key = lambda x: x.first) root = node() for word in a: flag, string = addWord(root, word.first, '') if not flag: print("NO") raise SystemExit ans[word.second] = string print("YES") for i in range(n): print(ans[i]) main() ```
instruction
0
23,206
6
46,412
Yes
output
1
23,206
6
46,413
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO Submitted Solution: ``` from sys import setrecursionlimit setrecursionlimit(1000000) bit = ['0', '1'] class Node: def __init__(self): self.endWord = False self.child = {} def addWord(root, lengthLeft): if root.endWord: return False if lengthLeft == 0: root.endWord = True return True for char in bit: if char not in root.child: root.child[char] = Node() if addWord(root.child[char], lengthLeft - 1): return True return False def saveWord(root, st): if root.endWord: global words words.append(st) for char in root.child: saveWord(root.child[char], st + char) N = int(input()) root = Node() lengths = list(map(int, input().split())) sortedLens = [] for i in range(1, N + 1): sortedLens.append((lengths[i - 1], i)) sortedLens.sort() for length, ID in sortedLens: if not addWord(root, length): print("NO") exit() words = [] saveWord(root, "") ans = [] for i in range(N): length, ID = sortedLens[i] word = words[i] ans.append((ID, word)) ans.sort() print("YES") for ID, word in ans: print(word) ```
instruction
0
23,207
6
46,414
Yes
output
1
23,207
6
46,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO Submitted Solution: ``` def get_next(b): if not b: b.append(0) return add = 1 remember = 0 for i in range(len(b) - 1 , -1 , -1): temp = b[i] + add + remember if temp > 1: temp = 0 remember = 1 else: remember = 0 add = 0 b[i] = temp def is_largest(b): if not b: return False for i in range(len(b)): if b[i] == 0: return False return True def solve(): N = int(input()) items =[] index = 0 for length in map(int,input().split()): items.append((index,length)) index += 1 items = sorted(items,key=lambda item: item[1]) result = [] results = [0 for _ in range(N)] for item in items: index,length = item if is_largest(result): print('NO') break if result: get_next(result) else: result = [0 for _ in range(length)] while len(result) < length: result.append(0) results[index] = ''.join(str(num) for num in result) else: print('YES') for result in results: print(result) solve() ```
instruction
0
23,208
6
46,416
Yes
output
1
23,208
6
46,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves. Input The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000. Output If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. Examples Input 3 1 2 3 Output YES 0 10 110 Input 3 1 1 1 Output NO Submitted Solution: ``` import sys sys.setrecursionlimit(1050) class Node: def __init__(self, depth, parent): self.depth = depth self.parent = parent self.left = None self.right = None self.leaf = False def __str__(self): return 'depth = %d, left? %s, right? %s' % (self.depth, self.left != None, self.right != None) stacks = {} def traverse(node, symbols): if node.leaf: s = ''.join(symbols) stacks.setdefault(len(s), []).append(s) return if node.left != None: symbols.append('0') traverse(node.left, symbols) symbols.pop() if node.right != None: symbols.append('1') traverse(node.right, symbols) symbols.pop() n = int(input()) original_lengths = list(map(int, input().split())) lengths = sorted(original_lengths[:]) root = Node(0, None) curr = root for i in range(lengths[0]): curr.left = Node(curr.depth+1, curr) curr = curr.left curr.leaf = True for length in lengths[1:]: curr = curr.parent while curr.right != None: if curr == root: print('NO') sys.exit() curr = curr.parent curr.right = Node(curr.depth+1, curr) curr = curr.right while curr.depth != length: curr.left = Node(curr.depth+1, curr) curr = curr.left curr.leaf = True print('YES') traverse(root, []) for length in original_lengths: print(stacks[length].pop()) ```
instruction
0
23,209
6
46,418
Yes
output
1
23,209
6
46,419