message stringlengths 2 23.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 97 109k | cluster float64 0 0 | __index_level_0__ int64 194 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb". | instruction | 0 | 86,942 | 0 | 173,884 |
Tags: data structures, strings
Correct Solution:
```
import sys
import math as mt
#input=sys.stdin.buffer.readline
#t=int(input())
t=1
mod=9007199254740881
for ___ in range(t):
#n,m=map(int,input().split())
s1=input()
s2=input()
k=int(input())
d={}
for i in range(len(s1)):
suma=0
pre=0
D=256
for j in range(i,len(s1)):
pre=(pre*D+ord(s1[j]))%mod
posi=ord(s1[j])-ord('a')
if s2[posi]=='0':
suma+=1
if suma<=k:
d[pre]=1
else:
break
print(len(d))
``` | output | 1 | 86,942 | 0 | 173,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb". | instruction | 0 | 86,943 | 0 | 173,886 |
Tags: data structures, strings
Correct Solution:
```
s = input()
a = input()
k = int(input())
S=sorted(s[i:] for i in range(len(s)))
p=''
r=0
for e in S:
t=0
s=0
for i in range(len(e)):
if i >= len(p) or e[i] != p[i]:
s=1
if a[ord(e[i])-ord('a')]=='0':
t+=1
if t > k:
break
if s:
r+=1
p = e
print(r)
``` | output | 1 | 86,943 | 0 | 173,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
alph = dict()
for i in range(26):
alph[chr(i+97)] = i
def main():
txt = input()
n = len(txt)
arr = list(map(int, input()))
k = int(input())
suffixArr = [txt[i:] for i in range(n)]
suffixArr.sort()
prev = ""
ans = 0
for s in suffixArr:
count = 0
flag = 0
for j in range(len(s)):
if j >= len(prev) or s[j] != prev[j]:
flag = 1
count += 1-arr[alph[s[j]]]
if count > k:
break
if flag:
ans += 1
prev = s
print(ans)
# ----------------------------------------------------------------------------------------
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip('\r\n')
# endregion
if __name__ == '__main__':
main()
``` | instruction | 0 | 86,944 | 0 | 173,888 |
Yes | output | 1 | 86,944 | 0 | 173,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
s = input()
L = input()
k = int(input())
'''from datetime import *
time1 = datetime.now()'''
good = set()
string = set()
LIST = [chr(i) for i in range(97,123)]
for i in range(26):
if L[i]=='1':
good.add(LIST[i])
t = [s[i] not in good for i in range(len(s))]
end = [0]*len(s)
badchars = 0
front=0; rear=0
while(front<len(s)):
while(rear<len(s)):
badchars+=t[rear]
if badchars>k:
badchars-=1
break
rear+=1
end[front]=rear
badchars -= t[front]
front+=1
for i in range(len(s)):
tempStrHash = 0
for j in range(i, end[i]):
tempStrHash = (tempStrHash*29+ord(s[j])-96)&1152921504606846975
string.add(tempStrHash)
print(len(string))
#print(datetime.now()-time1)
``` | instruction | 0 | 86,945 | 0 | 173,890 |
Yes | output | 1 | 86,945 | 0 | 173,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
import collections as cc
import sys
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
S=lambda :list(input().strip())
s=S()
t=S()
k,=I()
ans=0
prev=''
ss=sorted([s[i:] for i in range(len(s))])
for j in ss:
now=0
f=0
for i in range(len(j)):
if i>=len(prev) or j[i]!=prev[i]:
f=1
now+=t[ord(j[i])-ord('a')]=='0'
if now>k:
break
if f:
ans+=1
prev=j
print(ans)
``` | instruction | 0 | 86,946 | 0 | 173,892 |
Yes | output | 1 | 86,946 | 0 | 173,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
s = input()
L = input()
k = int(input())
l=len(s)
good = set()
string = set()
LIST = [chr(i) for i in range(97, 123)]
for i in range(26):
if L[i] == '1':
good.add(LIST[i])
t = [s[i] not in good for i in range(l)]
end = [0]*l
sumbad = 0
i,j=0,0
while i<l:
if j<l:
sumbad+=t[j]
if sumbad>k or j==l:
sumbad-=t[i]
end[i]=j
i+=1
if sumbad>k:
sumbad-=t[j]
continue
if j<l:
j+=1
for i in range(len(s)):
t = 0
for j in range(i, end[i]):
t = (t*29 + ord(s[j])-96)&1152921504606846975
string.add(t)
print(len(string))
``` | instruction | 0 | 86,947 | 0 | 173,894 |
Yes | output | 1 | 86,947 | 0 | 173,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
import sys
input=sys.stdin.readline
s=input().rstrip()
flag=input().rstrip()
k=int(input())
n=len(s)
ss=[s[i:] for i in range(n)]
ss.sort()
pre=""
ans=0
for e in ss:
veri,cnt_bad=0,0
for i in range(len(e)):
if i>=len(pre) or e[i]!=pre[i]:
veri=1
cnt_bad+=flag[ord(s[i])-ord("a")]=="0"
if cnt_bad>k:
break
if veri:
ans+=1
pre=e
print(ans)
``` | instruction | 0 | 86,948 | 0 | 173,896 |
No | output | 1 | 86,948 | 0 | 173,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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")
# ------------------- fast io --------------------
s = input()
l = [int(k) for k in input()]
bad = set()
for i in range(26):
if l[i] == 0:
bad.add(97+i)
k = int(input())
hashes = set()
base = 999983
s = [ord(x) for x in s]
value = 0
badcount = 0
mod = 10**13 + 11
basemod = [1]*1501
for i in range(1, 1501):
basemod[i] = basemod[i-1] * base % mod
for length in range(1, len(s)+1):
value = value * base % mod
value = (value + s[length-1]) % mod
if s[length-1] in bad:
badcount += 1
if badcount <= k:
hashes.add(value)
val, brbad = value, badcount
for i in range(length, len(s)):
if s[i] in bad:
brbad += 1
if s[i-length] in bad:
brbad -= 1
val = ((val - s[i-length]*(basemod[length-1]))*base + s[i]) % mod
if brbad <= k:
#print(length, i)
hashes.add(val)
print(len(hashes))
``` | instruction | 0 | 86,949 | 0 | 173,898 |
No | output | 1 | 86,949 | 0 | 173,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
s=input()
good=[int(x) for x in input()]
k=int(input())
n=len(s)
ans=0
mod=10**9+9
dp=[[0 for _ in range(n)] for _ in range(n)]
hashes=[(ord(s[0])-ord('a')+1)]
if good[ord(s[0])-ord('a')]:
dp[0][0]=0
else:
dp[0][0]= 1
for i in range(1,n):
hashes.append(hashes[-1]+(ord(s[i])-ord('a')+1)*(29**i))
for i in range(n):
hashes[i]=hashes[i]%(mod)
occ=set()
ppowi=[]
for i in range(n+1):
ppowi.append((29**i)%mod)
for i in range(n):
for j in range(i,n):
if i-1>=0:x=((hashes[j]-hashes[i-1])*(ppowi[n-i]))%mod
else:x=((hashes[j])*(ppowi[n]))%mod
if i==0 and j==0:
dp[i][j]=1^good[ord(s[j])-ord('a')]
elif good[ord(s[j])-ord('a')]==0:
dp[i][j]=dp[i][j-1]+1
else:
dp[i][j]=dp[i][j-1]
if dp[i][j]>k:break
if dp[i][j]<=k:
occ.add(x)
print(len(occ))
``` | instruction | 0 | 86,950 | 0 | 173,900 |
No | output | 1 | 86,950 | 0 | 173,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Submitted Solution:
```
# maa chudaaye duniya
s = input()
badwords = input()
k = int(input())
md = 10**9 + 9
p = 31
h = [0 for i in range(len(s) + 1)]
for i in range(len(s)):
h[i+1] = (h[i] + (ord(s[i]) - ord('a') + 1)*pow(p, i, md)) % md
cn = set([])
# print(h)
for i in range(len(s)):
count = 0
for j in range(i, len(s)):
if badwords[ord(s[j]) - ord('a')] == '0':
count += 1
cur_h = (h[j+1] - h[i] + md) % md
cur_h = (cur_h * pow(p, len(s)-i-1, md)) % md
# print(i, j, s[i:j+1], cur_h, count)
if count > k:
break
else:
cn.add(cur_h)
print(len(cn))
``` | instruction | 0 | 86,951 | 0 | 173,902 |
No | output | 1 | 86,951 | 0 | 173,903 |
Provide a correct Python 3 solution for this coding contest problem.
We have a string S consisting of lowercase English letters.
If the length of S is at most K, print S without change.
If the length of S exceeds K, extract the first K characters in S, append `...` to the end of them, and print the result.
Constraints
* K is an integer between 1 and 100 (inclusive).
* S is a string consisting of lowercase English letters.
* The length of S is between 1 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
K
S
Output
Print a string as stated in Problem Statement.
Examples
Input
7
nikoandsolstice
Output
nikoand...
Input
40
ferelibenterhominesidquodvoluntcredunt
Output
ferelibenterhominesidquodvoluntcredunt | instruction | 0 | 87,308 | 0 | 174,616 |
"Correct Solution:
```
k=int(input());s=input();l=len(s)
print(s[0:min(k,l)]+"."*3*(k<l))
``` | output | 1 | 87,308 | 0 | 174,617 |
Provide a correct Python 3 solution for this coding contest problem.
We have a string S consisting of lowercase English letters.
If the length of S is at most K, print S without change.
If the length of S exceeds K, extract the first K characters in S, append `...` to the end of them, and print the result.
Constraints
* K is an integer between 1 and 100 (inclusive).
* S is a string consisting of lowercase English letters.
* The length of S is between 1 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
K
S
Output
Print a string as stated in Problem Statement.
Examples
Input
7
nikoandsolstice
Output
nikoand...
Input
40
ferelibenterhominesidquodvoluntcredunt
Output
ferelibenterhominesidquodvoluntcredunt | instruction | 0 | 87,311 | 0 | 174,622 |
"Correct Solution:
```
k,s=open(0);k=int(k);print(s[:k]+'...'*(k<~-len(s)))
``` | output | 1 | 87,311 | 0 | 174,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,511 | 0 | 177,022 |
Tags: implementation, strings
Correct Solution:
```
#!/usr/bin/env python3.7
n = int(input())
for _ in range(n):
a, b, c = input(), input(), input()
for x in zip(a, b, c):
xl = len(set(x))
if xl == 3:
print("NO")
break
elif xl == 2:
if x[0]==x[1]:
print("NO")
break
else:
print("YES")
``` | output | 1 | 88,511 | 0 | 177,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,512 | 0 | 177,024 |
Tags: implementation, strings
Correct Solution:
```
def solve():
a=input()
b=input()
c=input()
n=len(a)
for x in range(n):
if a[x]!=c[x] and b[x]!=c[x]:
return "NO"
return "YES"
t=int(input())
for _ in range(t):
print(solve())
``` | output | 1 | 88,512 | 0 | 177,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,513 | 0 | 177,026 |
Tags: implementation, strings
Correct Solution:
```
t = int(input().strip())
for _ in range(t):
a = input().strip()
b = input().strip()
c = input().strip()
ans = True
for i in range(len(a)):
if c[i] == a[i] or c[i] == b[i]:
continue
else:
ans = False
break
if ans:
print("YES")
else:
print("NO")
``` | output | 1 | 88,513 | 0 | 177,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,514 | 0 | 177,028 |
Tags: implementation, strings
Correct Solution:
```
for _ in [0]*int(input()):
v = list(input())
x = list(input())
q = list(input())
s = ''
k = True
for i in range(len(v)):
if x[i]==q[i]:
s = v[i]
v[i] = q[i]
q[i] = s
s = ''
elif v[i]==q[i]:
s = x[i]
x[i] = q[i]
q[i] = s
s =''
elif v[i]==x[i]:
k = False
if v == x and k:
print("YES")
else:
print("NO")
``` | output | 1 | 88,514 | 0 | 177,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,515 | 0 | 177,030 |
Tags: implementation, strings
Correct Solution:
```
t=int(input())
while(t):
a=input()
b=input()
c=input()
s1=list(a)
s2=list(b)
s3=list(c)
c1=0
for i in range(len(s1)):
if(s1[i]==s2[i]==s3[i]):
c1+=1
else:
if(s1[i]==s3[i]):
t1=s3[i]
s3[i]=s2[i]
s2[i]=t1
c1+=1
else:
t1=s3[i]
s3[i]=s1[i]
s1[i]=t1
c1+=1
if(s1==s2 and c1==len(a)):
print("YES")
else:
print("NO")
t=t-1
``` | output | 1 | 88,515 | 0 | 177,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,516 | 0 | 177,032 |
Tags: implementation, strings
Correct Solution:
```
for t in range(int(input())):
a, b, c = input(), input(), input()
anser = 'YES'
for i in range(len(a)):
if not (c[i] == a[i] or c[i] == b[i]):
anser = 'NO'
break
print(anser)
``` | output | 1 | 88,516 | 0 | 177,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,517 | 0 | 177,034 |
Tags: implementation, strings
Correct Solution:
```
for _ in range(int(input())):
a,b,c=input(),input(),input()
ans="YES"
for i in range(len(a)):
if a[i]!=c[i] and b[i]!=c[i]:ans="NO"
print(ans)
``` | output | 1 | 88,517 | 0 | 177,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b. | instruction | 0 | 88,518 | 0 | 177,036 |
Tags: implementation, strings
Correct Solution:
```
for _ in range(int(input())):
a=str(input())
b=str(input())
c=str(input())
f=0
for i in range(len(a)):
if a[i]==b[i] and a[i] == c[i] or a[i] != b[i] and a[i] == c[i] or c[i] == b[i]:
f=1
else:
f=0
break
if f==1:
print('YES')
else:
print('NO')
``` | output | 1 | 88,518 | 0 | 177,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,591 | 0 | 177,182 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
for _ in " "*int(input()):
s=input()
if "B" not in s:
print(len(s))
elif "A" not in s:
print(len(s)%2)
else:
sm=0
n=len(s)
cnt=0
ind = ''.join(s).rindex('B')
for i in range(ind+1):
if s[i] == "A":
sm+=1
if s[i] == "B":
if sm>0:
sm-=1
else:
cnt+=1
print((cnt%2)+(sm)+(n-1-ind))
``` | output | 1 | 88,591 | 0 | 177,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,592 | 0 | 177,184 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
num = int(input())
while num != 0:
s = input()
ans = len(s)
temp = 0
for i in s:
if i == 'B' and temp != 0:
ans = ans - 2
temp = temp - 1
else:
temp = temp + 1
num = num - 1
print(ans)
``` | output | 1 | 88,592 | 0 | 177,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,593 | 0 | 177,186 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def gift():
for _ in range(t):
aabb = input()
n = len(aabb)
ans = n
curB = 0
for i in range(n):
#print(i, curB,aabb[n-1-i])
if aabb[n-1-i]=='B':
curB += 1
else:
if curB>=1:
ans -= 2
curB -= 1
ans -= (curB//2)*2
yield ans
if __name__ == '__main__':
t= int(input())
ans = gift()
print(*ans,sep='\n')
#"{} {} {}".format(maxele,minele,minele)
``` | output | 1 | 88,593 | 0 | 177,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,594 | 0 | 177,188 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
for _ in range(int(input())):
ans = 0
for i in input():
if i == 'B' and ans != 0:
ans -= 1
else:
ans += 1
print(ans)
``` | output | 1 | 88,594 | 0 | 177,189 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,595 | 0 | 177,190 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
from collections import deque
t = int(input())
for _ in range(t):
s=input()
n = len(s)
stack = deque()
for i in range(n):
if s[i]=='B' and stack:
stack.pop()
else:
stack.append(s[i])
length = len(stack)
ans = 0
if length>=1:
temp = []
prev ,prevIdx = stack[0],0
for i in range(1,length):
if stack[i]!=prev:
temp.append((prev,i-prevIdx))
prev = stack[i]
prevIdx = i
temp.append((prev,length-prevIdx))
length = len(temp)
for i in range(length):
if temp[i][0]=='B' and temp[i][1]&1:
ans+=1
else:
ans+=temp[i][1]
print(ans)
``` | output | 1 | 88,595 | 0 | 177,191 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,596 | 0 | 177,192 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
import bisect
def solve(s):
n = len(s)
stack = []
for j in range(n):
if stack:
if s[j] == 'B':
stack.pop()
else:
stack.append('A')
else:
stack.append(s[j])
return len(stack)
t = int(input())
ans = []
for i in range(t):
s = list(input())
ans.append(solve(s))
# print(ans)
for test in ans:
print(test)
``` | output | 1 | 88,596 | 0 | 177,193 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,597 | 0 | 177,194 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
import re
from collections import deque
T = int(input())
for test in range(T):
a = input()
basket = deque(re.findall('A+|B+', a))
i = 0
while i < len(basket) - 1: # A λ©μ΄λ¦¬κ° λ§μ§λ§μ μμΌλ©΄ μλλ€.
if basket[i][0] != 'A':
i += 1
continue
temp = len(basket[i]) - len(basket[i + 1])
del basket[i]
del basket[i]
if temp == 0:
continue
elif temp > 0:
if len(basket) == i:
basket.append('A' * temp)
else:
basket[i] += 'A' * temp
else:
if i == 0:
basket.appendleft('B' * abs(temp))
else:
basket[i - 1] += 'B' * abs(temp)
ans = 0
for elem in basket:
if elem[0] == 'B':
ans += len(elem) % 2
else:
ans += len(elem)
print(ans)
``` | output | 1 | 88,597 | 0 | 177,195 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0. | instruction | 0 | 88,598 | 0 | 177,196 |
Tags: brute force, data structures, greedy, strings
Correct Solution:
```
i = int(input())
for _ in range(i):
test = list(input())
ch = 0
totlen = len(test)
acount = 0
abcount = 0
aindex = []
for _ in range(len(test)):
if test[_]=='A':
test[_] = 'A*'
aindex.append(_)
acount += 1
elif acount > 0:
test[_] = '*'
ai = aindex.pop()
test[ai] = '*'
acount -= 1
abcount += 1
bbcount = 0
bcount = 0
for _ in test:
if _ == 'B':
if bcount > 0:
bbcount +=1
bcount = 0
else:
bcount = 1
elif _ == 'A*' or _ == 'A':
bcount = 0
print(totlen - abcount*2 - bbcount*2)
``` | output | 1 | 88,598 | 0 | 177,197 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None]*T
for qu in range(T):
S = [1 if s == 'A' else 0 for s in readline().strip()]
stack = []
for s in S:
if s:
stack.append(s)
else:
if stack and stack[-1] == 1:
stack.pop()
else:
stack.append(s)
stack2 = []
for s in stack:
if s:
stack2.append(s)
else:
if stack2 and stack2[-1] == 0:
stack2.pop()
else:
stack2.append(s)
Ans[qu] = len(stack2)
print('\n'.join(map(str, Ans)))
``` | instruction | 0 | 88,599 | 0 | 177,198 |
Yes | output | 1 | 88,599 | 0 | 177,199 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
S = list(input())[: -1]
b = 0
res = len(S)
for i in range(len(S) - 1, -1, -1):
b += S[i] == "B"
if S[i] == "A" and b > 0:
res -= 2
b -= 1
res -= (b // 2) * 2
print(res)
``` | instruction | 0 | 88,600 | 0 | 177,200 |
Yes | output | 1 | 88,600 | 0 | 177,201 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
import os
import math
import statistics
true = True;
false = False;
# from collections import defaultdict, deque
from functools import reduce
is_dev = 'vscode' in os.environ
if is_dev:
inF = open('in.txt', 'r')
outF = open('out.txt', 'w')
def ins():
return list(map(int, input_().split(' ')))
def inss():
return list(input_().split(' '))
def input_():
if is_dev:
return inF.readline()[:-1]
else:
return input()
def ranin():
return range(int(input_()))
def print_(data):
if is_dev:
outF.write(str(data)+'\n')
else:
print(data)
epsilon = 1e-7
def prev_i(ii):
return (ii - 1) % n
def next_i(ii):
return (ii + 1) % n
for _ in ranin():
a = input_()
if len(a) <= 1:
print_(len(a))
continue;
cntA = 0
cntB = 0
for i in a:
if i == 'A':
cntA += 1
else:
if cntA > 0:
cntA -= 1
else:
if cntB > 0:
cntB -= 1
else:
cntB += 1
print_(cntA+cntB)
# aa = [a[0]]
# idx = 1
# while idx < len(a):
# if a[idx] == 'B':
# if aa:
# aa.pop()
# else:
# aa.append(a[idx])
# else:
# aa.append(a[idx])
# idx += 1
# print_(len(aa))
if is_dev:
outF.close()
def compare_file():
print(open('out.txt', 'r').read() == open('outactual.txt', 'r').read())
compare_file()
``` | instruction | 0 | 88,601 | 0 | 177,202 |
Yes | output | 1 | 88,601 | 0 | 177,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
from math import *
t=int(input())
while t:
t=t-1
#x1,y1,x2,y2=map(int,input().split())
#n=int(input())
#a=list(map(int,input().split()))
s=input()
aa=0
bb=0
for i in s:
if i=='A':
aa+=1
elif i=='B' and aa!=0:
aa-=1
else:
bb+=1
print(bb%2+aa)
``` | instruction | 0 | 88,602 | 0 | 177,204 |
Yes | output | 1 | 88,602 | 0 | 177,205 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
for _ in range(int(input())) :
arr=input()
x=arr
z=''
while arr != z :
z=arr
arr=arr.replace('AB','')
arr=arr.replace('AB','')
arr=arr.replace('BB','')
arr=arr.replace('BB','')
print(len(arr))
``` | instruction | 0 | 88,603 | 0 | 177,206 |
No | output | 1 | 88,603 | 0 | 177,207 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
ab = int(input())
for s in range(ab):
temp = input()
rem = 0
for i in temp:
if s == 'B' and rem != 0:
rem = rem - 1
else:
rem = rem + 1
print(rem)
``` | instruction | 0 | 88,604 | 0 | 177,208 |
No | output | 1 | 88,604 | 0 | 177,209 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
for t in range(int(input())):
i = str(input())
h = 0
while h == 0:
if len(i) == 1 or len(i) == 0:
break
k2 = any([k,v] == ["A","B"] or [k,v] == ["B","B"] for k, v in zip(i, i[1:]))
if k2 == True:
for k, v in zip(i, i[1:]):
if [k,v] == ["A","B"] or [k,v] == ["B","B"]:
i = i.replace("{}{}".format(k,v),"")
else:
h = 1
print(i)
``` | instruction | 0 | 88,605 | 0 | 177,210 |
No | output | 1 | 88,605 | 0 | 177,211 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
Submitted Solution:
```
for t in range(int(input())):
s = input()
if len(set(s)) == 1:
print(len(s))
continue
while s.find('AB') != -1:
s = s.replace('AB', '')
while s.find('BB') != -1:
s = s.replace('BB', '')
print(len(s))
``` | instruction | 0 | 88,606 | 0 | 177,212 |
No | output | 1 | 88,606 | 0 | 177,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,607 | 0 | 177,214 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
##############--->>>>> Deepcoder Amit Kumar Bhuyan <<<<<---##############
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial,cos,tan,sin,radians
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *
#import threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
import sys
input = sys.stdin.readline
scanner = lambda: int(input())
string = lambda: input().rstrip()
get_list = lambda: list(read())
read = lambda: map(int, input().split())
get_float = lambda: map(float, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // math.gcd(a, b)
def is_integer(n):
return math.ceil(n) == math.floor(n)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
if y == 0:
return 1
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def next_prime(n, primes):
while primes[n] != True:
n += 1
return n
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
def binary_search(low, high, w, h, n):
while low < high:
mid = low + (high - low) // 2
# print(low, mid, high)
if check(mid, w, h, n):
low = mid + 1
else:
high = mid
return low
## for checking any conditions
def check(val, pair):
summ = 0
for x in pair:
if x[1] > val:
summ += x[0]
return summ > val
## for sorting according to second position
def sortSecond(val):
return val[1]
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
alphs = "abcdefghijklmnopqrstuvwxyz"
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal
# from sys import stdout
# import numpy as np
"""
_______________
rough work here
_______________
n piranhas with sizes a1, a2, .... an
scientist of berland state univ
want to find if there is dominant piranha
the piranha is dominant if
it can eat all the other
piranhas in the aquarium
piranha can eat only one of the adjacent piranhas during one move
piranha can do as many moves as it needs
piranha i can eat i - 1
"""
def solve():
n, k = read()
a = list(string())
b = list(string())
freqa = [0] * 26
freqb = [0] * 26
for c in a:
freqa[ord(c) - 97] += 1
for c in b:
freqb[ord(c) - 97] += 1
rem = 0
for x, y in zip(freqa, freqb):
d = x - y
if d == 0:
continue
if abs(d) % k:
print("NO")
break
rem += d // k
if rem < 0:
print("NO")
break
else:
print("YES")
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
t = scanner()
for i in range(t):
solve()
#dmain()
# Comment Read()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | output | 1 | 88,607 | 0 | 177,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,608 | 0 | 177,216 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
# region fastio
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
a=input()
b=input()
alist=[0]*26
blist=[0]*26
for i in range(n):
alist[ord(a[i])-97]+=1
blist[ord(b[i])-97]+=1
flag=0
tmp=0
for i in range(26):
if (blist[i]-alist[i])%k!=0:
flag=1
break
tmp+=alist[i]-blist[i]
if tmp<0:
flag=1
break
if flag==0:
print('YES')
else:
print('NO')
``` | output | 1 | 88,608 | 0 | 177,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,609 | 0 | 177,218 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
input = __import__('sys').stdin.readline
def solve(a, b, k, n):
deca = {}
decb = {}
for i in a:
if i in deca:
deca[i] += 1
else:
deca[i] = 1
for i in b:
if i in decb:
decb[i] += 1
else:
decb[i] = 1
for j in decb:
if j in deca:
if deca[j] < decb[j]:
decb[j] -= deca[j]
deca[j] = 0
else:
deca[j] -= decb[j]
decb[j] = 0
for j in decb:
if decb[j] % k != 0:
return 'NO'
for j in deca:
if deca[j] % k != 0:
return 'NO'
q = ""
p = ""
for i in decb:
if decb[i] != 0:
q += i
for i in deca:
if deca[i] != 0:
p += i
p = sorted(p)
q = sorted(q)
i = 0
j = 0
while i < len(p) and j < len(q):
if p[i] < q[j]:
if deca[p[i]] < decb[q[j]]:
decb[q[j]] -= deca[p[i]]
i += 1
elif deca[p[i]] == decb[q[j]]:
i += 1
j += 1
else:
deca[p[i]] -= decb[q[j]]
j += 1
else:
return 'NO'
return 'YES'
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(input())
b = list(input())
print(solve(a, b, k, n))
``` | output | 1 | 88,609 | 0 | 177,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,610 | 0 | 177,220 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n,k = map(int,input().split())
a = input()
b=input()
d1=[0]*26
d2=[0]*26
for i1,i2 in zip(a,b):
d1[ord(i1)-97]+=1
d2[ord(i2)-97]+=1
ans='YES'
for i,v in enumerate(d1):
if d1[i]!=d2[i]:
if v<d2[i] or (v-d2[i])%k or i==25 :
ans='NO'
break
else:
d1[i]=d2[i]
d1[i+1]+=v-d2[i]
print(ans)
``` | output | 1 | 88,610 | 0 | 177,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,611 | 0 | 177,222 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
for _ in range(INT()):
N, K = MAP()
S = [ord(s)-97 for s in input()]
T = [ord(s)-97 for s in input()]
C1 = [0] * 26
C2 = [0] * 26
for i in range(N):
C1[S[i]] += 1
C2[T[i]] += 1
ok = 1
for c in range(26):
if C1[c] < C2[c]:
ok = 0
break
while C1[c] > C2[c]:
C1[c] -= K
C1[c+1] += K
if C1[c] != C2[c]:
ok = 0
break
if ok:
Yes()
else:
No()
``` | output | 1 | 88,611 | 0 | 177,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,612 | 0 | 177,224 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
import math
from collections import deque
from sys import stdin, stdout, setrecursionlimit
from string import ascii_letters
letters = ascii_letters[:26]
from collections import defaultdict
#from functools import reduce
input = stdin.readline
print = stdout.write
for _ in range(int(input())):
n, k = map(int, input().split())
first = input().strip()
second = input().strip()
have = defaultdict(int)
need = defaultdict(int)
for i in first:
have[letters.index(i)] += 1
for i in second:
need[letters.index(i)] += 1
ost = 0
can = True
for i in range(26):
if (have[i] + ost) - need[i] < 0 or ((have[i] + ost) - need[i]) % k:
can = False
break
ost = (have[i] + ost) - need[i]
if ost > 0:
can = False
print('Yes\n' if can else 'No\n')
``` | output | 1 | 88,612 | 0 | 177,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,613 | 0 | 177,226 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
import sys
input=sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = input()
b = input()
c1=[0]*26
c2=[0]*26
for i in range(0,n):
c1[ord(a[i])-97]+=1
c2[ord(b[i])-97]+=1
ans="YES"
for j in range(0,26):
if c1[j]==c2[j]:
continue
elif c2[j]>c1[j]:
ans='NO'
break
elif (c1[j]-c2[j])%k!=0:
ans="NO"
break
else:
c1[j+1]+=c1[j]-c2[j]
print(ans)
``` | output | 1 | 88,613 | 0 | 177,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc". | instruction | 0 | 88,614 | 0 | 177,228 |
Tags: dp, greedy, hashing, implementation, strings
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('a')
file = 1
def ceil(a,b):
return (a+b-1)//b
# write fastio for getting fastio template.
def solve():
for _ in range(ii()):
n,k = mi()
f1 = [0]*27
f2 = f1[::]
a = si()
b = si()
for i in a:
f1[bo(i)]+=1
for i in b:
f2[bo(i)]+=1
f = 0
for i in range(26):
mnx = min(f1[i],f2[i])
f1[i] -= mnx
f2[i] -= mnx
if f1[i]%k or f2[i]%k:
f = 1
break
if f:
print('NO')
continue
for i in range(26):
if f1[i] >= f2[i]:
f1[i] -= f2[i]
f2[i] = 0
f1[i+1] += f1[i]
f1[i] = 0
# print(f1,f2)
for i in range(26):
if f2[i]:
f = 1
break
print('NO' if f else 'YES')
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
``` | output | 1 | 88,614 | 0 | 177,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fibonacci strings are defined as follows:
* f1 = Β«aΒ»
* f2 = Β«bΒ»
* fn = fn - 1 fn - 2, n > 2
Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba".
You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substring.
Input
The first line contains two space-separated integers k and m β the number of a Fibonacci string and the number of queries, correspondingly.
Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters "a" and "b".
The input limitations for getting 30 points are:
* 1 β€ k β€ 3000
* 1 β€ m β€ 3000
* The total length of strings si doesn't exceed 3000
The input limitations for getting 100 points are:
* 1 β€ k β€ 1018
* 1 β€ m β€ 104
* The total length of strings si doesn't exceed 105
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.
Examples
Input
6 5
a
b
ab
ba
aba
Output
3
5
3
3
1 | instruction | 0 | 88,675 | 0 | 177,350 |
Tags: matrices, strings
Correct Solution:
```
F = ['', 'a', 'b', 'ba', 'bab', 'babba', 'babbabab', 'babbababbabba', 'babbababbabbababbabab', 'babbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabab']
while len(F[-3]) < 100000: F.append(F[-1] + F[-2])
d = 1000000007
def sqr(t):
return [[sum(t[i][k] * t[k][j] for k in range(4)) % d for j in range(4)] for i in range(4)]
def mul(a, b):
return [[sum(a[i][k] * b[k][j] for k in range(4)) % d for j in range(4)] for i in range(4)]
def fib(k):
s, p = format(k, 'b')[:: -1], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
t = [[[0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0]]] + [0] * (len(s) - 1)
for i in range(1, len(s)):
t[i] = sqr(t[i - 1])
for i, k in enumerate(s):
if k == '1': p = mul(p, t[i])
return p
def cnt(t, p):
s, i = 0, p.find(t) + 1
while i > 0:
i = p.find(t, i) + 1
s += 1
return s
def f(t, p, k):
l = len(t) - 1
if l: x, y = cnt(t, F[k - 1][- l: ] + F[k][:l ]), cnt(t, F[k][- l: ] + F[k + 1][:l ])
else: x, y = 0, 0
a, b = cnt(t, F[k - 1]), cnt(t, F[k])
return (p[0] * a + p[1] * b + p[2] * y + p[3] * x) % d
k, m = map(int, input().split())
if k > 15:
x, y, z = len(F[7]), len(F[17]), len(F) - 4
a, b, c = fib(k - 7)[0], fib(k - 17)[0], fib(k - z)[0]
for i in range(m):
t = input()
if len(t) < x: print(f(t, a, 8))
elif len(t) < y: print(f(t, b, 18))
else: print(f(t, c, z + 1))
else:
p = F[k]
for i in range(m):
print(cnt(input(), p))
``` | output | 1 | 88,675 | 0 | 177,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s. | instruction | 0 | 88,692 | 0 | 177,384 |
Tags: data structures, dp, strings
Correct Solution:
```
import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
for (i, ch) in enumerate(t):
idx = ord(ch) - ord('a')
char_occur[idx].append(i)
for ch in string.ascii_lowercase:
idx = ord(ch) - ord('a')
char_occur[idx].append(len(t)+1)
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes")
``` | output | 1 | 88,692 | 0 | 177,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
Submitted Solution:
```
import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
for ch in string.ascii_lowercase:
idx = ord(ch) - ord('a')
char_occur[idx] = [i for (i,_ch) in enumerate(t) if ch==_ch]
char_occur[idx].append(len(t)+1)
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
if len(t) > 100000:
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes")
``` | instruction | 0 | 88,693 | 0 | 177,386 |
No | output | 1 | 88,693 | 0 | 177,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
Submitted Solution:
```
import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
idx = ord(ch) - ord('a')
char_occur[idx].append(i)
for ch in string.ascii_lowercase:
idx = ord(ch) - ord('a')
char_occur[idx].append(len(t)+1)
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes")
``` | instruction | 0 | 88,694 | 0 | 177,388 |
No | output | 1 | 88,694 | 0 | 177,389 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
Submitted Solution:
```
import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
for ch in string.ascii_lowercase:
idx = ord(ch) - ord('a')
char_occur[idx] = [i for (i,_ch) in enumerate(t) if ch==_ch]
char_occur[idx].append(len(t)+1)
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
if len(t) < 100000:
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes")
``` | instruction | 0 | 88,695 | 0 | 177,390 |
No | output | 1 | 88,695 | 0 | 177,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
Submitted Solution:
```
import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
idx = ord(ch) - ord('a')
char_occur[idx].append(i)
for ch in string.ascii_lowercase:
char_occur[idx].append(len(t)+1)
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes")
``` | instruction | 0 | 88,696 | 0 | 177,392 |
No | output | 1 | 88,696 | 0 | 177,393 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.