1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <bits/stdc++.h> #define per(i, a, b) for(int i=a; i<b; i++) #define rep(i, a, b) for(int i=b-1; i>=a; i--) using namespace std; typedef long long ll; const ll mod(998244353); ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; } const int MAXN(3e3+7); int T; int m, n; string str, ptr; ll dp[MAXN][MAXN], dp1[MAXN][MAXN], dp0[MAXN][MAXN]; ll ANS; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; while(T--) { cin >> n >> m >> str >> ptr; dp[0][0] = dp1[0][0] = dp0[0][0] = 1; per(i, 1, n+1) { dp[i][0] = dp1[i][0] = dp0[i][0] = 1; if(str[i-1]=='0') { dp[i][1] = dp[i-1][1]; dp0[i][1] = dp0[i-1][1]; dp1[i][1] = dp1[i-1][1]; } else { dp0[i][1] = (dp0[i-1][1])%mod; dp1[i][1] = dp1[i-1][1]%mod; dp[i][1] = (dp[i-1][1])%mod; if(str[i-1]==ptr[0]) { dp1[i][1] = (dp1[i][1]+dp1[i-1][0])%mod; } else if(str[i-1]>ptr[0]){ dp[i][1] = (dp[i][1]+dp1[i-1][0])%mod; } else { dp0[i][1] = (dp0[i][1]+dp1[i-1][0])%mod; } } per(j, 2, m+1) { dp0[i][j] = (dp0[i-1][j]+dp0[i-1][j-1])%mod; dp1[i][j] = dp1[i-1][j]%mod; dp[i][j] = (dp[i-1][j]+dp[i-1][j-1])%mod; if(str[i-1]==ptr[j-1]) { dp1[i][j] = (dp1[i][j]+dp1[i-1][j-1])%mod; } else if(str[i-1]>ptr[j-1]){ dp[i][j] = (dp[i][j]+dp1[i-1][j-1])%mod; } else { dp0[i][j] = (dp0[i][j]+dp1[i-1][j-1])%mod; } } per(j, m+1, n+1) { dp1[i][j] = dp0[i][j] = 0; dp[i][j] = (dp[i-1][j]+dp[i-1][j-1]+dp1[i-1][j-1]+dp0[i-1][j-1])%mod; } } ANS=0; per(i, m, n+1) { ANS=(ANS+dp[n][i])%mod; } cout << ANS << endl; } return 0; }
|