Kaavi and Magic Spell CodeForces - 1337E【dp strings】
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.
Kaavi has a string TT of length mm and all the strings with the prefix TT are magic spells. Kaavi also has a string SS of length nn and an empty string AA.
During the divination, Kaavi needs to perform a sequence of operations. There are two different operations:
- Delete the first character of SS and add it at the front of AA.
- Delete the first character of SS and add it at the back of AA.
Kaavi can perform no more than nn operations. To finish the divination, she wants to know the number of different operation sequences to make AA a magic spell (i.e. with the prefix TT). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998244353998244353.
Two operation sequences are considered different if they are different in length or there exists an iithat their ii-th operation is different.
A substring is a contiguous sequence of characters within a string. A prefix of a string SS is a substring of SS that occurs at the beginning of SS.
Input
The first line contains a string SS of length nn (1≤n≤30001≤n≤3000).
The second line contains a string TT of length mm (1≤m≤n1≤m≤n).
Both strings contain only lowercase Latin letters.
Output
The output contains only one integer — the answer modulo 998244353998244353.
Examples
abab ba
12
defineintlonglong signedmain
0
rotator rotator
4
cacdcdbbbb bdcaccdbbb
24
Note
The first test:
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 AA, although the results are the same, they are considered as different operations. So the answer is 6×2=126×2=12.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 4e18+10; const int mod = 1000000007; const int mx = 200010; //check the limits, dummy typedef pair<int, int> pa; const double PI = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } #define swa(a,b) a^=b^=a^=b #define re(i,a,b) for(int i=(a),_=(b);i<_;i++) #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--) #define clr(a) memset(a, 0, sizeof(a)) #define lowbit(x) ((x)&(x-1)) #define mkp make_pair void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); } ll m, n,y,z,k,sum=0,ans=0; const int N = 3030,M= 998244353; ll dp[N][N]; string s, t; bool match(int i, char c) { if (i > m)return 1; else return t[i - 1] == c; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> t; n = s.size(); m = t.size(); for (int i = 1; i <= n + 1; i++) dp[i][i - 1] = 1; for (int d = 1; d <= n; d++) { char c = s[d - 1]; for (int i = 1, j = d; j <= n; i++, j++) { if (match(i, c)) { dp[i][j] = (dp[i][j] + dp[i + 1][j]) % M; } if (match(j, c)) { dp[i][j] = (dp[i][j] + dp[i][j - 1]) % M; } } } int ans = 0; for (int i = m; i <= n; i++) { ans = (ans + dp[1][i]) % M; } cout << ans << "\n"; return 0; }