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
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import os import sys #input = sys.stdin.buffer.readline #sys.setrecursionlimit(int(3e5)) from collections import deque from queue import PriorityQueue import math # list(map(int, input().split())) ##################################################################################### class CF(object): def __init__(self): self.mod = 998244353 self.s = input() self.t = input() self.n = len(self.s) self.dp = [[0]*(self.n+1) for _ in range(self.n +1)] for i in range(len(self.s) + 1): self.dp[0][i] = 1 for i in range(self.n): if((i+1)<=len(self.t)): if(self.s[i] == self.t[i]): self.dp[i+1][0] += self.dp[i][0] self.dp[i+1][0] %= self.mod else: self.dp[i+1][0] += self.dp[i][0] self.dp[i+1][0] %= self.mod for j in range(1, len(self.s) + 1): if(j-1 < len(self.t)): if(self.s[i] == self.t[j-1]): self.dp[i+1][j-1] += self.dp[i][j] self.dp[i+1][j-1] %= self.mod else: self.dp[i+1][j-1] += self.dp[i][j] self.dp[i+1][j-1] %= self.mod if(j+i < len(self.t)): if(self.s[i] == self.t[j+i]): self.dp[i+1][j] += self.dp[i][j] self.dp[i+1][j] %= self.mod else: self.dp[i+1][j] += self.dp[i][j] self.dp[i+1][j] %= self.mod pass pass ans = 0 for i in range(len(self.t), len(self.s)+1): ans += self.dp[i][0] ans %= self.mod print(ans) #print(self.dp) def main(self): pass if __name__ == "__main__": cf = CF() cf.main() pass ```
instruction
0
54,925
0
109,850
Yes
output
1
54,925
0
109,851
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import sys from copy import copy, deepcopy input = sys.stdin.readline inf = 100000000000000000 # 1e17 mod = 998244353 S = input().strip() n = len(S) T = input().strip() T = list(T) num = len(T) T.extend(["!"] * (n - len(T))) f = [[0] * (n + 10) for i in range(n + 10)] for i in range(n): f[i][i - 1] = 1 f[i + 1][i] = 1 for i in range(n): for l in range(n): r = l + i if r >= n: break if S[i] == T[r] or T[r] == "!": f[l][r] += f[l][r - 1] f[l][r] %= mod if S[i] == T[l] or T[l] == "!": f[l][r] += f[l + 1][r] f[l][r] %= mod ans = 0 for i in range(num - 1, n): ans += (f[0][i]) ans %= mod print(ans % mod) ```
instruction
0
54,926
0
109,852
Yes
output
1
54,926
0
109,853
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 4) s = input() t = input() MOD = 998244353 t = t + (len(s) - len(t)) * "?" n = len(s) dp = [[-1] * (n + 1) for i in range(n + 1)] def solve(l, r): if dp[l][r] != -1: return dp[l][r] times = r - l if times == 1: if s[0] == t[l] or t[l] == "?": dp[l][r] = 2 else: dp[l][r] = 0 return dp[l][r] dp[l][r] = 0 if s[times - 1] == t[l] or t[l] == "?": dp[l][r] += solve(l + 1, r) if s[times - 1] == t[r - 1] or t[r - 1] == "?": dp[l][r] += solve(l, r - 1) dp[l][r] %= MOD return dp[l][r] ans = 0 solve(0, n) for i in range(n): if t[i] == "?": ans += dp[0][i] ans %= MOD ans += dp[0][n] ans %= MOD print(ans) ```
instruction
0
54,927
0
109,854
No
output
1
54,927
0
109,855
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import sys from copy import copy, deepcopy input = sys.stdin.readline ''' n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() for CASES in range(int(input())): ''' inf = 100000000000000000 # 1e17 mod = 998244353 ''' # example input abab ba ''' S = input().strip() n = len(S) T = input().strip() T = list(T) num = len(T) T.extend(["!"] * (n - len(T))) f = [[0] * (n + 10) for i in range(n + 10)] for i in range(n): f[i][i - 1] = 1 f[i + 1][i] = 1 for i in range(n): for l in range(n): r = l + i if r >= n: break if S[i] == T[r] or T[r] == "!": f[l][r] += f[l][r - 1] if S[i] == T[l] or T[l] == "!": f[l][r] += f[l + 1][r] ans = 0 for i in range(num - 1, n): ans += (f[0][i]) print(ans) ```
instruction
0
54,928
0
109,856
No
output
1
54,928
0
109,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline S = input().rstrip('\n') S = S[::-1] T = input().rstrip('\n') NS = len(S) NT = len(T) dp = [[0] * (NT+1) for _ in range(NS+1)] dp[0][0] = 1 for i in range(NS): s = S[i] for j in range(NT+1): # front if j == 0: if i != NS-1: if T[0] == s: dp[i+1][1] = (dp[i+1][1] + dp[i][0] + min(i, NS-NT))%mod else: if T[0] == s: dp[i+1][-1] = (dp[i+1][-1] + dp[i][0])%mod elif j == NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if i != NS-1: if s == T[j]: dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j])%mod else: if s == T[j]: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod # back k = NS - (i-j) - 1 if k >= NT: if i != NS-1: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod else: if T[k] == s: if i != NS-1: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod print(dp[-1][-1]) #[print(i, dp[i]) for i in range(NS+1)] if __name__ == '__main__': main() ```
instruction
0
54,929
0
109,858
No
output
1
54,929
0
109,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline S = input().rstrip('\n') S = S[::-1] T = input().rstrip('\n') NS = len(S) NT = len(T) dp = [[0] * (NT+1) for _ in range(NS+1)] dp[0][0] = 1 for i in range(NS): s = S[i] for j in range(min(NT+1, i+1)): # front if j == 0: if i != NS-1: if T[0] == s: dp[i+1][1] = (dp[i+1][1] + dp[i][0] + min(i, NS-NT))%mod else: dp[i+1][-1] = (dp[i+1][-1] + dp[i][0])%mod elif j == NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if i != NS-1: if s == T[j]: dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j])%mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod # back k = NS - (i-j) - 1 if k >= NT: if i != NS-1: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod else: if T[k] == s: if i != NS-1: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod print(dp[-1][-1]) #[print(i, dp[i]) for i in range(NS+1)] if __name__ == '__main__': main() ```
instruction
0
54,930
0
109,860
No
output
1
54,930
0
109,861
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Arkady's code contains n variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code. He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names. A string a is a prefix of a string b if you can delete some (possibly none) characters from the end of b and obtain a. Please find this minimum possible total length of new names. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of variables. The next n lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than 10^5. The variable names are distinct. Output Print a single integer — the minimum possible total length of new variable names. Examples Input 3 codeforces codehorses code Output 6 Input 5 abba abb ab aa aacada Output 11 Input 3 telegram digital resistance Output 3 Note In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c". In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names. Submitted Solution: ``` from sys import stdin,stdout n=int(input()) a=stdin.readlines() b={} d={} for i in range(len(a)): b[a[i]]=len(a[i])-1 xx=list(set(sorted(b.values()))) aaa=[] t=0 answer=0 for i in range(len(xx)): for key, value in b.items(): if xx[i] == value: aaa.append(key) for i in range(len(aaa)): ccc=aaa[i] for j in range(1,len(ccc)): try: d[ccc[:j]]==1 except: d[ccc[:j]]=1 answer+=j break print(answer) ```
instruction
0
55,372
0
110,744
No
output
1
55,372
0
110,745
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,390
0
110,780
"Correct Solution: ``` N, P = map(int, input().split()) S = input() total = 0 if P == 2 or P == 5: for i, s in enumerate(S): if int(s) % P == 0: total += i + 1 else: counts = [0] * P counts[0] = 1 h = 0 d = 1 for s in reversed(S): m = int(s) * d % P h = (h + m) % P total += counts[h] counts[h] += 1 d = (d * 10) % P #print(counts) print(total) ```
output
1
55,390
0
110,781
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,391
0
110,782
"Correct Solution: ``` N, P = map(int, input().split()) S = input() U = [0]*P ans = 0 if P == 2 or P == 5: for i in range(0, N): if int(S[i])%P == 0: ans += i+1 print (ans) else: t, s = 0, 1 for i in range(N-1, -1, -1): t = (int(S[i])*s+t)%P U[t] += 1 s = s*10%P for u in U: ans += u*(u-1)//2 ans += U[0] print (ans) ```
output
1
55,391
0
110,783
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,392
0
110,784
"Correct Solution: ``` N,P=map(int,input().split());S,a,i,j,c=list(map(int,input())),0,0,1,[1]+[0]*P if 10%P: for v in S[::-1]:i=(i+v*j)%P;j=j*10%P;a+=c[i];c[i]+=1 else: for i,v in enumerate(S): if v%P<1:a+=i+1 print(a) ```
output
1
55,392
0
110,785
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,393
0
110,786
"Correct Solution: ``` p = int(input().split()[1]) s = input() if 10 % p == 0: r = sum(i for i, x in enumerate(s, 1) if int(x) % p == 0) else: d = [1] + [0] * (p - 1) t, y = 0, 1 for x in reversed(s): t = (t + int(x) * y) % p d[t] += 1 y = y * 10 % p; r = sum(i * i - i for i in d) // 2 print(r) ```
output
1
55,393
0
110,787
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,394
0
110,788
"Correct Solution: ``` n,p=map(int,input().split()) S=input()[::-1] ans=0 if p==2: for i,s in enumerate(S): if int(s)%2==0: ans+=(n-i) print(ans) exit() if p==5: for i,s in enumerate(S): if int(s)==0 or int(s)==5: ans+=(n-i) print(ans) exit() sd=0 d=1 count=[0]*(p+1) count[0]+=1 for s in S: sd+=int(s)*d d*=10 sd%=p d%=p count[sd]+=1 for cnt in count: ans+=cnt*(cnt-1)//2 print(ans) ```
output
1
55,394
0
110,789
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,395
0
110,790
"Correct Solution: ``` from collections import Counter N, P = map(int, input().split()) S = input() cnt = 0 if P in [2, 5]: for i, s in enumerate(S): if int(s) % P == 0: cnt += (i + 1) else: T = [0] * (N + 1) mods = Counter({0: 1}) for i, s in enumerate(reversed(S)): T[N - 1 - i] = (T[N - i] + int(s) * pow(10, i, P)) % P mods[T[N - 1 - i]] += 1 for t in T: mods[t] -= 1 cnt += mods[t] print(cnt) ```
output
1
55,395
0
110,791
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,396
0
110,792
"Correct Solution: ``` n, p = map(int, input().split()) s = list(map(int, list(input())))[::-1] div = [0 for _ in range(p)] su = 0 for i in range(n): su = (su + s[i] * pow(10, i, p)) % p div[su] += 1 cnt = 0 if p in [2, 5]: if p == 2: ok = [0, 2, 4, 6, 8] else: ok = [0, 5] for i in range(len(s)): if s[i] in ok: cnt += n - i else: for i in range(len(div)): x = div[i] if i == 0: y = x * (x + 1) // 2 else: y = x * (x - 1) // 2 cnt += y print(cnt) ```
output
1
55,396
0
110,793
Provide a correct Python 3 solution for this coding contest problem. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68
instruction
0
55,397
0
110,794
"Correct Solution: ``` # E - Divisible Substring N,P = map(int,input().split()) S = input() ans = 0 if P==2 or P==5: for i in range(N): if int(S[i])%P==0: ans += i+1 else: l = [1]+[0]*(P-1) tmp = 0 r10 = 1 for i in range(N-1,-1,-1): tmp = (r10*int(S[i])+tmp)%P l[tmp] += 1 r10 = (r10*10)%P for x in l: ans += x*(x-1)//2 print(ans) ```
output
1
55,397
0
110,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` import sys;input=sys.stdin.readline N, P = map(int, input().split()) S=input().strip() if P in {2, 5}: r=0 for i in range(N): if not int(S[i])%P: r+=i+1 print(r) else: S = S[::-1] r=0 from collections import defaultdict d=defaultdict(int) d[0] += 1 R=0 X=1 for i in range(N): r += int(S[i])*X R += d[r%P] X = (X*10)%P d[r%P]+=1 print(R) ```
instruction
0
55,399
0
110,798
Yes
output
1
55,399
0
110,799
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` n,m=map(int,input().split());s=input();l=[0]*m;a,t,p=0,0,1 if 10%m: for i in s[::-1]:l[t%m]+=1;t+=int(i)*p;a+=l[t%m];p=p*10%m else:a=sum(i+1 for i in range(n) if int(s[i])%m<1) print(a) ```
instruction
0
55,400
0
110,800
Yes
output
1
55,400
0
110,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` n, p = [int(_) for _ in input().split()] s = input() if p == 2 or p == 5: ans = 0 for i in range(n): if int(s[i]) % p == 0: ans += i+1 print(ans) exit() t = [0 for i in range(n+1)] P = [0 for i in range(p)] P[0] += 1 d = 1 for i in range(1, n+1): t[i] = (t[i-1]+int(s[n-i])*d) % p P[t[i]] += 1 d = d*10 % p ans = 0 for c in P: ans += c*(c-1)//2 # print(t) print(ans) ```
instruction
0
55,401
0
110,802
Yes
output
1
55,401
0
110,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` N,P = map(int, input().split()) S = input() L = [0]*P num = 0 for i in range(N-1, -1, -1): num += int(S[i])*pow(10, N-1-i, P) num %= P L[num] += 1 ans = L[0] for i in range(P): ans += L[i]*(L[i]-1)//2 print(ans) ```
instruction
0
55,402
0
110,804
No
output
1
55,402
0
110,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` N,P = list(map(lambda x:int(x),input().split(" "))) S = input() count=0 store=[0]*P if P in [2,5]: for i in range(N): if int(S[i])%P==0: count+=(i+1) print(count) else: for i in range(N): num = int(S[i:]) store[num%P]+=1 for i in range(P): count += store[i]*(store[i]-1)/2 count += store[0] #print(store) print(int(count)) ```
instruction
0
55,403
0
110,806
No
output
1
55,403
0
110,807
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Takahashi has a string S of length N consisting of digits from `0` through `9`. He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten. Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers. Compute this count to help Takahashi. Constraints * 1 \leq N \leq 2 \times 10^5 * S consists of digits. * |S| = N * 2 \leq P \leq 10000 * P is a prime number. Input Input is given from Standard Input in the following format: N P S Output Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten. Examples Input 4 3 3543 Output 6 Input 4 2 2020 Output 10 Input 20 11 33883322005544116655 Output 68 Submitted Solution: ``` a = input().split(" ") a[1] = int(a[1]) num = input() k = [0] flag = 1 count_k = [0] for i in range(int(a[0])): for p in range(len(k)): s = num[k[p]:(i+1)] if(int(s) % a[1] == 0): count_k[p] += 1 flag = 0 for j in range(k[p], i+1): if(int(num[j:(i+1)]) % a[1] == 0): k[p] = j if(flag == 1): for j in range(i+1): s = num[j:(i+1)] if(int(s) % a[1] == 0): k.append(1) count_k.append(1) flag = 1 count = 0 for i in count_k: count += i * (i+1) / 2 print(int(count)) ```
instruction
0
55,405
0
110,810
No
output
1
55,405
0
110,811
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,716
0
111,432
Tags: dp, greedy, strings Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy cases = int(input().strip()) for case in range(cases): s = list(input().strip()) t = list(input().strip()) ss = set(s) tt = list(set(t)) can = True for i in range(len(tt)): if not tt[i] in ss: can = False if not can: print(-1) else: n = len(s) nxt = [{} for i in range(n)] for i in range(n - 1, -1, -1): if i + 1 < n: for (k, v) in nxt[i + 1].items(): nxt[i][k] = v nxt[i][s[i]] = i ans = 1 pos = 0 for i in range(len(t)): if pos >= n or not(t[i] in nxt[pos]): pos = 0 ans += 1 pos = nxt[pos][t[i]] + 1 print(ans) ```
output
1
55,716
0
111,433
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,717
0
111,434
Tags: dp, greedy, strings Correct Solution: ``` from sys import stdin from bisect import bisect t = int(stdin.readline()) for i in range(t): s = stdin.readline().strip() t = stdin.readline().strip() if not set(t) <= set(s): print(-1) continue p = [[] for i in range(26)] for i, c in enumerate(s): j = ord(c) - ord("a") p[j].append(i) # print(p) x = 0 ops = 0 while x < len(t): # print("x = ", x) k = -1 while x < len(t): c = ord(t[x]) - ord("a") idx = bisect(p[c], k) if idx == len(p[c]): break k = p[c][idx] x += 1 ops += 1 print(ops) ```
output
1
55,717
0
111,435
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,718
0
111,436
Tags: dp, greedy, strings Correct Solution: ``` from bisect import bisect_right for _ in range(int(input())) : s = input() t = input() pre = {i : [] for i in range(26)}; a = ord('a') for i in range(len(s)) : pre[ord(s[i]) - a].append(i) ans = 1; last = -1 for i in t : n = ord(i) - a if pre[n] == [] : ans = -1 break else : w = bisect_right(pre[n], last) if w >= len(pre[n]) : ans += 1 last = pre[n][0] else : last = pre[n][w] print(ans) ```
output
1
55,718
0
111,437
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,719
0
111,438
Tags: dp, greedy, strings Correct Solution: ``` import bisect,sys input = sys.stdin.readline t = int(input()) for testcase in range(t): s = list(input()) t = list(input()) idx = [[] for i in range(30)] for i in range(len(s)-1): s[i] = ord(s[i]) idx[s[i]-97].append(i) now = -1 res = 1 for i in range(len(t)-1): p = ord(t[i])-97 if len(idx[p]) == 0: res = -1 break k = bisect.bisect_right(idx[p],now) if k == len(idx[p]): res += 1 now = idx[p][0] else: now = idx[p][k] print(res) ```
output
1
55,719
0
111,439
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,720
0
111,440
Tags: dp, greedy, strings Correct Solution: ``` from bisect import bisect_right def solve(s, t): pos = [[] for _ in range(26)] for i, c in enumerate(s): x = ord(c) - ord('a') pos[x].append(i) cur = -1 ans = 1 for i, c in enumerate(t): x = ord(c) - ord('a') if pos[x] == []: print(-1) return it = bisect_right(pos[x], cur) if it == len(pos[x]): it = 0 ans += 1 cur = pos[x][it] print(ans) def main(): tc = int(input()) for _ in range(tc): s = input() t = input() solve(s, t) if __name__ == "__main__": main() ```
output
1
55,720
0
111,441
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,721
0
111,442
Tags: dp, greedy, strings Correct Solution: ``` import bisect t = int(input()) for _ in range(t): s=input() t=input() f=[list() for _ in range(26)] orda=ord('a') for i in range(len(s)): f[ord(s[i])-orda].append(i) ans=0 idx=10**9 for i in range(len(t)): search=ord(t[i])-orda if f[search] == list(): ans=-1 break nidx=bisect.bisect_right(f[search], idx) if nidx<len(f[search]): idx = f[search][nidx] else: ans+=1 idx=f[search][0] print(ans) ```
output
1
55,721
0
111,443
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,722
0
111,444
Tags: dp, greedy, strings Correct Solution: ``` import sys from bisect import bisect_left from collections import defaultdict for _ in range(int(input())): s = input() t = input() inds = defaultdict(list) for i in range(len(s)): inds[s[i]].append(i) cnt = 1 ti = 0 si = 0 for ti in range(len(t)): if t[ti] not in inds: print(-1) break ci = bisect_left(inds[t[ti]], si) if ci != len(inds[t[ti]]): si = inds[t[ti]][ci] + 1 else: si = inds[t[ti]][0] + 1 cnt += 1 else: print(cnt) ```
output
1
55,722
0
111,445
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3
instruction
0
55,723
0
111,446
Tags: dp, greedy, strings Correct Solution: ``` from bisect import bisect_left t = int(input()) for _ in range(t): s = input() t = input() d = {} for i in range(len(s)): if s[i] not in d: d[s[i]] = [] d[s[i]].append(i) ans = 1 j = 0 for i in range(len(t)): if t[i] not in d: print(-1) break nxt = bisect_left(d[t[i]], j) if nxt != len(d[t[i]]): j = d[t[i]][nxt] + 1 else: j = d[t[i]][0] + 1 ans += 1 else: print(ans) ```
output
1
55,723
0
111,447
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` import sys, bisect; input = sys.stdin.readline; t = int(input()) while t: t-=1; s1 = input().strip(); s2 = input().strip(); locations = {} for i in range(len(s1)): if s1[i] in locations: locations[s1[i]].append(i) else: locations[s1[i]] = [i] a = i = len(s1);j = len(s2)-1; ans = 1 while j>=0: if s2[j] not in locations: ans = -1; break i = bisect.bisect_right(locations[s2[j]], i)-1 if i<0: i = a; ans += 1 else: i = locations[s2[j]][i]-1; j-=1 print(ans) ```
instruction
0
55,724
0
111,448
Yes
output
1
55,724
0
111,449
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` def search_just_greater(arr, target): if target >= arr[-1]: return -1 low = 0 high = len(arr) - 1 while low < high: mid = low + (high - low) // 2 if arr[mid] <= target: low = mid + 1 else: high = mid # if arr[low] <= target: # return -1 return low if __name__ == "__main__": for _ in range(int(input())): s = list(input()) t = list(input()) positions = [] for i in range(26): positions.append([]) for i in range(len(s)): positions[ord(s[i]) - 97].append(i) pointer = -1 bufferEmpty = True ans = 0 for i in range(len(t)): cur = ord(t[i]) - 97 if len(positions[cur]) == 0: ans = -1 break newptr = search_just_greater(positions[cur], pointer) if newptr == -1: ans += 1 newptr = search_just_greater(positions[cur], -1) # else: if i == len(t) - 1: ans += 1 # break pointer = positions[cur][newptr] # if ans == -1: # print(ans) # continue # # if not bufferEmpty: # # ans += 1 print(ans) ```
instruction
0
55,725
0
111,450
Yes
output
1
55,725
0
111,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` # from debug import debug import sys import bisect # sys.setrecursionlimit(int(1e6)) input = sys.stdin.readline # inf = int(1e10) # recursion code.... # def rec(s1, s2, i, j, g): # if j<0: # return 1 # if i<0 and g == 0: # return 0 # if i<0: # i = len(s1)-1 # g = 0 # while i>=0 and j>=0: # if s1[i] == s2[j]: # j-=1 # g = 1 # i-=1 # val = rec(s1,s2,i,j,g) # if val: # return 1+val # return 0 t = int(input()) while t: t-=1 s1 = input().strip() s2 = input().strip() locations = {} for i in range(len(s1)): if s1[i] in locations: locations[s1[i]].append(i) else: locations[s1[i]] = [i] a,b = len(s1), len(s2)-1 i, j = a, b g = 0 ans = 1 out = 0 while j>=0: if s2[j] not in locations: ans = -1 break i = bisect.bisect_right(locations[s2[j]], i)-1 if i<0: i = a ans += 1 else: i = locations[s2[j]][i]-1 j-=1 print(ans) ```
instruction
0
55,726
0
111,452
Yes
output
1
55,726
0
111,453
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` import math import bisect from collections import defaultdict for _ in range(int(input())): s = input() t = input() n = len(s) d = defaultdict(list) ans = 1 for i in range(n): d[s[i]].append(i) k = 0 curr = -1 #print(d) while(k != len(t)): if len(d[t[k]]) == 0: ans = -1 break curr = bisect.bisect(d[t[k]],curr) #print(curr) if curr>= len(d[t[k]]): curr = -1 ans += 1 else: curr = d[t[k]][curr] k += 1 #print(curr) print(ans) ```
instruction
0
55,727
0
111,454
Yes
output
1
55,727
0
111,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` import sys import math input = sys.stdin.readline from functools import cmp_to_key; def pi(): return(int(input())) def pl(): return(int(input(), 16)) def ti(): return(list(map(int,input().split()))) def ts(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) f = []; def factMod(n,m): global f; f = [1 for i in range(n+1)]; f[0] = 1; for i in range(1,n+1): f[i] = (f[i-1]*i)%m; def fact(n): global f; f = [1 for i in range(n)]; for i in range(1,n): f[i] = f[i-1]*i; def fast_mod_exp(a,b,m): res = 1; while b > 0: if b & 1: res = (res*a)%m; a = (a*a)%m; b = b >> 1; return res; def inverseMod(n,m): return fast_mod_exp(n,m-2,m); def ncrMod(n,r,m): if r == 0: return 1; if n < r: return 0; return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m; def ncr(n,r): if r == 0: return 1; if n < r: return 0; return (f[n])/(f[r]*f[n-r]); def nc2(n): return (n*(n-1))/2; def main(): C(); a = [[-1 for j in range(26)] for i in range(100005)]; def C(): T = int(input()); while T > 0: T -= 1; s = ts(); t = ts(); x = [[] for i in range(26)] res = 1; for i in range(len(s)): index = ord(s[i])-ord('a'); x[index].append(i); for i in range(len(t)): index = ord(t[i])-ord('a'); if len(x[index]) == 0: res = 0; break; if res == 0: print(-1); continue; for i in range(len(s)): for j in range(26): if len(x[j]) > 0: if j != ord(s[i])-ord('a'): a[i][j] = x[j][0]; else: a[i][j] = x[j][1] if len(x[j]) > 1 else -1; x[ord(s[i])-ord('a')].pop(0); curr = 0; count = 0; p = 0; while p < len(t): if curr >= len(s): curr = 0; count += 1; if t[p] == s[curr]: p += 1; curr += 1; continue; if a[curr][ord(t[p])-ord('a')] != -1: curr = a[curr][ord(t[p])-ord('a')]+1; p += 1; continue; count += 1; curr = 0; print(count+1); main(); ```
instruction
0
55,728
0
111,456
No
output
1
55,728
0
111,457
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys from collections import Counter 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 j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j 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') # sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 for _ in range(INT()): S = [ord(c) - 97 for c in input()] T = [ord(c) - 97 for c in input()] N = len(S) M = len(T) C1 = Counter(S) C2 = Counter(T) for k, v in C2.items(): if C1[k] == 0: print(-1) break else: acc = list2d(26, N+1, INF) for i, c in enumerate(S): acc[c][i] = 1 for i in range(26): for j in range(N-1, -1, -1): if acc[i][j] == 1: acc[i][j] = j else: acc[i][j] = acc[i][j+1] cnt = 0 j = 0 for i, c in enumerate(T): if j == 0: cnt += 1 if acc[c][j] == INF: j = 0 elif acc[c][j] == j: j += 1 else: j = acc[c][j] + 1 if j == N: j = 0 print(cnt) ```
instruction
0
55,729
0
111,458
No
output
1
55,729
0
111,459
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` from collections import defaultdict, deque #input = sys.stdin.readline for _ in range(int(input())): s = input() t = input() d = defaultdict(deque) used = defaultdict(deque) for i in range(len(s)): d[s[i]].append(i) exitf = False for i in t: if not len(d[i]): exitf = True if exitf: print(-1) continue count = 0 i = 0 prev = -1 while i < len(t): if len(d[t[i]]) and d[t[i]][0] > prev: prev = d[t[i]].popleft() used[t[i]].append(prev) i+=1 else: prev = -1 count+=1 for tup in used.items(): while len(tup[1]): d[tup[0]].appendleft(tup[1].pop()) print(count+1) ```
instruction
0
55,730
0
111,460
No
output
1
55,730
0
111,461
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z = ac, s = abcde, you may turn z into following strings in one operation: 1. z = acace (if we choose subsequence ace); 2. z = acbcd (if we choose subsequence bcd); 3. z = acbce (if we choose subsequence bce). Note that after this operation string s doesn't change. Calculate the minimum number of such operations to turn string z into string t. Input The first line contains the integer T (1 ≤ T ≤ 100) — the number of test cases. The first line of each testcase contains one string s (1 ≤ |s| ≤ 10^5) consisting of lowercase Latin letters. The second line of each testcase contains one string t (1 ≤ |t| ≤ 10^5) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings s and t in the input does not exceed 2 ⋅ 10^5. Output For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it's impossible print -1. Example Input 3 aabce ace abacaba aax ty yyt Output 1 -1 3 Submitted Solution: ``` import sys import bisect q=int(input()) while q: s=input() t=input() l=-1 ch=0 cnt=1 d={i:[] for i in range(26)} ind=0 for i in s: d[ord(i)-97].append(ind) ind+=1 for i in t: if d[ord(i)-97]==[]: print(-1) ch=1 break else: p=bisect.bisect_right(d[ord(i)-97],l) if p>=len(d[ord(i)-97]): cnt+=1 else: l=d[ord(i)-97][p] if ch==0: print(cnt) q-=1 ```
instruction
0
55,731
0
111,462
No
output
1
55,731
0
111,463
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,749
0
111,498
Tags: dp, strings Correct Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 5) s = input()[:-1] t = input()[:-1] MOD = 998244353 r_lim = len(t) n = len(s) dp = [[0] * (n + 1) for i in range(n + 1)] for length in range(1, n + 1): for l in range(n + 1): r = l + length if r > n: break if length == 1: if l >= r_lim or s[0] == t[l]: dp[l][r] = 2 else: dp[l][r] = 0 continue if l >= r_lim or s[length - 1] == t[l]: dp[l][r] += dp[l + 1][r] dp[l][r] %= MOD if r - 1 >= r_lim or s[length - 1] == t[r - 1]: dp[l][r] += dp[l][r - 1] dp[l][r] %= MOD ans = 0 for i in range(r_lim, n + 1): ans += dp[0][i] ans %= MOD print(ans) ```
output
1
55,749
0
111,499
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,750
0
111,500
Tags: dp, strings Correct Solution: ``` import sys, inspect strings = iter(sys.stdin.read().split()) mod = 998244353 def main(): s,t = next(strings), next(strings) n, m = len(s), len(t) dp = [j==0 for j in range(m+1)] f = lambda j: ( (n-i+1 if i>=m else (s[i]==t[j+i] and dp[j])) if j==0 else (2*dp[j] + (s[i]==t[j-1] and dp[j-1])) if j==m else ((s[i]==t[j-1] and dp[j-1]) + ((i+j>=m or s[i]==t[j+i]) and dp[j])) ) % mod for i in range(n-1, 0, -1): dp = [f(j) for j in range(m+1)] ans = sum(2*dp[j] for j in range(m+1) if j==m or s[0]==t[j]) % mod print(ans) return main() def F(S,T): global s, t, n, m s,t,n,m = S,T,len(S),len(T) # start with A='s[0]' either matching some t[j] or being wasted in the tail (j=m) ans = sum(2*f(1,j) for j in range(m+1) if j==m or t[j]==s[0]) return ans%mod def f(i, j): # 1<=i<=n ; 0<=j<=m # answer given that s[:i] was already used into A and A[j:j+i][:m] matches t[j:j+i] #print('{}{} <- {}'.format(' '*len(inspect.stack(0)), t[j:j+i], s[i:])) if i==n: ans = j==0 elif j==0: if i>=m: ans = n-i+1 # stop anywhere else: ans = s[i]==t[j+i] and f(i+1, j) # back elif j<m: ans = 0 ans += (s[i]==t[j-1]) and f(i+1, j-1) # front ans += (i+j>=m or s[i]==t[j+i]) and f(i+1, j) # back elif j==m: ans = 2*f(i+1, j) # Waste s[i], front-back ans += s[i]==t[j-1] and f(i+1, j-1) # front #print('{}{}'.format(' '*len(inspect.stack(0)), ans)) return ans%mod ```
output
1
55,750
0
111,501
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,751
0
111,502
Tags: dp, strings Correct Solution: ``` # -*- coding:utf-8 -*- """ created by shuangquan.huang at 2020/7/1 """ import collections import time import os import sys import bisect import heapq from typing import List MOD = 998244353 def solve(s, t): n, m = len(s), len(t) dp = [[0 for _ in range(m+1)] for _ in range(n+1)] dp[n][0] = 1 for i in range(n-1, 0, -1): for j in range(m+1): if j == 0: if i >= m: dp[i][0] = n - i + 1 elif s[i] == t[i]: dp[i][0] = dp[i+1][0] elif j == m: dp[i][m] = 2 * dp[i+1][m] % MOD if s[i] == t[m-1]: dp[i][m] = (dp[i][m] + dp[i+1][m-1]) % MOD else: if i + j >= m or s[i] == t[i+j]: dp[i][j] = dp[i+1][j] if s[i] == t[j-1]: dp[i][j] = (dp[i][j] + dp[i+1][j-1]) % MOD ans = dp[1][m] for i in range(m): if t[i] == s[0]: ans = (ans + dp[1][i]) % MOD ans *= 2 ans %= MOD return ans if __name__ == '__main__': s = input() t = input() print(solve(s, t)) ```
output
1
55,751
0
111,503
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,752
0
111,504
Tags: dp, strings Correct Solution: ``` S = input() T = input() b = 998244353 n, m = len(S), len(T) S = '7' + S # 1-indexing T = '7' + T f = [[0 for _ in range(n + 1)] for _ in range(n + 1)] for i in range(m, n + 1): if i > m or S[i] == T[m]: f[i][0] = 1 if S[i] == T[1]: f[i][1] = 1 for i in reversed(range(2, n + 1)): for j in range(0, n - i + 2): if j >= m or T[j + 1] == S[i - 1]: f[i - 1][j + 1] = (f[i - 1][j + 1] + f[i][j]) % b x = j + i - 1 if x > m or S[i - 1] == T[x]: f[i - 1][j] = (f[i - 1][j] + f[i][j]) % b ans = 0 for i in range(n + 1): ans = (ans + f[1][i]) % b print(ans) ```
output
1
55,752
0
111,505
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,753
0
111,506
Tags: dp, strings Correct Solution: ``` import sys from array import array # noqa: F401 from typing import List, Tuple, TypeVar, Generic, Sequence, Union # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def solve(): s, t = input().rstrip(), '*' + input().rstrip() n, m = len(s), len(t) mod = 998244353 dp = [array('i', [0]) * (n + 2) for _ in range(n + 2)] for i in range(1, n + 2): dp[i][i - 1] = 1 for slen, c in enumerate(s): for i, j in zip(range(n + 2), range(slen, n + 2)): if 0 < i < m and t[i] == c or m <= i <= n: dp[i][j] += dp[i + 1][j] if dp[i][j] >= mod: dp[i][j] -= mod if 0 < j < m and t[j] == c or m <= j <= n: dp[i][j] += dp[i][j - 1] if dp[i][j] >= mod: dp[i][j] -= mod print(sum(dp[1][m - 1:]) % mod) if __name__ == '__main__': solve() ```
output
1
55,753
0
111,507
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,754
0
111,508
Tags: dp, strings Correct Solution: ``` def power(x, y, p): res = 1 # Initialize result # Update x if it is more # than or equal to p x = x % p if (x == 0): return 0 while (y > 0): # If y is odd, multiply # x with result if ((y & 1) == 1): res = (res * x) % p # y must be even now y = y >> 1 # y = y/2 x = (x * x) % p return res p=998244353 s=input() t=input() n=len(s) m=len(t) dp=[[0 for j in range(m)] for i in range(n)] for j in range(m): if s[0]==t[j]: dp[0][j]=2 for i in range(1,n): for j in range(m): if s[i]==t[j]: if j==(m-1): dp[i][j]+=power(2,i,p) else: dp[i][j]+=dp[i-1][j+1] if (i+j)>=m or s[i]==t[i+j]: dp[i][j]+=dp[i-1][j] dp[i][j]=dp[i][j]%p ans=0 for i in range(m-1,n): ans+=dp[i][0] ans=ans%p print(ans) print() ```
output
1
55,754
0
111,509
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,755
0
111,510
Tags: dp, strings Correct Solution: ``` # import sys # input = sys.stdin.readline def dp(S,T,n,m,mod): mp = modPower(n,mod) rows, cols = n, n dp = [([0] * cols) for _ in range(rows)] for j in range(n): if j<m: c = int(S[0]==T[j]) dp[0][j] = 2*c else: dp[0][j] = 2 for i in range(1,n): for j in range(n-i): if(i+j<m): dp[i][j] = (dp[i-1][j]*int(S[i]==T[i+j]) + dp[i-1][j+1]*int(S[i]==T[j]))%mod elif(i+j>=m and j<m): dp[i][j] = (dp[i-1][j+1]*int(S[i]==T[j]) + dp[i-1][j])%mod else: dp[i][j] = mp[i+1] r = 0 for i in range(m-1,n): r += dp[i][0]%mod return r%mod def modPower(n,mod): mp = [0]*n mp[0]=1 for i in range(1,n): mp[i] = int((2*mp[i-1])%mod) return mp def solu(S,T,n,m): mod = 998244353 dpr = dp(S,T,n,m,mod) return dpr def printMatrix(des,m): print(des) for i in range(len(m)): print(*m[i]) S = input() T = input() n = len(S) m = len(T) mod = 998244353 dpr = dp(S,T,n,m,mod) print(dpr) ```
output
1
55,755
0
111,511
Provide tags and a correct Python 3 solution for this coding contest problem. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12.
instruction
0
55,756
0
111,512
Tags: dp, strings Correct Solution: ``` S = input() T = input() base = 998244353 dp = [[0 for _ in range(len(S) + 1)] for _ in range(len(S) + 1)] for j in range(1, len(S) + 1): if (j > len(T)) or (S[0] == T[j - 1]): dp[1][j] = 2 for i in range(2, len(S) + 1): for j in range(1, len(S) - i + 1 + 1): if (j > len(T)) or (S[i - 1] == T[j - 1]): dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % base if (j + i - 1 > len(T)) or (S[i - 1] == T[j + i - 1 - 1]): dp[i][j] = (dp[i][j] + dp[i - 1][j]) % base ans = 0 for i in range(len(T), len(S) + 1): ans = (ans + dp[i][1]) % base print(ans) ```
output
1
55,756
0
111,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline S = input().rstrip('\n') S = S[::-1] T = input().rstrip('\n') NS = len(S) NT = len(T) dp = [[0] * (NT+1) for _ in range(NS+1)] dp[0][0] = 1 for i in range(NS): s = S[i] for j in range(NT+1): # front if j == 0: if i != NS-1: if T[0] == s and dp[i][0]: dp[i+1][1] = (dp[i+1][1] + dp[i][0] + min(i, NS-NT))%mod else: if T[0] == s and dp[i][0]: dp[i+1][-1] = (dp[i+1][-1] + dp[i][0] + NS-NT)%mod elif j == NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if i != NS-1: if s == T[j]: dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j])%mod else: if s == T[j]: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod # back k = NS - (i-j) - 1 if k >= NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if T[k] == s: if i != NS-1: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod if k == 0 and dp[i][j]: dp[i+1][-1] = (dp[i+1][-1] + (NS-NT))%mod print(dp[-1][-1]%mod) #[print(i, dp[i]) for i in range(NS+1)] if __name__ == '__main__': main() ```
instruction
0
55,757
0
111,514
Yes
output
1
55,757
0
111,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import io import os import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') fileno = sys.stdin.fileno() input = io.BytesIO( os.read(fileno, os.fstat(fileno).st_size) ).readline MOD = 998_244_353 S = input().strip().decode('utf8') T = input().strip().decode('utf8') N = len(S) M = len(T) D = [ [0 for j in range(M)] for i in range(N) ] for j in range(M): if S[0] == T[j]: D[0][j] = 2 for i in range(1, N): for j in range(M): # left operation if S[i] == T[j]: if j != M - 1: D[i][j] = D[i-1][j+1] else: D[i][j] = pow(2, i, MOD) # right operation if i + j >= M or S[i] == T[i+j]: D[i][j] += D[i-1][j] D[i][j] %= MOD ans = 0 for i in range(M - 1, N): ans += D[i][0] ans %= MOD print(ans) ```
instruction
0
55,759
0
111,518
Yes
output
1
55,759
0
111,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` import sys from copy import copy, deepcopy input = sys.stdin.readline inf = 100000000000000000 # 1e17 mod = 998244353 S = input().strip() n = len(S) T = input().strip() T = list(T) num = len(T) T.extend(["!"] * (n - len(T))) f = [[0] * (n + 10) for i in range(n + 10)] for i in range(n): f[i][i - 1] = 1 f[i + 1][i] = 1 for i in range(n): for l in range(n): r = l + i if r >= n: break if S[i] == T[r] or T[r] == "!": f[l][r] += f[l][r - 1] f[l][r] %= mod if S[i] == T[l] or T[l] == "!": f[l][r] += f[l + 1][r] f[l][r] %= mod ans = 0 for i in range(num - 1, n): ans += (f[0][i]) ans %= mod print(ans % mod) ```
instruction
0
55,760
0
111,520
Yes
output
1
55,760
0
111,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline S = input().rstrip('\n') S = S[::-1] T = input().rstrip('\n') NS = len(S) NT = len(T) dp = [[0] * (NT+1) for _ in range(NS+1)] dp[0][0] = 1 for i in range(NS): s = S[i] for j in range(min(NT+1, i+1)): # front if j == 0: if i != NS-1: if T[0] == s: dp[i+1][1] = (dp[i][0] + min(i, NS-NT))%mod else: dp[i+1][-1] = (dp[i+1][-1] + dp[i][0])%mod elif j == NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if i != NS-1: if s == T[j]: dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j])%mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod # back k = NS - (i-j) - 1 if k >= NT: dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod else: if T[k] == s: if i != NS-1: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod print(dp[-1][-1]) #[print(i, dp[i]) for i in range(NS+1)] if __name__ == '__main__': main() ```
instruction
0
55,761
0
111,522
No
output
1
55,761
0
111,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≤ n ≤ 3000). The second line contains a string T of length m (1 ≤ m ≤ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer — the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6×2=12. Submitted Solution: ``` #f # Fri Oct 02 2020 16:47:22 GMT+0300 (Москва, стандартное время) ```
instruction
0
55,762
0
111,524
No
output
1
55,762
0
111,525