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
Provide tags and a correct Python 3 solution for this coding contest problem. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f
instruction
0
92,309
6
184,618
Tags: dp, greedy, implementation Correct Solution: ``` checking = input() vowels = ["a","e","i","o","u"] streak = [] new = "" for i,letter in enumerate(checking): # print(streak) if letter not in vowels: streak.append(letter) else: streak = [] if len(streak) == 3: if streak.count(letter) == 3: streak = [letter, letter] else: streak = [letter] new += " " new += letter print(new) ```
output
1
92,309
6
184,619
Provide tags and a correct Python 3 solution for this coding contest problem. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f
instruction
0
92,310
6
184,620
Tags: dp, greedy, implementation Correct Solution: ``` a=str(input()) sgl="bcdfghjklmnpqrstvwxyz" ans="" for i in range(len(a)): ans+=a[i] if len(ans)>2: if sgl.count(ans[-1]) and sgl.count(ans[-2]) and sgl.count(ans[-3]) and (ans[-1]!=ans[-2] or ans[-1]!=ans[-3]): ans=ans[:-1]+" "+ ans[-1] print(ans) ```
output
1
92,310
6
184,621
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` s = input() ans = [] start = 0 c = 0 const = set() for i in range(len(s)): if s[i] in 'aeiou': const = set() c = 0 else: c += 1 const.add(s[i]) if c>=3 and len(const) >= 2: ans.append(s[start:i]) start = i const = set({s[i]}) c = 1 ans.append(s[start:]) print(' '.join(ans)) ```
instruction
0
92,311
6
184,622
Yes
output
1
92,311
6
184,623
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` a = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"] b = input() n = 0 g = 0 s = [] check = False print(b[0], end="") for i in range(1, len(b) - 1): if check: print(b[i], end="") check = False continue m = [b[i-1], b[i], b[i+1]] if b[i-1] in a and b[i] in a and b[i+1] in a and len(list(set(m))) > 1: print(b[i], end=" ") check = True else: print(b[i], end="") if len(b) > 1: print(b[-1]) ```
instruction
0
92,312
6
184,624
Yes
output
1
92,312
6
184,625
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` s = input() num = 0 res = "" A = ["a", "e", "i", "o", "u"] for i in s: num+=1 if len(res) > 1 and i == res[len(res) - 1] == res[len(res) - 2]: num = 2 if i in A: num = 0 if num >= 3: res += " " num = 1 res+=i print(res) ```
instruction
0
92,313
6
184,626
Yes
output
1
92,313
6
184,627
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` inp = input() out = '' c = 0 d = 0 for i in range(len(inp)): if inp[i] == 'a' or inp[i] == 'e' or inp[i] == 'i' or inp[i] == 'o' or inp[i] == 'u': c = 0 d = 0 elif c >= 2: if d or inp[i] != inp[i - 1]: out += " " c = 1 d = 0 else: c += 1 else: if c > 0 and inp[i] != inp[i-1]: d = 1 c += 1 out += inp[i] print(out) ```
instruction
0
92,314
6
184,628
Yes
output
1
92,314
6
184,629
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` def Dif(string): for i in range(len(string)): if s[i] != s[-i - 1]: return True return False s = input() s1 = '' b = False for i in range(len(s)): if s[i] != 'a' and s[i] != 'e' and s[i] != 'i' and s[i] != 'o' and s[i] != 'u': s1 += s[i] if len(s1) > 2 and Dif(s1): b = True break else: s1 = '' if b: s1 = '' s2 = '' for i in range(len(s)): if s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u': s1 += s[i] s2 = '' else: s2 += s[i] s1 += s[i] if len(s2) > 1 and Dif(s2): s1 += ' ' s2 = '' s = s1 print(s) ```
instruction
0
92,315
6
184,630
No
output
1
92,315
6
184,631
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` s=input() c=0 d=0 p='' for i in s: if i=='a'or i=='e' or i=='i' or i=='o' or i=='u': c=0 d=0 p='' else: c+=1 if c>1: if p!='' and i not in p: d=1 p+=i if c>=3 and d: print(' ',end='') c=0 d=0 p='' print(i,end='') ```
instruction
0
92,316
6
184,632
No
output
1
92,316
6
184,633
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` s = input() g = {'a', 'e', 'i', 'o', 'u'} l = 0 lc = "" for ch in s: if ch not in g and lc != ch: l+=1 else: l=0 if l>2 and lc != ch: print(" ", end="") l = 1 lc = ch print(ch, end="") ```
instruction
0
92,317
6
184,634
No
output
1
92,317
6
184,635
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them. Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block. For example: * the following words have typos: "hellno", "hackcerrs" and "backtothefutttture"; * the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos. Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem. Input The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters. Output Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them. Examples Input hellno Output hell no Input abacaba Output abacaba Input asdfasdf Output asd fasd f Submitted Solution: ``` s=input() l='' k=0 o=1 p=0 new=s a=['a', 'e', 'i', 'o','u'] for i in range(len(s)): if s[i] not in a: if s[i]!=l: if o>=2: k+=2 l=s[i] else: k+=1 l=s[i] o=1 else: o+=1 if k==3: new=new[:i+p]+' '+new[i+p:] p+=1 k=1 else: k=0 l='' o=1 print(new) ```
instruction
0
92,318
6
184,636
No
output
1
92,318
6
184,637
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,806
6
185,612
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` def ok(a,b,n): for i in range(n): if(a[i]>b[i]): return False return True MAX = 100001 parent = [0] * MAX Rank = [0] * MAX def find(x): if parent[x] == x: return x else: return find(parent[x]) def merge(r1, r2): if (r1 != r2): if (Rank[r1] > Rank[r2]): parent[r2] = r1 Rank[r1] += Rank[r2] else: parent[r1] = r2 Rank[r2] += Rank[r1] def minimumOperations(s1, s2): for i in range(1, 26 + 1): parent[i] = i Rank[i] = 1 ans = [] for i in range(len(s1)): if (s1[i] != s2[i]): if (find(ord(s1[i]) - 96) != find(ord(s2[i]) - 96)): x = find(ord(s1[i]) - 96) y = find(ord(s2[i]) - 96) merge(x, y) ans.append([s1[i], s2[i]]) print(len(ans)) for _ in range(int(input())): n = int(input()) s = [i for i in input()] t = [i for i in input()] if(ok(s,t,n)): minimumOperations("".join(s),"".join(t)) else: print(-1) ```
output
1
92,806
6
185,613
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,807
6
185,614
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` T=int(input()) for _ in range(T): n=int(input()) a=input() b=input() flag=0 for i in range(n): if (a[i]>b[i]): flag=1 break if (flag==1): print(-1) else: G={} for i in range(97,97+20): G[chr(i)]=[] for i in range(n): if (a[i]!=b[i]): G[a[i]].append(b[i]) G[b[i]].append(a[i]) v=0 vis=[-1 for i in range(20)] for i in range(20): if (vis[i]==-1): A=[chr(97+i)] T1=[] while(len(A)!=0): a=A.pop() vis[ord(a)-97]=0 T1.append(a) for i in G[a]: if (vis[ord(i)-97]==-1): vis[ord(i)-97]=0 A.append(i) v=v+len(T1)-1 print(v) ```
output
1
92,807
6
185,615
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,808
6
185,616
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` def x(): n = int(input()) ans = 0 A = list(input()) B = list(input()) G = [] ver = set() for i in range(n): a = ord(A[i]) - ord('a') b = ord(B[i]) - ord('a') ver.add(a) ver.add(b) if a <= b: G.append([a, b]) else: print(-1) return 0 G.sort() res = [] tree_id = [i for i in range(20)] for i in range(n): a = G[i][0] b = G[i][1] if tree_id[a] != tree_id[b]: res.append((a, b)) old_id = tree_id[b] new_id = tree_id[a] for j in range(20): if tree_id[j] == old_id: tree_id[j] = new_id print(len(res)) for q in range(int(input())): x() ```
output
1
92,808
6
185,617
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,809
6
185,618
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` def found(x): global uf temp = x while uf[x] != x: x = uf[x] ans = x while uf[temp] != temp: parent = uf[temp] uf[temp] = ans temp = parent return ans inp = lambda : list(map(lambda x: ord(x) - 97, list(input()))) t = int(input()) for _ in range(t): n = int(input()) a = inp() b = inp() res = 0 for i in range(n): if a[i] > b[i]: res = -1 break if res == -1: print(-1) continue edges = [[] for _ in range(20)] for i in range(n): if a[i] != b[i] and not b[i] in edges[a[i]]: edges[a[i]].append(b[i]) uf = [i for i in range(20)] for i in range(20): if len(edges[i]) == 0: continue for j in edges[i]: temp1 = found(i) temp2 = found(j) if temp1 != temp2: uf[temp1] = temp2 res += 1 print(res) ```
output
1
92,809
6
185,619
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,810
6
185,620
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` import sys import math import heapq import bisect from collections import Counter from collections import defaultdict from io import BytesIO, IOBase import string class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os 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 self.BUFSIZE = 8192 def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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 = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def get_int(): return int(input()) def get_ints(): return list(map(int, input().split(' '))) def get_int_grid(n): return [get_ints() for _ in range(n)] def get_str(): return input().split(' ') def yes_no(b): if b: return "YES" else: return "NO" def binary_search(good, left, right, delta=1, right_true=False): """ Performs binary search ---------- Parameters ---------- :param good: Function used to perform the binary search :param left: Starting value of left limit :param right: Starting value of the right limit :param delta: Margin of error, defaults value of 1 for integer binary search :param right_true: Boolean, for whether the right limit is the true invariant :return: Returns the most extremal value interval [left, right] which is good function evaluates to True, alternatively returns False if no such value found """ limits = [left, right] while limits[1] - limits[0] > delta: if delta == 1: mid = sum(limits) // 2 else: mid = sum(limits) / 2 if good(mid): limits[int(right_true)] = mid else: limits[int(~right_true)] = mid if good(limits[int(right_true)]): return limits[int(right_true)] else: return False def prefix_sums(a, drop_zero=False): p = [0] for x in a: p.append(p[-1] + x) if drop_zero: return p[1:] else: return p def prefix_mins(a, drop_zero=False): p = [float('inf')] for x in a: p.append(min(p[-1], x)) if drop_zero: return p[1:] else: return p class DSU: # Disjoint Set Union (Union-Find) Data Structure def __init__(self, nodes): # Parents self.p = {i: i for i in nodes} # Ranks self.r = {i: 0 for i in nodes} # Sizes self.s = {i: 1 for i in nodes} def get(self, u): # Recursive Returns the identifier of the set that contains u, includes path compression if u != self.p[u]: self.p[u] = self.get(self.p[u]) return self.p[u] def union(self, u, v): # Unites the sets with identifiers u and v u = self.get(u) v = self.get(v) if u != v: if self.r[u] > self.r[v]: u, v = v, u self.p[u] = v if self.r[u] == self.r[v]: self.r[v] += 1 self.s[v] += self.s[u] def solve(): n = get_int() s1 = input().strip() s2 = input().strip() G = defaultdict(list) for letter in list(string.ascii_lowercase)[:20]: top_set = set(s2[i] for i in range(n) if s1[i] == letter) if len(top_set) == 0: pass else: for item in top_set: if item < letter: return -1 elif item > letter: G[item].append(letter) G[letter].append(item) dsu = DSU(G.keys()) for x in G: for y in G[x]: dsu.union(x, y) S = 0 for x in G: if dsu.p[x] == x: S += dsu.s[x] - 1 return S t = get_int() for _ in range(t): print(solve()) ```
output
1
92,810
6
185,621
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,811
6
185,622
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` t=int(input()) for j in range(t): n=int(input()) a=input() b=input() # First constructing graph gr=dict(list()) flag=0 for i in range(n): if ord(a[i])>ord(b[i]): flag=1 break if gr.get(a[i],0)==0: gr[a[i]]=[] if gr.get(b[i],0)==0: gr[b[i]]=[] gr[a[i]].append(b[i]) gr[b[i]].append(a[i]) if flag==1: print(-1) else: # print(gr) ans=0 visited=[0 for i in range(20)] def dfs(ii): visited[ii-ord('a')]=1 # fgh=gr.get(chr(ii)) for j in gr.get(chr(ii),[]): if visited[ord(j)-ord('a')]==0: dfs(ord(j)) for i in range(ord('a'),ord('t')+1): if visited[i-ord('a')]!=1: dfs(i) # print(chr(i)) ans+=1 print(20-ans) ```
output
1
92,811
6
185,623
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,812
6
185,624
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` import sys input = sys.stdin.readline for _ in range(int(input())): n, a, b, ans = int(input()), [char for char in input().strip()], [char for char in input().strip()], 0 for i in range(n): if a[i] > b[i]: ans = -1 if ans != -1: for char in "abcdefghijklmnopqrst": s = set() for i in range(n): if char == b[i] and a[i] < b[i]: s.add(a[i]) # need to convert from for i in range(n): if a[i] in s: a[i] = char ans += len(s) print(ans) ```
output
1
92,812
6
185,625
Provide tags and a correct Python 3 solution for this coding contest problem. Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions p_1, p_2, …, p_k (k β‰₯ 1; 1 ≀ p_i ≀ n; p_i β‰  p_j if i β‰  j) of A such that A_{p_1} = A_{p_2} = … = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). 2. selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). 3. sets each letter in positions p_1, p_2, …, p_k to letter y. More formally: for each i (1 ≀ i ≀ k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A. Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains t (1 ≀ t ≀ 10) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 10^5) β€” the length of strings A and B. The second line of each test case contains string A (|A|=n). The third line of each test case contains string B (|B|=n). Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other (A = B) or -1 if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Output 2 -1 3 2 -1 Note * In the 1-st test case Koa: 1. selects positions 1 and 2 and sets A_1 = A_2 = b (\color{red}{aa}b β†’ \color{blue}{bb}b). 2. selects positions 2 and 3 and sets A_2 = A_3 = c (b\color{red}{bb} β†’ b\color{blue}{cc}). * In the 2-nd test case Koa has no way to make string A equal B. * In the 3-rd test case Koa: 1. selects position 1 and sets A_1 = t (\color{red}{a}bc β†’ \color{blue}{t}bc). 2. selects position 2 and sets A_2 = s (t\color{red}{b}c β†’ t\color{blue}{s}c). 3. selects position 3 and sets A_3 = r (ts\color{red}{c} β†’ ts\color{blue}{r}).
instruction
0
92,813
6
185,626
Tags: dsu, graphs, greedy, sortings, strings, trees, two pointers Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=list(input()) b=list(input()) for i in range(n): if a[i]>b[i]: print(-1) break else: s=sorted(list(set(a)|set(b))) ans=0 for j in s: f=False x=set() for k in range(n): if a[k]==j and a[k]!=b[k]: x.add(b[k]) f=True x=sorted(list(x)) for k in range(n): if a[k]==j and a[k]!=b[k]: a[k]=x[0] if f:ans+=1 print(ans) ```
output
1
92,813
6
185,627
Provide tags and a correct Python 3 solution for this coding contest problem. If you have ever interacted with a cat, you have probably noticed that they are quite particular about how to pet them. Here is an approximate map of a normal cat. <image> However, some cats won't tolerate this nonsense from the humans. Here is a map of a grumpy cat. <image> You have met a cat. Can you figure out whether it's normal or grumpy? Interaction This is an interactive problem. Initially you're not given any information about the cat. Instead, the cat is divided into ten areas, indexed from 0 to 9. In one query you can choose which area you'll pet and print the corresponding index to standard out. You will get the cat's response, as depicted on the corresponding map, via standard in. For simplicity all responses are written in lowercase. Once you're certain what type of cat you're dealing with, output "normal" or "grumpy" to standard out. Note Please make sure to use the stream flushing operation after each query in order not to leave part of your output in some buffer.
instruction
0
93,216
6
186,432
Tags: brute force, interactive Correct Solution: ``` G = {"no.":3, "eve":1, "are":1, "wor":1, "ter":1, "god":1, "now":2} N = {"no.":5, "gre":1, "coo":1, "thi":1, "not":1, "tou":1} from collections import defaultdict as dd, deque C = dd(int) S = set() for i in range(10): print(i) s = input()+'........' s = s.replace(' ','').replace("don't", "") s = s[:3] C[s] += 1 if s not in G or C[s]>G[s]: print('normal') break if s not in N or C[s]>N[s]: print('grumpy') break ```
output
1
93,216
6
186,433
Provide tags and a correct Python 3 solution for this coding contest problem. If you have ever interacted with a cat, you have probably noticed that they are quite particular about how to pet them. Here is an approximate map of a normal cat. <image> However, some cats won't tolerate this nonsense from the humans. Here is a map of a grumpy cat. <image> You have met a cat. Can you figure out whether it's normal or grumpy? Interaction This is an interactive problem. Initially you're not given any information about the cat. Instead, the cat is divided into ten areas, indexed from 0 to 9. In one query you can choose which area you'll pet and print the corresponding index to standard out. You will get the cat's response, as depicted on the corresponding map, via standard in. For simplicity all responses are written in lowercase. Once you're certain what type of cat you're dealing with, output "normal" or "grumpy" to standard out. Note Please make sure to use the stream flushing operation after each query in order not to leave part of your output in some buffer.
instruction
0
93,217
6
186,434
Tags: brute force, interactive Correct Solution: ``` import sys for i in range(10): print(i) sys.stdout.flush() a = sys.stdin.readline().strip().lower() if "cool" in a or "great" in a or "not bad" in a or "think" in a: print("normal") sys.exit(0) if "die" in a or "serious" in a or "way" in a or "worse" in a or "terrible" in a or "even" in a: print("grumpy") sys.exit(0) ```
output
1
93,217
6
186,435
Provide tags and a correct Python 3 solution for this coding contest problem. If you have ever interacted with a cat, you have probably noticed that they are quite particular about how to pet them. Here is an approximate map of a normal cat. <image> However, some cats won't tolerate this nonsense from the humans. Here is a map of a grumpy cat. <image> You have met a cat. Can you figure out whether it's normal or grumpy? Interaction This is an interactive problem. Initially you're not given any information about the cat. Instead, the cat is divided into ten areas, indexed from 0 to 9. In one query you can choose which area you'll pet and print the corresponding index to standard out. You will get the cat's response, as depicted on the corresponding map, via standard in. For simplicity all responses are written in lowercase. Once you're certain what type of cat you're dealing with, output "normal" or "grumpy" to standard out. Note Please make sure to use the stream flushing operation after each query in order not to leave part of your output in some buffer.
instruction
0
93,222
6
186,444
Tags: brute force, interactive Correct Solution: ``` #print('grumpy') from sys import exit import sys a = ['no', 'no', 'no', 'no', 'no', "don'",'grea',"don'",'not ','cool'] b = ['no','no','no', 'no w', 'no w','go d','wors','terr', "don'", 'are '] for i in range(9): sys.stdout.write(str(i)) sys.stdout.write('\n') sys.stdout.flush() inp = input() if (inp.__len__()>4): inp = inp[:4] if inp in a: a.remove(inp) else : print('grumpy') exit() if inp in b: b.remove(inp) else: print('normal') exit() sys.stdout.write('normal') #sys.stdout.write('\n') sys.stdout.flush() exit() for i in range(10): print(i) a = input() if (a.__len__() > 2): if a[-2:] == 'en': print('grumpy') exit() print('normal') ```
output
1
93,222
6
186,445
Provide tags and a correct Python 3 solution for this coding contest problem. If you have ever interacted with a cat, you have probably noticed that they are quite particular about how to pet them. Here is an approximate map of a normal cat. <image> However, some cats won't tolerate this nonsense from the humans. Here is a map of a grumpy cat. <image> You have met a cat. Can you figure out whether it's normal or grumpy? Interaction This is an interactive problem. Initially you're not given any information about the cat. Instead, the cat is divided into ten areas, indexed from 0 to 9. In one query you can choose which area you'll pet and print the corresponding index to standard out. You will get the cat's response, as depicted on the corresponding map, via standard in. For simplicity all responses are written in lowercase. Once you're certain what type of cat you're dealing with, output "normal" or "grumpy" to standard out. Note Please make sure to use the stream flushing operation after each query in order not to leave part of your output in some buffer.
instruction
0
93,223
6
186,446
Tags: brute force, interactive Correct Solution: ``` normal = ["no"] * 5 + ["cool", "not bad", "great!", "don't touch me!", "don't think so"] grumpy = ["no"] * 3 + ["no way"] * 2 + ["terrible", "worse", "go die in a hole", "don't even", "are you serious?"] import sys import re sys.stdout.flush() normal = [re.sub("('|!|\?| )", "", x).lower() for x in normal] grumpy = [re.sub("('|!|\?| )", "", x).lower() for x in grumpy] cat = None counter = 0 while cat is None: print(counter) sys.stdout.flush() text = re.sub("('|!|\?| )", "", input()).lower() if text in normal: normal.remove(text) else: cat = "grumpy" if text in grumpy: grumpy.remove(text) else: cat = "normal" counter += 1 print(cat) ```
output
1
93,223
6
186,447
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,078
6
188,156
Tags: implementation Correct Solution: ``` x=input() z=input().split() mx=0 for i in z: pres=0 for a in i: if a>='A' and a<='Z': pres+=1 if mx < pres: mx=pres print(mx) ```
output
1
94,078
6
188,157
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,079
6
188,158
Tags: implementation Correct Solution: ``` n = int(input()) s = input().split(' ') ans = 0 for words in s: c = 0 for ch in words: if (ch >= 'A' and ch <= 'Z'): c += 1 ans = max(ans, c) print(ans) ```
output
1
94,079
6
188,159
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,080
6
188,160
Tags: implementation Correct Solution: ``` big_letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] max_n = 0 n = int(input()) a = input().split(" ") for el in a: n = 0 for i in el: if i in big_letters: n += 1 if n > max_n: max_n = n print(max_n) ```
output
1
94,080
6
188,161
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,081
6
188,162
Tags: implementation Correct Solution: ``` n = int(input()) text = input().split() ans = 0 for word in text: cur_ans = 0 for let in word: if let >= "A" and let <= "Z": cur_ans+=1 ans = max(ans,cur_ans) print(ans) ```
output
1
94,081
6
188,163
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,082
6
188,164
Tags: implementation Correct Solution: ``` """ You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. """ def volumetext(length, sentence): maxvol=0 vol=0 for word in sentence: vol=0 for letter in word: if letter.upper()==letter: vol+=1 if vol>maxvol: maxvol=vol return maxvol length=(map(int, input().strip().split())) sentence=input().strip().split() print(volumetext(length,sentence)) ```
output
1
94,082
6
188,165
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,083
6
188,166
Tags: implementation Correct Solution: ``` n =int(input()) capital= ['A','B','C','D','E','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] linha= input().split() sum_max=0 for palavra in linha: s=0 for ch in palavra: if ch in capital: s+=1 if s>sum_max: sum_max=s print(sum_max) ```
output
1
94,083
6
188,167
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,084
6
188,168
Tags: implementation Correct Solution: ``` n = input() s = input() res = 0 mx = 0 for i in s.split(): for j in i: if j.isupper(): res += 1 mx = max(res, mx) res = 0 print (mx) ```
output
1
94,084
6
188,169
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. Input The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text. The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters. Output Print one integer number β€” volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
instruction
0
94,085
6
188,170
Tags: implementation Correct Solution: ``` n=int(input()) x=list(input().split()) ma=0 for i in x: ca=0 for a in i: if 'A'<=a<='Z': ca+=1 if ca>ma: ma=ca print(ma) ```
output
1
94,085
6
188,171
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
instruction
0
95,738
6
191,476
Tags: dp Correct Solution: ``` from math import inf from sys import stdin input = stdin.readline s,k = input().split() n = len(s) k = int(k) ch = [[0]*26 for _ in range(26)] for _ in range(int(input())): a,b,c = input().split() ch[ord(a)-97][ord(b)-97] = int(c) dp = [[[-inf]*26 for _ in range(k+2)] for _ in range(n)] dp[0][0][ord(s[0])-97] = 0 for i in range(26): if ord(s[0])-97 != i: dp[0][1][i] = 0 for i in range(n-1): for j in range(k+1): for m in range(26): for n in range(26): if ord(s[i+1])-97 != n: dp[i+1][j+1][n] = max(dp[i+1][j+1][n],dp[i][j][m]+ch[m][n]) else: dp[i+1][j][n] = max(dp[i+1][j][n],dp[i][j][m]+ch[m][n]) print(max(map(max,dp[-1][:-1]))) ```
output
1
95,738
6
191,477
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
instruction
0
95,739
6
191,478
Tags: dp Correct Solution: ``` from math import inf s,k= input().split() k=int(k) dict=[[0]*26 for i in range(26)] for i in range(int(input())): x=input().split() # print(ord(x[0])-97,ord(x[1])-97) dict[ord(x[0])-97][ord(x[1])-97]=int(x[2]) dp=[[[-inf]*26 for j in range(k+2)]for i in range(len(s))] m=-1 for i in range(26): if ord(s[0])-97==i: dp[0][0][i]=0 else: dp[0][1][i]=0 m=-1 for i in range(1,len(s)): for j in range(k+1): for p in range(26): if ord(s[i])-97==p: for xx in range(26): dp[i][j][p]=max(dp[i-1][j][xx]+dict[xx][p],dp[i][j][p]) else: for xx in range(26): dp[i][j+1][p]=max(dp[i-1][j][xx]+dict[xx][p],dp[i][j+1][p]) print(max(map(max, dp[-1][:-1]))) ```
output
1
95,739
6
191,479
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
instruction
0
95,740
6
191,480
Tags: dp Correct Solution: ``` import string s, k = input().split() max_changes = int(k) len_s = len(s) alpha = string.ascii_lowercase bonuses = { ch: { ch: 0 for ch in alpha } for ch in alpha } for i in range(int(input())): a, b, x = input().split() bonuses[a][b] = int(x) def improve(current, changed, a, b, score): if b not in current[changed] or current[changed][b] < score: current[changed][b] = score #print('%c: current[%d][%c] <- %d' % (a, changed, b, score)) current = [ { s[0]: 0 }, { ch: 0 for ch in alpha } ] for pos in range(1, len_s): previous = current len_current = min(len(previous) + 1, max_changes + 1) current = [ {} for i in range(len_current) ] for changed, scores in enumerate(previous): for a, score in scores.items(): for b, bonus in bonuses[a].items(): if b == s[pos]: if changed < len(current): improve(current, changed, a, b, score + bonus) elif changed + 1 < len(current): improve(current, changed + 1, a, b, score + bonus) best = -(10 ** 5) for changed, scores in enumerate(current): for score in scores.values(): best = max(best, score) print(best) ```
output
1
95,740
6
191,481
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
instruction
0
95,741
6
191,482
Tags: dp Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from math import inf def main(): s,k=input().split() k=int(k) a=[[0 for _ in range(26)] for _ in range(26)] n=len(s) for _ in range(int(input())): b=input().split() a[ord(b[0])-97][ord(b[1])-97]=int(b[2]) dp=[[[-inf for _ in range(k+1)] for _ in range(26)] for _ in range(n)] z=ord(s[0])-97 if k>0: for i in range(26): dp[0][i][int(i!=z)]=0 else: dp[0][z][0]=0 for i in range(1,n): for j in range(26): for l in range(k+1): if j==ord(s[i])-97: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l]+a[m][j],dp[i][j][l]) elif l!=0: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l-1]+a[m][j],dp[i][j][l]) ma=-inf for j in range(26): for l in range(k+1): ma=max(ma,dp[n-1][j][l]) print(ma) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
output
1
95,741
6
191,483
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36.
instruction
0
95,742
6
191,484
Tags: dp Correct Solution: ``` import math R = lambda: map(int, input().split()) s, k = input().split() k = int(k) g = [[0 for j in range(30)] for i in range(30)] for i in range(int(input())): f, t, sc = input().split() f, t, sc = ord(f) - ord('a'), ord(t) - ord('a'), int(sc) g[f][t] = sc n = len(s) dp = [[[-math.inf for kk in range(30)] for j in range(k + 1)] for i in range(n)] for c in range(26): for j in range(k + 1): dp[0][j][c] = 0 for i in range(1, n): cir, cpr = ord(s[i]) - ord('a'), ord(s[i - 1]) - ord('a') dp[i][0][cir] = max(dp[i][0][cir], dp[i - 1][0][cpr] + g[cpr][cir]) for j in range(1, k + 1): for ci in range(26): for cj in range(26): if j == 1 and ci != cir and cj != cpr: continue dp[i][j][ci] = max(dp[i][j][ci], dp[i - 1][j - (ci != cir)][cj] + g[cj][ci]) print(max(dp[n - 1][j][i] for j in range(k + 1) for i in range(26))) ```
output
1
95,742
6
191,485
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36. Submitted Solution: ``` # Beautiful Words (DP) # R Visser # Python 3 # 8 Sep 2016 # Solution is O(n*k*h^2) (where h = 26) INF = 1000000000 # Input inp = input().split() s = inp[0] k = int(inp[1]) n = int(input()) # Initialise bonus bonus = [[0 for j in range(26)] for i in range(26)] # Input bonus for i in range(n): inp = input().split() x = inp[0] y = inp[1] c = int(inp[2]) bonus[ord(x)-ord('a')][ord(y)-ord('a')] = c # Convert word to list of numbers from 0 to 25 word = [] for i in range(len(s)): word.append(ord(s[i])-ord('a')) # Initialise DP dp = [[[-INF for w in range(27)] for j in range(k+2)] for i in range(len(s)+1)] dp[0][0][0] = 0 for i in range(len(s)): for j in range(k+1): for u in range(26): for w in range(26): # Check if letter equal if (w == word[i]): dp[i+1][j][w] = max(dp[i+1][j][w], dp[i][j][u] + bonus[u][w]) else: dp[i+1][j+1][w] = max(dp[i+1][j+1][w], dp[i][j][u] + bonus[u][w]) # Obtain solution answer = -INF for i in range(0, k+1): for j in range(0, 26): answer = max(answer, dp[len(s)][i][j]) # Output solution print(answer) ```
instruction
0
95,744
6
191,488
No
output
1
95,744
6
191,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36. Submitted Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import defaultdict from heapq import * from math import inf def main(): s,k=input().split() k=int(k) a=[[0 for _ in range(26)] for _ in range(26)] n=int(input()) for _ in range(n): b=input().split() a[ord(b[0])-97][ord(b[1])-97]=int(b[2]) n=len(s) dp=[[[-inf for _ in range(k+2)] for _ in range(26)] for _ in range(n)] z=ord(s[0])-97 for i in range(26): dp[0][i][int(i!=z)]=0 for i in range(1,n): for j in range(26): for l in range(k+1): if j==ord(s[i])-97: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l]+a[m][j],dp[i][j][l]) elif l!=0: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l-1]+a[m][j],dp[i][j][l]) ma=0 for i in range(n): for j in range(26): for l in range(k+1): ma=max(ma,dp[i][j][l]) print(ma) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
instruction
0
95,745
6
191,490
No
output
1
95,745
6
191,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β€” non-empty string s, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than k letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters x and y (x immediately precedes y) the bonus c(x, y) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most k letters in the name of the Vasya's character. Input The first line contains character's name s and an integer number k (0 ≀ k ≀ 100). The length of the nonempty string s does not exceed 100. The second line contains an integer number n (0 ≀ n ≀ 676) β€” amount of pairs of letters, giving bonus to the euphony. The next n lines contain description of these pairs Β«x y cΒ», which means that sequence xy gives bonus c (x, y β€” lowercase Latin letters, - 1000 ≀ c ≀ 1000). It is guaranteed that no pair x y mentioned twice in the input data. Output Output the only number β€” maximum possible euphony ΠΎf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Output 36 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Output 20 Note In the first example the most euphony name will be looser. It is easy to calculate that its euphony is 36. Submitted Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import defaultdict from heapq import * from math import inf def main(): s,k=input().split() k=int(k) a=[[0 for _ in range(26)] for _ in range(26)] n=int(input()) for _ in range(n): b=input().split() a[ord(b[0])-97][ord(b[1])-97]=int(b[2]) n=len(s) dp=[[[-inf for _ in range(k+1)] for _ in range(26)] for _ in range(n)] z=ord(s[0])-97 if k>0: for i in range(26): dp[0][i][int(i!=z)]=0 for i in range(1,n): for j in range(26): for l in range(k+1): if j==ord(s[i])-97: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l]+a[m][j],dp[i][j][l]) elif l!=0: for m in range(26): dp[i][j][l]=max(dp[i-1][m][l-1]+a[m][j],dp[i][j][l]) ma=0 for i in range(n): for j in range(26): for l in range(k+1): ma=max(ma,dp[i][j][l]) print(ma) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
instruction
0
95,746
6
191,492
No
output
1
95,746
6
191,493
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,260
6
192,520
Tags: implementation Correct Solution: ``` command = input() act = ['H', 'Q', '9'] x = 0 for el in command: if el in act: print('YES') x = 1 break if x == 0: print('NO') ```
output
1
96,260
6
192,521
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,262
6
192,524
Tags: implementation Correct Solution: ``` #!/usr/local/bin/python3 string = input() if ('H' in string) or ('Q' in string) or ('9' in string): print("YES") else: print("NO") ```
output
1
96,262
6
192,525
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,264
6
192,528
Tags: implementation Correct Solution: ``` n=input() if '9' in n or 'H' in n or 'Q' in n: print('YES') else: print('NO') ```
output
1
96,264
6
192,529
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,265
6
192,530
Tags: implementation Correct Solution: ``` str=input("") c=0 for i in str: if i=='H' or i=='Q' or i=='9': c+=1 if c>0: print("YES") else: print("NO") ```
output
1
96,265
6
192,531
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,266
6
192,532
Tags: implementation Correct Solution: ``` print('YES' if set('HQ9') & set(input()) else 'NO') ```
output
1
96,266
6
192,533
Provide tags and a correct Python 3 solution for this coding contest problem. HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise. Examples Input Hi! Output YES Input Codeforces Output NO Note In the first case the program contains only one instruction β€” "H", which prints "Hello, World!". In the second case none of the program characters are language instructions.
instruction
0
96,267
6
192,534
Tags: implementation Correct Solution: ``` x= input() y=True for i in range (len(x)): if x[i] in "HQ9": print("YES") y= False break if y: print("NO") ```
output
1
96,267
6
192,535
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,372
6
192,744
Tags: dp, greedy, implementation Correct Solution: ``` s = input() a = [] for i, c in enumerate(s): if c == '.': a.append(i) n = len(s) if not a or not 1 <= a[0] <= 8 or not 1 <= n - a[-1] - 1 <= 3: print('NO') elif not all(2 <= abs(y - x - 1) <= 11 for x, y in zip(a, a[1:])): print('NO') else: print('YES') st = 0 for i in range(len(a) - 1): ed = a[i] + (1 if a[i + 1] - a[i] - 1 < 4 else 3) + 1 print(s[st: ed]) st = ed if st < n: print(s[st:]) ```
output
1
96,372
6
192,745
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,373
6
192,746
Tags: dp, greedy, implementation Correct Solution: ``` s = input() words = s.split('.') n = s.count('.') numberOfWordsWithSizeLessThanTwo = len([x for x in words[1:-1] if len(x) < 2]) numberOfWordsWithSizeMoreThanEleven = len([x for x in words[1:-1] if len(x) > 11]) if len(words) == 1: print('NO') elif len(words[0]) < 1 or len(words[0]) > 8 or len(words[-1]) < 1 or len(words[-1]) > 3 or numberOfWordsWithSizeLessThanTwo != 0 or numberOfWordsWithSizeMoreThanEleven != 0: print('NO') else: s0 = words[0] sn = words[-1] s1 = '' s2 = '' print('YES') for word in words[1:-1]: if len(word) > 9: s2 = word[-8:] s1 = word[:-8] else: s1 = word[0] s2 = word[1:] res = s0 + '.' + s1 print(res) s0 = s2 print(s0 + '.' + sn) ```
output
1
96,373
6
192,747
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,374
6
192,748
Tags: dp, greedy, implementation Correct Solution: ``` s = input() v = s.split(sep='.') flen = len(v[0]) llen = len(v[-1]) #print(v) if len(v) < 2 : print('NO') elif not (flen >= 1 and flen <= 8) : print('NO') elif not (llen >= 1 and llen <= 3) : print('NO') else : for i in v[1:-1] : if not (len(i) >= 2 and len(i) <= 11) : #cant make a valid file print('NO') exit() print('YES') print(v[0], end='') print('.', end='') for i in v[1:-1] : mi = min(3, len(i) - 1) print(i[0:mi]) print(i[mi:len(i)], end = '') print('.', end = '') print(v[-1]) ```
output
1
96,374
6
192,749
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,375
6
192,750
Tags: dp, greedy, implementation Correct Solution: ``` s = input() v = s.split(sep='.') flen = len(v[0]) llen = len(v[-1]) if len(v) < 2 : print('NO') elif not (flen >= 1 and flen <= 8) : print('NO') elif not (llen >= 1 and llen <= 3) : print('NO') else : for i in v[1:-1] : if not (len(i) >= 2 and len(i) <= 11) : print('NO') exit() print('YES') print(v[0], end='') print('.', end='') for i in v[1:-1] : mi = min(3, len(i) - 1) print(i[0:mi]) print(i[mi:len(i)], end = '') print('.', end = '') print(v[-1]) ```
output
1
96,375
6
192,751
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,376
6
192,752
Tags: dp, greedy, implementation Correct Solution: ``` s = input() if s[0] == "." or s[-1] == ".": print("NO") quit() s = s.split(".") if len(s) == 1: print("NO") quit() if not 1 <= len(s[-1]) <= 3 or not 1 <= len(s[0]) <= 8: print("NO") quit() arr = [s[-1]] s.pop() n = len(s) for i in range(n - 1, -1, -1): p = len(s[i]) if i > 0 and not 2 <= p <= 11: print("NO") quit() for k in range(1, 4): if i > 0: if 1 <= p - k <= 8: arr[-1] = s[i][k:] + "." + arr[-1] arr.append(s[i][:k]) break else: arr[-1] = s[0] + "." + arr[-1] break print("YES") for item in arr[::-1]: print(item) ```
output
1
96,376
6
192,753
Provide tags and a correct Python 3 solution for this coding contest problem. Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; * ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string s, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide s into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in s. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip Output NO
instruction
0
96,377
6
192,754
Tags: dp, greedy, implementation Correct Solution: ``` import sys def solve(): a = input().split('.') if len(a) < 2: print("NO") return res = [[""] * 2 for _ in range(len(a) - 1)] res[0][0] = a[0] res[len(res) - 1][1] = a[-1] for i in range(1, len(a) - 1): if len(a[i]) < 2: print("NO") return extension = min(3, len(a[i]) - 1) res[i - 1][1] = a[i][:extension] res[i][0] = a[i][extension:] for f, s in res: if len(f) < 1 or len(f) > 8 or len(s) < 1 or len(s) > 3: print("NO") return print("YES") print('\n'.join('.'.join(entry) for entry in res)) def prt(l): return print(' '.join(l)) def rv(): return map(int, input().split()) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
96,377
6
192,755