比赛的时候看错题了。。以为是要求有多少\(s\)的子序列大于\(t\)的子序列。
考虑\(dp[i][j]\) 表示\([i, n]\)的\(s\)子序列个数\(\geq t\)以\(j\)开始时所能得到的答案,注意此时我们考虑的都是长度\(\leq m\)的子序列
那么很显然可以得到\(dp[i][j] = dp[i+1][j]\)
那么此时再分情况考虑。
若\(s[i] == t[j]\) 则 \(dp[i][j] += dp[i+1][j+1]\)
若\(s[i] \geq t[j]\) 则 \(dp[i][j] += C[n-i][m-j]\) (此时第一位大于,那么我们只需要任取长度和当前t相等的即可得到答案
对于长度\(\geq m\)的情况 直接用组合数算即可
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define lowbit(x) ((x)&(-x))
#define mem(i, a) memset(i, a, sizeof(i))
#define sqr(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define ls (k << 1)
#define rs (k << 1 | 1)
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &X) {
X = 0; char ch = 0; T op = 1;
for(; ch > '9' || ch < '0'; ch = getchar())
if(ch == '-') op = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar())
X = (X << 3) + (X << 1) + ch - 48;
X *= op;
}
const int INF = 0x3f3f3f3f;
const int N = 3005 + 5;
const ll p = 998244353;
char s[N],t[N];
ll c[N][N],dp[N][N];
void init() {
c[0][0] = 1;
for(int i = 1; i <= 3000; i++) {
c[i][0] = c[i][i] = 1;
for(int j = 1; j < i; j++) {
c[i][j] = c[i-1][j-1] % p + c[i-1][j] % p;
c[i][j] %= p;
}
}
}
int main() {
#ifdef INCTRY
freopen("input.txt", "rt", stdin);
#endif
int q;
read(q);
init();
while(q--) {
int n,m;
read(n); read(m);
scanf("%s %s", s + 1, t + 1);
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(s[i] != '0') {
for(int j = m; j <= n - i; j++) {
ans = ans + c[n - i][j];
ans %= p;
}
}
}
//cout << ans << "\n";
for(int i = 1; i <= n + 1; i++)
for(int j = 1; j <= m + 1; j++) dp[i][j] = 0;
for(int i = n; i >= 1; i--) {
for(int j = m; j >= 1; j--) {
dp[i][j] = dp[i + 1][j];
int x = s[i] - '0';
int y = t[j] - '0';
if(x > y) {
dp[i][j] = dp[i][j] + c[n - i][m - j];
dp[i][j] %= p;
}
if(x == y) {
dp[i][j] = dp[i][j] + dp[i+1][j+1];
dp[i][j] %= p;
}
}
}
//cout << dp[4][2] << "\n";
cout << (dp[1][1] + ans) % p << "\n";
}
#ifdef INCTRY
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}