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.
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.
First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l, r (1 β€ l β€ i β€ r β€ |w|) such that substring w[l ... r] is contained in the collection s1, s2, ..., sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.
Input
The first line contains the only integer n (1 β€ n β€ 100) β the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.
Output
Output the only line β Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1 β€ i β€ |a|), that ai < bi, and for any j (1 β€ j < i) aj = bj. |a| stands for the length of string a.
Examples
Input
3
bers
ucky
elu
PetrLoveLuckyNumbers
t
Output
PetrLovtTttttNumtttt
Input
4
hello
party
abefglghjdhfgj
IVan
petrsmatchwin
a
Output
petrsmatchwin
Input
2
aCa
cba
abAcaba
c
Output
abCacba | instruction | 0 | 46,813 | 6 | 93,626 |
Tags: implementation, strings
Correct Solution:
```
import sys
from math import *
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def same(a, b):
return a.lower() == b.lower()
n = mint()
w = []
for i in range(n):
w.append(minp().lower())
s = minp()
g = [False]*len(s)
c = minp()
for i in w:
for j in range(len(s)-len(i)+1):
if s[j:j+len(i)].lower() == i:
for k in range(len(i)):
g[j+k] = True
r = ''
for i in range(len(s)):
if g[i]:
x = s[i]
if x == c.lower():
if c.lower() == 'a':
r += 'b'
else:
r += 'a'
elif x == c.upper():
if c.lower() == 'a':
r += 'B'
else:
r += 'A'
elif x.islower():
r += c.lower()
else:
r += c.upper()
else:
r += s[i]
print(r)
``` | output | 1 | 46,813 | 6 | 93,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.
First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l, r (1 β€ l β€ i β€ r β€ |w|) such that substring w[l ... r] is contained in the collection s1, s2, ..., sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.
Input
The first line contains the only integer n (1 β€ n β€ 100) β the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.
Output
Output the only line β Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1 β€ i β€ |a|), that ai < bi, and for any j (1 β€ j < i) aj = bj. |a| stands for the length of string a.
Examples
Input
3
bers
ucky
elu
PetrLoveLuckyNumbers
t
Output
PetrLovtTttttNumtttt
Input
4
hello
party
abefglghjdhfgj
IVan
petrsmatchwin
a
Output
petrsmatchwin
Input
2
aCa
cba
abAcaba
c
Output
abCacba | instruction | 0 | 46,814 | 6 | 93,628 |
Tags: implementation, strings
Correct Solution:
```
import sys
f = sys.stdin
#f = open("input.txt")
n = int(f.readline())
fbs = []
for i in range(n):
fbs.append(f.readline().strip())
s = f.readline().strip()
ch = f.readline().strip()
bChange = [0] * len(s)
for i in range(n):
idx = s.lower().find(fbs[i].lower())
while idx != -1:
bChange[idx:idx+len(fbs[i])] = [1] * len(fbs[i])
idx = s.lower().find(fbs[i].lower(), idx+1)
#print(s)
#print(bChange)
result = ['']*len(s)
for i in range(len(s)):
if bChange[i] == 1:
result[i] = ch
if result[i].upper() == s[i].upper():
if result[i].upper() == 'A':
result[i] = 'B'
else:
result[i] = 'A'
if s[i].isupper():
result[i] = result[i].upper()
else:
result[i] = result[i].lower()
else:
result[i] = s[i]
print("".join(result))
``` | output | 1 | 46,814 | 6 | 93,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.
First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l, r (1 β€ l β€ i β€ r β€ |w|) such that substring w[l ... r] is contained in the collection s1, s2, ..., sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.
Input
The first line contains the only integer n (1 β€ n β€ 100) β the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.
Output
Output the only line β Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1 β€ i β€ |a|), that ai < bi, and for any j (1 β€ j < i) aj = bj. |a| stands for the length of string a.
Examples
Input
3
bers
ucky
elu
PetrLoveLuckyNumbers
t
Output
PetrLovtTttttNumtttt
Input
4
hello
party
abefglghjdhfgj
IVan
petrsmatchwin
a
Output
petrsmatchwin
Input
2
aCa
cba
abAcaba
c
Output
abCacba | instruction | 0 | 46,815 | 6 | 93,630 |
Tags: implementation, strings
Correct Solution:
```
n=int(input())
s=[]
for _ in range(n):
s.append(input())
w=input()
l=input()
x=[]
ans=w
for i in range(n):
ts=w
li=len(s[i])
for j in range(len(w)-li+1):
if ts[j:j+li].lower()==s[i].lower():
p=''
for e in range(j,j+li):
if w[e].islower():
if w[e]==l.lower():
if l.lower()=='a':
p=p+'b'
else:
p=p+'a'
else:
p=p+l.lower()
else:
if w[e]==l.upper():
if l.upper()=='A':
p=p+'B'
else:
p=p+'A'
else:
p=p+l.upper()
ans=ans[:j]+p+ans[j+li:]
print(ans)
``` | output | 1 | 46,815 | 6 | 93,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,430 | 6 | 94,860 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
n,m=map(int,input().split())
ans=0
st=[input() for i in range(n)]
new=['']*n
for i in range(m):
newstr=[new[j]+st[j][i] for j in range(n)]
if(newstr==sorted(newstr)):
new=newstr
else:
ans+=1
print(ans)
``` | output | 1 | 47,430 | 6 | 94,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,431 | 6 | 94,862 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
### NOT FINISHED
# I really need to make it go from line to line:
# first, the first 2 lines should be corrected
# then, the third, and so on.
def issorted(l):
return all(l[i] <= l[i+1] for i in range(len(l)-1))
def mainX(n,m,t):
ans = 0
while not issorted(t):
remove = -1
for i in range(len(t) - 1):
if t[i] > t[i+1]:
for j in range(len(t[i])):
if t[i][j] != t[i+1][j]:
remove = j
break
if remove != -1:
print(i,remove)
break
t = [_t[:remove] + _t[remove+1:] for _t in t]
ans += 1
print(ans)
def is_good(s):
return all(s[i] <= s[i+1] for i in range(len(s)-1))
def line_is_good(a,j):
return all(a[i][j] <= a[i+1][j] for i in range(len(a)-1))
def main5(n,m,a):
if n <= 1 or m<=0 or is_good(a):
print(0)
return
bad = []
for i in range(0,m):
if line_is_good(a,i):
continue
#if bad == []: bad.append(i)
s = ["".join([k for b,k in enumerate(x) if b not in bad]) for x in a]
print(i,bad)
if is_good(s):
print(len(bad))
return
else:
bad.append(i)
print(m)
def main6(n,m,a):
if n <= 1 or m<=0 or is_good(a):
print(0)
return
bad = []
good = range(m)
ans = 0
while not is_good(a):
remove = -1
return
for i in range(0,m):
if line_is_good(a,i):
continue
bad.append(i)
#print(i)
s = ["".join([k for b,k in enumerate(x) if b not in bad]) for x in a]
if is_good(s):
print(len(bad))
return
print(m)
def main(n,m,a):
if n <= 1 or m<=0 or is_good(a):
print(0)
return
bad = []
ans = 0
while not is_good(a):
remove = -1
for i in range(n-1):
if a[i] > a[i+1]:
for j in range(len(a[i])):
if a[i][j] > a[i+1][j]:
remove = j
break
if remove != -1:
break
a = [x[:remove] + x[remove+1:] for x in a]
ans += 1
print(ans)
def main_input():
n,m = [int(i) for i in input().split()]
a = [input() for s in range(n)]
main(n,m,a)
if __name__ == "__main__":
main_input()
``` | output | 1 | 47,431 | 6 | 94,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,432 | 6 | 94,864 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
# from itertools import combinations
# from bisect import bisect_left
# from functools import *
# from collections import Counter
I = lambda: list(map(int, input().split()))
n, m = I()
ans = 0
columns = list(zip(*[input() for i in range(n)]))
g = [0 for _ in range(n)]
for column in columns:
if sum(g[i] or column[i] <= column[i + 1] for i in range(n - 1)) == n - 1:
for i in range(n - 1):
g[i] |= column[i] < column[i + 1]
else:
ans += 1
print(ans)
``` | output | 1 | 47,432 | 6 | 94,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,433 | 6 | 94,866 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
def main():
n, m = map(int, input().split())
a = [input() for x in range(n)]
s = ['']*n
ans = 0
for i in range(m):
ts = [s[j] + a[j][i] for j in range(n)]
if(ts == sorted(ts)):
s = ts
else:
ans += 1
print(ans)
main()
``` | output | 1 | 47,433 | 6 | 94,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,434 | 6 | 94,868 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
def remove(p, a):
for i in range(len(a)):
a[i] = a[i][: p] + a[i][p + 1: ]
n, m = [int(x) for x in input().split()]
a = []
for i in range(n):
a.append(input())
removed = 0
for i in range(m):
i -= removed
for j in range(n - 1):
if a[j][: i + 1] > a[j + 1][: i + 1]:
remove(i, a)
removed += 1
break
print(removed)
``` | output | 1 | 47,434 | 6 | 94,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,435 | 6 | 94,870 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
# cook your dish here
from sys import stdin,stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I=lambda: map(int,stdin.readline().split())
I1=lambda: stdin.readline()
n,m=I()
if n==1:
print(0)
exit()
s=[I1() for i in range(n)]
if m==1:
if s==sorted(s):
print(0)
else:
print(1)
exit()
c,i=0,1
while True:
if i>len(s[0]): break
for j in range(n-1):
if s[j][:i]>s[j+1][:i]:
c+=1
s=[x[:(i-1)]+x[i:] for x in s]
i-=1
break
i+=1
print(c)
``` | output | 1 | 47,435 | 6 | 94,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,436 | 6 | 94,872 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
n, m = map(int,input().split())
table = [input() for i in range(n)]
ans = 0
ar = [1] * n
for i in range(m):
ar2 = [ar[i] for i in range(n)]
flag = 1
for j in range(1,n):
if ar[j]:
if table[j][i] > table[j - 1][i]:
ar2[j] = 0
elif table[j][i] < table[j - 1][i]:
ans += 1
flag = 0
break
if flag:
ar = [ar2[i] for i in range(n)]
print(ans)
``` | output | 1 | 47,436 | 6 | 94,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. | instruction | 0 | 47,437 | 6 | 94,874 |
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
from operator import itemgetter
n, m = map(int, input().strip().split())
table = []
bad_cols = [False] * m
for _ in range(n):
table.append(input().strip())
for j in range(m):
indices = [idx for idx in range(j + 1) if not bad_cols[idx]]
for i in range(1, n):
if itemgetter(*indices)(table[i]) < itemgetter(*indices)(table[i - 1]):
bad_cols[j] = True
break
print(sum(bad_cols))
``` | output | 1 | 47,437 | 6 | 94,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
n,m=map(int, input().split(' ') )
good = [[] for i in range(n)]
masstr = [list(str(input())) for i in range(n)]
for nomer_stolb in range(m):
tmp = [stroka + [masstr[i][nomer_stolb]] for i, stroka in enumerate(good)]
if sorted(tmp)==tmp:
good = tmp
print (m-len(good[0]))
``` | instruction | 0 | 47,438 | 6 | 94,876 |
Yes | output | 1 | 47,438 | 6 | 94,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
def remove(p, a):
for i in range(len(a)):
a[i] = a[i][: p] + a[i][p + 1: ]
n, m = [int(x) for x in input().split()]
a = []
for i in range(n):
a.append(input())
removed = 0
for i in range(m):
i -= removed
for j in range(n - 1):
if a[j][: i + 1] > a[j + 1][: i + 1]:
remove(i, a)
removed += 1
break
print(removed)
# Made By Mostafa_Khaled
``` | instruction | 0 | 47,439 | 6 | 94,878 |
Yes | output | 1 | 47,439 | 6 | 94,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
def input_split(f): return map(f, input().split())
def main():
n, m = input_split(int)
lst = [input()]
sml = []
for i in range(n - 1):
lst.append(input())
sml.append(False)
ans = 0
for i in range(m):
flag = True
for j in range(n - 1):
flag = flag and ((lst[j][i] <= lst[j + 1][i]) or sml[j])
if flag:
for j in range(n - 1):
if lst[j][i] < lst[j + 1][i]:
sml[j] = True
else:
ans += 1
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 47,440 | 6 | 94,880 |
Yes | output | 1 | 47,440 | 6 | 94,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
n,m = map(int, input().split())
words = []
for i in range(n):
words.append(input())
nums = [0] * n
count = 0
for j in range(m):
good = True
for i in range(n - 1):
if (nums[i+1], words[i+1][j]) < (nums[i], words[i][j]):
good = False
if good:
for i in range(n):
nums[i] *= 30
nums[i] += ord(words[i][j]) - ord('a')
else:
count += 1
print(count)
``` | instruction | 0 | 47,441 | 6 | 94,882 |
Yes | output | 1 | 47,441 | 6 | 94,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
'''input
10 10
ddorannorz
mdrnzqvqgo
gdtdjmlsuf
eoxbrntqdp
hribwlslgo
ewlqrontvk
nxibmnawnh
vxiwdjvdom
hyhhewmzmp
iysgvzayst
'''
from sys import stdin
from collections import deque
def get_segment(gstart, arr):
pair = []
start = gstart
end = gstart
for i in range(1, len(arr)):
if arr[i] == arr[i - 1]:
end = i
else:
pair.append([start, end])
start = i
end = i
pair.append([start, end])
return pair
def check_arr(arr):
flag = 0
for i in range(1, len(arr)):
if arr[i] > arr[i - 1]:
pass
elif arr[i] == arr[i - 1]:
flag = 1
else:
return 3
if flag == 0:
return 1
else:
return 2
def power_of_recursion(segment, index):
# print(segment, index)
global myset
if index == m:
return
for start, end in segment:
temp = []
for i in range(start, end + 1):
temp.append(matrix[i][index])
# print(index, start, end, temp)
# print(myset)
t = check_arr(temp)
if t == 1:
continue
elif t == 2:
t_segment = get_segment(start, temp)
# print("temp", t_segment, temp)
power_of_recursion(t_segment, index + 1)
else:
myset.add(index)
# print("SET", index, start, end, temp)
power_of_recursion([[start, end]], index + 1)
# main starts
n, m = list(map(int, stdin.readline().split()))
matrix = []
for _ in range(n):
matrix.append(stdin.readline().strip())
segment = [[0, n - 1]]
myset = set()
power_of_recursion(segment, 0)
print(len(myset))
``` | instruction | 0 | 47,442 | 6 | 94,884 |
No | output | 1 | 47,442 | 6 | 94,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
n_lines, n_cols = map(int, input().split())
cols = []
for _ in range(n_lines):
cols.append(input())
def is_sorted(l):
l = iter(l)
prev = next(l, None)
if prev is None:
return True
while True:
cur = next(l, None)
if cur is None:
break
elif cur < prev:
return False
prev = cur
return True
def pivot_cols(cols):
if len(cols) < 2:
return None
s = []
for i in range(len(cols[0])):
col = cols[i]
if col in pivot_cols.memo:
ok = pivot_cols.memo[col]
else:
ok = is_sorted(map(lambda c: c[i], cols))
pivot_cols.memo[col] = ok
if not ok:
s.append(i)
return s
pivot_cols.memo = dict()
def do_rec(cols):
pivots = pivot_cols(cols)
if not pivots or pivots[0] != 0:
return 0
min_steps = 100
for i in pivots:
n_cols = [s[0:i] + s[i+1:] for s in cols]
steps = do_rec(n_cols)
min_steps = min(steps, min_steps)
return 1 + min_steps
print(do_rec(cols))
``` | instruction | 0 | 47,443 | 6 | 94,886 |
No | output | 1 | 47,443 | 6 | 94,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
from sys import *
inp = lambda : stdin.readline()
def main():
r,c = map(int,inp().split())
l,ans = [inp()[:-1] for i in range(r)],0
chec = [0 for i in range(c)]
for j in range(c):
for i in range(1,r):
if l[i][j] < l[i-1][j]:
chec[j] = 1
break
de = []
for st in range(c):
if chec[st]:
ans += 1
de.append(st)
else:
break
p = []
for i in range(1,r):
if l[i][st] == l[i-1][st]:
p.append(i-1)
#print(p)
for i in p:
ls = st
for j in (j for j in range(st+1,c) if j not in de):
if l[i][j] < l[i+1][j]: break
elif l[i][j] > l[i-1][j]:
de.append(j)
ans += 1
#print(j)
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 47,444 | 6 | 94,888 |
No | output | 1 | 47,444 | 6 | 94,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an n Γ m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table
abcd
edfg
hijk
we obtain the table:
acd
efg
hjk
A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Input
The first line contains two integers β n and m (1 β€ n, m β€ 100).
Next n lines contain m small English letters each β the characters of the table.
Output
Print a single number β the minimum number of columns that you need to remove in order to make the table good.
Examples
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note
In the first sample the table is already good.
In the second sample you may remove the first and third column.
In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).
Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
Submitted Solution:
```
#Er2
def main():
n, m = map(int, input().split())
if n == 1:
s = input()
print(0)
return
a = []
for i in range(n):
s = list(input())
a.append(s)
answer = 0
for i in range(m):
is_sorted = True
for j in range(n - 1):
if ord(a[j][i]) > ord(a[j + 1][i]):
is_sorted = False
break
if not is_sorted:
answer += 1
for p in range(n):
a[p][i] = ''
all_sort = True
for t in range(n - 1):
if a[t] > a[t + 1]:
all_sort = False
break
if all_sort:
break
print(answer)
return
if __name__=="__main__":
main()
``` | instruction | 0 | 47,445 | 6 | 94,890 |
No | output | 1 | 47,445 | 6 | 94,891 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,577 | 6 | 95,154 |
Tags: binary search, greedy, strings
Correct Solution:
```
s = (input())
p = (input())
a = [i-1 for i in list(map(int,input().split()))]
left = 0
right = len(s)
while right - left > 1:
d = list(s)
mid = int((left + right)/2)
for i in range(mid):
d[a[i]] = ''
j = 0
for i in range(len(s)):
if p[j] == d[i]:
j += 1
if j == len(p):
left = mid
break
if j != len(p):
right = mid
print (left)
``` | output | 1 | 47,577 | 6 | 95,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,578 | 6 | 95,156 |
Tags: binary search, greedy, strings
Correct Solution:
```
def good(t, p, b, k):
j = 0
for i in range(len(t)):
if b[i] >= k and t[i] == p[j]:
j += 1
if j == len(p):
return True
return False
def solve(t, p, a):
b = [0] * len(a)
for i in range(len(a)):
b[a[i] - 1] = i
low = 0
high = len(a)
while low + 1 < high:
mid = (low + high) // 2
if good(t, p, b, mid):
low = mid
else:
high = mid
return low
t = input()
p = input()
a = list(map(int, input().split()))
print(solve(t, p, a))
``` | output | 1 | 47,578 | 6 | 95,157 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,579 | 6 | 95,158 |
Tags: binary search, greedy, strings
Correct Solution:
```
def possible(t,p,a,n):
s = ''
check = [True]*len(t)
for i in range(n):
check[a[i]] = False
for i in range(len(check)):
if check[i]:
s += t[i]
m = len(s)
lp = len(p)
c = 0
for i in range(m):
if s[i] == p[c]:
c += 1
if c == lp:
return True
return False
t = input()
p = input()
a = list(map(int,input().split()))
for i in range(len(a)):
a[i] -= 1
low = 0
high = len(a)
ans = 0
while low <= high:
mid = (low+high)//2
if possible(t,p,a,mid):
ans = mid
low = mid+1
else:
high = mid-1
print(ans)
``` | output | 1 | 47,579 | 6 | 95,159 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,580 | 6 | 95,160 |
Tags: binary search, greedy, strings
Correct Solution:
```
s = input()
t = input()
a = list(map(int, input().split()))
def ok(n):
bad = set()
for i in range(n):
bad.add(a[i] - 1)
pt = 0
ps = 0
while pt < len(t) and ps < len(s):
if ps in bad:
ps += 1
else:
if t[pt] == s[ps]:
ps += 1
pt += 1
else:
ps += 1
return pt == len(t)
low = 0
high = len(s)
while high >= low:
mid = (high + low) // 2
if (ok(mid)):
poss = mid
low = mid + 1
else:
high = mid - 1
print (poss)
``` | output | 1 | 47,580 | 6 | 95,161 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,581 | 6 | 95,162 |
Tags: binary search, greedy, strings
Correct Solution:
```
s = input()
t = input()
a = [int(x) - 1 for x in input().split()]
def ok(n):
bad = set(a[:n])
it = (a for i, a in enumerate(s) if i not in bad)
return all(c in it for c in t)
low = 0
high = len(s)
while low < high:
mid = (high + low + 1) // 2
if (ok(mid)):
low = mid
else:
high = mid - 1
print (high)
``` | output | 1 | 47,581 | 6 | 95,163 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,582 | 6 | 95,164 |
Tags: binary search, greedy, strings
Correct Solution:
```
b = input()
s = input()
i = [int(x)-1 for x in input().split()]
def viable(x):
ind = 0
ok = [0]*len(b)
for j in range(0,x):
ok[i[j]] = 1
for j in range(0,len(b)):
if s[ind] == b[j] and ok[j] != 1:
ind += 1
if ind == len(s):
break
if ind == len(s):
return True
else:
return False
low = 0
high = len(b)
while low < high:
mid = (low+high)//2
if viable(mid):
ans = mid
low = mid+1
else:
high = mid
print(ans)
``` | output | 1 | 47,582 | 6 | 95,165 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,583 | 6 | 95,166 |
Tags: binary search, greedy, strings
Correct Solution:
```
#########################################################################################################\
#########################################################################################################
###################################The_Apurv_Rathore#####################################################
#########################################################################################################
#########################################################################################################
import sys,os,io
from math import log, gcd, ceil
from collections import defaultdict as dd, deque, Counter
from heapq import heappush, heappop
from sys import stdin
from bisect import bisect_left, bisect_right
import math
def ncr(n, r, p): #using fermat's little theorem
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def si():
return input()
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
mod = 998244353
t = 1
# t = ii()
def check(moves):
l = list(s)
for i in range(moves):
l[p[i]-1] = '.'
ptrt = 0
ptrl = 0
while(ptrt<len(t) and ptrl<len(l)):
if (l[ptrl]==t[ptrt]):
ptrl+=1
ptrt+=1
else:
ptrl+=1
# print("ptrt,ptrl",ptrt,ptrl)
if (ptrt==len(t)):
return 1
return 0
for _ in range(t):
s = si()
t = si()
p = li()
n = len(s)
low = 0
high = n
ans = 0
while(low<=high):
mid = (high+low)//2
# print("check(mid)",check(mid))
if (check(mid)):
ans = mid
low = mid+1
else:
high = mid-1
print(ans)
``` | output | 1 | 47,583 | 6 | 95,167 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters. | instruction | 0 | 47,584 | 6 | 95,168 |
Tags: binary search, greedy, strings
Correct Solution:
```
s = input()
t = input()
a = [int(x) - 1 for x in input().split()]
def ok(n):
bad = set(a[:n])
it = (a for i, a in enumerate(s) if i not in bad)
return all(c in it for c in t)
low = 0
high = len(s)
while low <= high:
mid = (high + low) // 2
if (ok(mid)):
low = mid + 1
poss = mid
else:
high = mid - 1
print (poss)
``` | output | 1 | 47,584 | 6 | 95,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
def can(m):
u = sorted(a[:m])
inp = 0
inu = 0
for i in range(len(t)):
if inu < len(u) and i == u[inu] - 1:
inu += 1
elif t[i] == p[inp]:
inp += 1
if inp == len(p):
return True
return False
t = input()
p = input()
a = list(map(int, input().split()))
l = 0
r = len(t)
while r - l > 1:
m = (r + l) // 2
if can(m):
l = m
else:
r = m
print(l)
``` | instruction | 0 | 47,585 | 6 | 95,170 |
Yes | output | 1 | 47,585 | 6 | 95,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
a = input()
b = input()
p = list(map(int,input().split()))
n = len(p)
h = [0]*n
for i in range(n):
p[i]-=1
h[p[i]] = i
def check(s1,s2,g):
m1 = len(s1)
m2 = len(s2)
j = 0
i = 0
while j<m1 and i<m2:
if s1[j] == s2[i] and h[i]>g:
j = j+1
i = i + 1
return j==m1
l = -1
r = n-1
while l<r:
md = (l+r+1)//2
# print(md,p[md],h[p[md]])
if check(b,a,h[p[md]]):
# print(md,p[md],h[p[md]])
l = md
else:
r = md-1
print(l+1)
``` | instruction | 0 | 47,586 | 6 | 95,172 |
Yes | output | 1 | 47,586 | 6 | 95,173 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
# from fractions import *
# from heapq import*
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz/'
M=1000000007
EPS=1e-6
def Ceil(a,b): return a//b+int(a%b>0)
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def sub(x):
have=set(a[:x])
i=0
j=0
while(i<len(s) and j<len(t)):
if(i+1 not in have and s[i]==t[j]): j+=1
i+=1
return j==len(t)
s=input()
t=input()
a=array()
low=0
high=len(a)
# print(sub(3))
while(low<=high):
mid=low+(high-low)//2
if(sub(mid)):
ans=mid
low=mid+1
else:
high=mid-1
print(ans)
``` | instruction | 0 | 47,587 | 6 | 95,174 |
Yes | output | 1 | 47,587 | 6 | 95,175 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
s = list(input().strip())
t = input().strip()
sq = [int(a) for a in input().strip().split()]
l = len(s)
lt = len(t)
def check(x):
tmp = s.copy()
for i in range(x):
tmp[sq[i]-1] = '_'
idx = 0
for i in range(l):
if tmp[i]==t[idx]:
idx+=1
if idx==lt:
return True
return False
low = 0
high = l
while(low<=high):
mid = (low + high) >> 1
if check(mid):
low = mid + 1
else:
high = mid - 1
print(high)
``` | instruction | 0 | 47,588 | 6 | 95,176 |
Yes | output | 1 | 47,588 | 6 | 95,177 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
def is_subseq(x, y):
"""Check whether x is a subsequence of y."""
it = iter(y)
return all(c in it for c in x)
t, p = input(), input()
a = [e-1 for e in map(int, input().split())]
r = ''.join(t[i] for i in a)
ri, pi = r[::-1], p[::-1]
it = iter(ri)
all(c in it for c in pi)
n = len(tuple(it))
b = ''.join(t[i] for i in sorted(a[n:]))
while not is_subseq(p, b):
n -= 1
b = ''.join(t[i] for i in sorted(a[n:]))
print(n)
``` | instruction | 0 | 47,589 | 6 | 95,178 |
No | output | 1 | 47,589 | 6 | 95,179 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
def check(x):
temp=[]
for i in t:
temp.append(i)
for i in range(x):
temp[a[i]-1]=''
#print(''.join(temp))
l=0;r=0;c=0
while r<len(s) and l<len(t):
if s[r]==temp[l]:
r+=1;c+=1
l+=1
#print(c)
return c==len(s)
t=input()
s=input()
a=list(map(int,input().split()))
lo=0;hi=len(a)-1
while lo<hi-1:
mid=lo+(hi-lo)//2
#print(lo,hi,mid)
if check(mid):
lo=mid
else:
hi=mid
print(lo)
``` | instruction | 0 | 47,590 | 6 | 95,180 |
No | output | 1 | 47,590 | 6 | 95,181 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
t=input()
p=input()
a=input()
b=a.split()
for k in range(len(b)):
b[k]=int(b[k])
low=0
high=len(b)-1
ans=-1
g=list(t)
while low<=high:
mid=(low+high)//2
d=''
for h in range(mid+1):
g[b[h]-1]=""
k=0
for j in range(mid+1,len(b)):
if d!=p:
if g[j]==p[k]:
d+=g[j]
k+=1
else:
break
if (d==p):
low=mid+1
ans=mid
else:
high=mid-1
for hh in range(mid+1):
if (g[b[hh]-1]==""):
g[b[hh]-1]=t[b[hh]-1]
print (ans+1)
``` | instruction | 0 | 47,591 | 6 | 95,182 |
No | output | 1 | 47,591 | 6 | 95,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 β€ |p| < |t| β€ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 β€ ai β€ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Submitted Solution:
```
from bisect import bisect_left
from math import pi
def isSubSequence(str1,str2,m,n):
j = 0
i = 0
while j<m and i<n:
if str1[j] == str2[i]:
j = j+1
i = i + 1
return j==m
def searchCandies(t,p,a):
l,h,m,ans=0,len(t),0,0
while(h>=l):
m,s,te=l+(h-l)//2,0,t
if(m<len(a)):
te=list(te)
for i in range(m):
te[a[i]-1]='0'
te="".join(te)
if(isSubSequence(p,te,len(p),len(te))):
ans=m
# print(ans)
l=m+1
else:
h=m-1
return ans
def main():
t=input()
p=input()
a=list(map(int,input().split()))
print(searchCandies(t,p,a))
main()
``` | instruction | 0 | 47,592 | 6 | 95,184 |
No | output | 1 | 47,592 | 6 | 95,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.
In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.
The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".
Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.
Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.
Input
The first line contains integer n (1 β€ n β€ 5Β·105) β the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Output
Print a single number β length of the sought dynasty's name in letters.
If Vasya's list is wrong and no dynasty can be found there, print a single number 0.
Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
Note
In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings).
In the second sample there aren't acceptable dynasties.
The only dynasty in the third sample consists of one king, his name is "c". | instruction | 0 | 48,183 | 6 | 96,366 |
Tags: dp
Correct Solution:
```
import sys
import math
from heapq import *;
input = sys.stdin.readline
from functools import cmp_to_key;
def pi():
return(int(input()))
def pl():
return(int(input(), 16))
def ti():
return(list(map(int,input().split())))
def ts():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
mod = 1000000007;
f = [];
def fact(n,m):
global f;
f = [1 for i in range(n+1)];
f[0] = 1;
for i in range(1,n+1):
f[i] = (f[i-1]*i)%m;
def fast_mod_exp(a,b,m):
res = 1;
while b > 0:
if b & 1:
res = (res*a)%m;
a = (a*a)%m;
b = b >> 1;
return res;
def inverseMod(n,m):
return fast_mod_exp(n,m-2,m);
def ncr(n,r,m):
if n < 0 or r < 0 or r > n: return 0;
if r == 0: return 1;
return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m;
def main():
C();
def C():
n = pi();
x = [];
dp = [[0 for j in range(26)] for i in range(26)];
ans = 0;
for i in range(n):
x.append(input()[:-1]);
l = ord(x[i][0]) - ord('a');
r = ord(x[i][len(x[i])-1]) - ord('a');
for j in range(26):
if dp[j][l] != 0:
dp[j][r] = max(dp[j][r], dp[j][l]+len(x[i]));
dp[l][r] = max(dp[l][r], len(x[i]));
for i in range(26):
ans = max(ans, dp[i][i]);
print(ans);
main();
``` | output | 1 | 48,183 | 6 | 96,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.
In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.
The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".
Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.
Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.
Input
The first line contains integer n (1 β€ n β€ 5Β·105) β the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Output
Print a single number β length of the sought dynasty's name in letters.
If Vasya's list is wrong and no dynasty can be found there, print a single number 0.
Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
Note
In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings).
In the second sample there aren't acceptable dynasties.
The only dynasty in the third sample consists of one king, his name is "c". | instruction | 0 | 48,184 | 6 | 96,368 |
Tags: dp
Correct Solution:
```
from sys import stdin
n=int(stdin.readline().strip())
s=[stdin.readline().strip() for i in range(n)]
dp=[[0 for i in range(26)] for j in range(26)]
now=1
last=0
for i in range(n):
a,b=ord(s[i][0])-97,ord(s[i][-1])-97
for j in range(26):
if dp[j][a]>0:
dp[j][b]=max(dp[j][b],dp[j][a]+len(s[i]))
dp[a][b]=max(dp[a][b],len(s[i]))
ans=0
for i in range(26):
ans=max(ans,dp[i][i])
print(ans)
``` | output | 1 | 48,184 | 6 | 96,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.
In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.
The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".
Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.
Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.
Input
The first line contains integer n (1 β€ n β€ 5Β·105) β the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Output
Print a single number β length of the sought dynasty's name in letters.
If Vasya's list is wrong and no dynasty can be found there, print a single number 0.
Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
Note
In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings).
In the second sample there aren't acceptable dynasties.
The only dynasty in the third sample consists of one king, his name is "c". | instruction | 0 | 48,185 | 6 | 96,370 |
Tags: dp
Correct Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
s = list(input().strip() for _ in range(n))
dp = [[0 for i in range(28)] for j in range(28)]
for i in range(n):
a, b = ord(s[i][0]) - ord('a'), ord(s[i][-1]) - ord('a')
for j in range(26):
if dp[j][a] > 0:
dp[j][b] = max(dp[j][b], dp[j][a] + len(s[i]))
dp[a][b] = max(dp[a][b], len(s[i]))
res = 0
for i in range(26):
res = max(res, dp[i][i])
print(res)
``` | output | 1 | 48,185 | 6 | 96,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.
In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.
The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".
Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.
Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.
Input
The first line contains integer n (1 β€ n β€ 5Β·105) β the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Output
Print a single number β length of the sought dynasty's name in letters.
If Vasya's list is wrong and no dynasty can be found there, print a single number 0.
Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
Note
In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings).
In the second sample there aren't acceptable dynasties.
The only dynasty in the third sample consists of one king, his name is "c". | instruction | 0 | 48,186 | 6 | 96,372 |
Tags: dp
Correct Solution:
```
from sys import stdin, stdout
fst = 97
sze = 26
values = [[0 for i in range(sze)] for j in range(sze)]
n = int(stdin.readline())
challengers = []
for i in range(n):
s = stdin.readline().strip()
challengers.append((ord(s[0]) - fst, ord(s[-1]) - fst))
for i in range(sze):
if values[i][challengers[-1][0]]:
values[i][challengers[-1][1]] = max(values[i][challengers[-1][1]], values[i][challengers[-1][0]] + len(s))
values[challengers[-1][0]][challengers[-1][1]] = max(values[challengers[-1][0]][challengers[-1][1]], len(s))
ans = 0
for i in range(sze):
ans = max(ans, values[i][i])
stdout.write(str(ans))
``` | output | 1 | 48,186 | 6 | 96,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,366 | 6 | 96,732 |
Tags: strings
Correct Solution:
```
#Hard Work
s = []
for i in range(3):
tmp = input()
s.append(tmp)
for i in range(3):
tmp = ""
for ch in s[i]:
if ch != '-' and ch != ';' and ch != '_':
tmp += ch
s[i] = tmp.lower()
res = []
for i in range(3):
for j in range(3):
tmp = s[i]
if j != i:
tmp += s[j]
for k in range(3):
if i != k and j != k:
tmp += s[k]
res.append(tmp)
n = int(input())
for i in range(n):
str = input()
tmp = ""
for ch in str:
if ch != '-' and ch != ';' and ch != '_':
tmp += ch
if tmp.lower() in res:
print('ACC')
else:
print('WA')
``` | output | 1 | 48,366 | 6 | 96,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,367 | 6 | 96,734 |
Tags: strings
Correct Solution:
```
def foo(s):
s=''.join(s.split('-'))
s=''.join(s.split(';'))
s=''.join(s.split('_'))
return s.lower()
s1=foo(input())
s2=foo(input())
s3=foo(input())
n=int(input())
arr=[s1+s2+s3,s1+s3+s2,s2+s1+s3,s2+s3+s1,s3+s1+s2,s3+s2+s1]
for _ in range(n):
if foo(input()) in arr:
print('ACC')
else:
print('WA')
``` | output | 1 | 48,367 | 6 | 96,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,368 | 6 | 96,736 |
Tags: strings
Correct Solution:
```
import string
a = input()
b = input()
c = input()
t = []
s = ""
s0 = ""
s1 = ""
reponse = ""
for i in range(len(a)):
if a[i] not in string.punctuation :
s = s + a[i]
for i in range(len(b)):
if b[i] not in string.punctuation :
s0 = s0 + b[i]
for i in range(len(c)):
if c[i] not in string.punctuation :
s1 = s1 + c[i]
n = int(input())
for i in range(n):
x = input()
s2 = ""
for j in range(len(x)) :
if x[j] not in string.punctuation :
s2 = s2 + x[j]
if ((s2.lower() == (s + s0 + s1).lower()) or (s2.lower() == (s + s1 + s0).lower()) or (s2.lower() == (s0 + s + s1).lower()) or (s2.lower() == (s0 + s1 + s).lower()) or (s2.lower() == (s1 + s0 + s).lower()) or (s2.lower() == (s1 + s + s0).lower())):
reponse = "ACC"
else :
reponse = "WA"
t.append(reponse)
for i in range(len(t)):
print(t[i])
``` | output | 1 | 48,368 | 6 | 96,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,369 | 6 | 96,738 |
Tags: strings
Correct Solution:
```
from sys import stdin, stdout
def changing(s):
return s.replace('_', '').replace('-', '').replace(';', '').lower()
s1 = changing(stdin.readline().strip())
s2 = changing(stdin.readline().strip())
s3 = changing(stdin.readline().strip())
n = int(stdin.readline())
for i in range(n):
conc = changing(stdin.readline().strip())
if conc == (s1 + s2 + s3) or conc == (s1 + s3 + s2) or conc == (s2 + s1 + s3) or conc == (s2 + s3 + s1) or conc == (s3 + s1 + s2) or conc == (s3 + s2 + s1):
stdout.write('ACC\n')
else:
stdout.write('WA\n')
``` | output | 1 | 48,369 | 6 | 96,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,370 | 6 | 96,740 |
Tags: strings
Correct Solution:
```
from itertools import permutations
import sys
u=input
l=[u(),u(),u()]
#print(l)
d={';':'' , '-':'', '_':''}
for _ in range(3):
l[_]=''.join(d[s] if s in d else s for s in l[_])
l[_]=l[_].lower();
#print(l)
n=int(u())
for i in range(n):
s=u().lower()
b=0
s=''.join(d[p] if p in d else p for p in s)
#print(s)
for q in permutations(l):
r=''.join(x for x in q)
if(r==s):
b=1
break
print(["WA","ACC"][b])
``` | output | 1 | 48,370 | 6 | 96,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,371 | 6 | 96,742 |
Tags: strings
Correct Solution:
```
import re
def sub(s): return re.sub('[-_;]', '', s).lower()
s1, s2, s3, n, a = sub(input()), sub(input()), sub(input()), int(input()), []
possible = [s1+s2+s3, s1+s3+s2, s2+s1+s3, s2+s3+s1, s3+s1+s2, s3+s2+s1]
for i in range(n):
print('ACC' if sub(input()) in possible else 'WA')
``` | output | 1 | 48,371 | 6 | 96,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,372 | 6 | 96,744 |
Tags: strings
Correct Solution:
```
from collections import Counter
def SomRas(s):
return "".join([item.lower() for item in s if item.isalpha()])
naradmuni = SomRas(input())
vedvyas = SomRas(input())
RishiMuni = SomRas(input())
n = int(input())
for test in range(n):
koi_dikkat = SomRas(input())
was_a_thing = Counter(koi_dikkat)
maybe_a_thing = Counter(naradmuni + vedvyas + RishiMuni)
if was_a_thing != maybe_a_thing:
print("WA")
continue
sort_kiya_hua_cheez = sorted([naradmuni, vedvyas, RishiMuni], key=len)
if sort_kiya_hua_cheez[2] in koi_dikkat:
koi_dikkat = koi_dikkat.split(sort_kiya_hua_cheez[2])
else:
print("WA")
continue
if sort_kiya_hua_cheez[1] in koi_dikkat[0]:
koi_dikkat += koi_dikkat[0].split(sort_kiya_hua_cheez[1])
koi_dikkat = koi_dikkat[1:]
elif sort_kiya_hua_cheez[1] in koi_dikkat[1]:
last = koi_dikkat.pop()
koi_dikkat += last.split(sort_kiya_hua_cheez[1])
else:
print("WA")
continue
if sort_kiya_hua_cheez[0] in koi_dikkat:
print("ACC")
else:
print("WA")
``` | output | 1 | 48,372 | 6 | 96,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the contest in comparing numbers, Shapur's teacher found out that he is a real genius and that no one could possibly do the calculations faster than him even using a super computer!
Some days before the contest, the teacher took a very simple-looking exam and all his n students took part in the exam. The teacher gave them 3 strings and asked them to concatenate them. Concatenating strings means to put them in some arbitrary order one after the other. For example from concatenating Alireza and Amir we can get to AlirezaAmir or AmirAlireza depending on the order of concatenation.
Unfortunately enough, the teacher forgot to ask students to concatenate their strings in a pre-defined order so each student did it the way he/she liked.
Now the teacher knows that Shapur is such a fast-calculating genius boy and asks him to correct the students' papers.
Shapur is not good at doing such a time-taking task. He rather likes to finish up with it as soon as possible and take his time to solve 3-SAT in polynomial time. Moreover, the teacher has given some advice that Shapur has to follow. Here's what the teacher said:
* As I expect you know, the strings I gave to my students (including you) contained only lowercase and uppercase Persian Mikhi-Script letters. These letters are too much like Latin letters, so to make your task much harder I converted all the initial strings and all of the students' answers to Latin.
* As latin alphabet has much less characters than Mikhi-Script, I added three odd-looking characters to the answers, these include "-", ";" and "_". These characters are my own invention of course! And I call them Signs.
* The length of all initial strings was less than or equal to 100 and the lengths of my students' answers are less than or equal to 600
* My son, not all students are genius as you are. It is quite possible that they make minor mistakes changing case of some characters. For example they may write ALiReZaAmIR instead of AlirezaAmir. Don't be picky and ignore these mistakes.
* Those signs which I previously talked to you about are not important. You can ignore them, since many students are in the mood for adding extra signs or forgetting about a sign. So something like Iran;;-- is the same as --;IRAN
* You should indicate for any of my students if his answer was right or wrong. Do this by writing "WA" for Wrong answer or "ACC" for a correct answer.
* I should remind you that none of the strings (initial strings or answers) are empty.
* Finally, do these as soon as possible. You have less than 2 hours to complete this.
Input
The first three lines contain a string each. These are the initial strings. They consists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). All the initial strings have length from 1 to 100, inclusively.
In the fourth line there is a single integer n (0 β€ n β€ 1000), the number of students.
Next n lines contain a student's answer each. It is guaranteed that the answer meets what the teacher said. Each answer iconsists only of lowercase and uppercase Latin letters and signs ("-", ";" and "_"). Length is from 1 to 600, inclusively.
Output
For each student write in a different line. Print "WA" if his answer is wrong or "ACC" if his answer is OK.
Examples
Input
Iran_
Persian;
W_o;n;d;e;r;f;u;l;
7
WonderfulPersianIran
wonderful_PersIAN_IRAN;;_
WONDERFUL___IRAN__PERSIAN__;;
Ira__Persiann__Wonderful
Wonder;;fulPersian___;I;r;a;n;
__________IranPersianWonderful__________
PersianIran_is_Wonderful
Output
ACC
ACC
ACC
WA
ACC
ACC
WA
Input
Shapur;;
is___
a_genius
3
Shapur__a_is___geniUs
is___shapur___a__Genius;
Shapur;;is;;a;;geni;;us;;
Output
WA
ACC
ACC | instruction | 0 | 48,373 | 6 | 96,746 |
Tags: strings
Correct Solution:
```
import re
lib = []
#clean sign trong lib, lower
for i in range(3):
temp = input()
temp = re.sub('[\\_\\-\\;]', '', temp)
lib.append(temp.lower())
answers = int(input())
#thisis; if remove is before it will remove 'is' in this
lib = sorted(lib,key=len,reverse=True)
for i in range(answers):
#preprocess sign cua answer
answer = re.sub('[\\_\\-\\;]', '', input())
answer = answer.lower()
for ele in lib:
temp = answer.find(ele)
if temp == -1:
break
else:
answer = answer.replace(ele,' ',1)#xoa nhung khong dc de trong
if answer.isspace():
print('ACC')
else:
print('WA')
``` | output | 1 | 48,373 | 6 | 96,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:
The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.
If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).
For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.
Input
The first line contains an integer n, which is the number of people in the crew (1 β€ n β€ 100). Then follow n lines. The i-th of those lines contains two words β the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.
Output
Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.
Examples
Input
6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
Output
Teddy
Alice
Bob
Julia
Charlie
Jack | instruction | 0 | 48,382 | 6 | 96,764 |
Tags: implementation, sortings, strings
Correct Solution:
```
n = int(input())
man = []
womanchildren = []
captain = []
for i in range(n):
n, r = map(str, input().split())
if r == 'rat':
print(n)
elif r == 'man':
man.append(n)
elif r == 'woman' or r == 'child':
womanchildren.append(n)
elif r == 'captain':
captain.append(n)
for i in womanchildren:
print(i)
for i in man:
print(i)
for i in captain:
print(i)
``` | output | 1 | 48,382 | 6 | 96,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:
The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.
If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).
For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.
Input
The first line contains an integer n, which is the number of people in the crew (1 β€ n β€ 100). Then follow n lines. The i-th of those lines contains two words β the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.
Output
Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.
Examples
Input
6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
Output
Teddy
Alice
Bob
Julia
Charlie
Jack | instruction | 0 | 48,383 | 6 | 96,766 |
Tags: implementation, sortings, strings
Correct Solution:
```
crew_members = [input().split() for _ in range(int(input()))]
for status_values in ('rat', ('woman', 'child'), 'man', 'captain'):
for name, status in crew_members:
if status in status_values:
print(name)
``` | output | 1 | 48,383 | 6 | 96,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:
The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.
If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).
For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.
Input
The first line contains an integer n, which is the number of people in the crew (1 β€ n β€ 100). Then follow n lines. The i-th of those lines contains two words β the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.
Output
Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.
Examples
Input
6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman
Output
Teddy
Alice
Bob
Julia
Charlie
Jack | instruction | 0 | 48,384 | 6 | 96,768 |
Tags: implementation, sortings, strings
Correct Solution:
```
import sys
def main():
crew = {
'rat' : [],
'woman_child' : [],
'man' : [],
'captain' : []
}
x = input.readline()
n = int(x)
for i in range(n):
member = input.readline()
name = member.split(' ')[0]
status = member[:-1].split(' ')[1]
if status in crew:
crew[status].append(name)
else:
crew['woman_child'].append(name)
for rat in crew['rat']:
output.write(rat + '\n')
for woman_child in crew['woman_child']:
output.write(woman_child + '\n')
for man in crew['man']:
output.write(man + '\n')
output.write(crew['captain'][0])
def run():
global input, output
input = sys.stdin
output = sys.stdout
main()
if __name__ == '__main__':
run()
``` | output | 1 | 48,384 | 6 | 96,769 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.