鏈接:https://ac.nowcoder.com/acm/contest/885/G
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
You are given two strings s and t composed by digits (characters '0' ∼\sim∼ '9'). The length of s is n and the length of t is m. The first character of both s and t aren't '0'.
Please calculate the number of valid subsequences of s that are larger than t if viewed as positive integers. A subsequence is valid if and only if its first character is not '0'.
Two subsequences are different if they are composed of different locations in the original string. For example, string "1223" has 2 different subsequences "23".
Because the answer may be huge, please output the answer modulo 998244353.
輸入描述:
The first line contains one integer T, indicating that there are T tests. Each test consists of 3 lines. The first line of each test contains two integers n and m, denoting the length of strings s and t. The second line of each test contains the string s. The third line of each test contains the string t. * 1≤m≤n≤30001 \le m \le n \le 30001≤m≤n≤3000.
* sum of n in all tests ≤3000\le 3000≤3000.
* the first character of both s and t aren't '0'.
輸出描述:
For each test, output one integer in a line representing the answer modulo 998244353.
示例1
輸入
3 4 2 1234 13 4 2 1034 13 4 1 1111 2
輸出
9 6 11
利用組合知識,首先長度大於m部分的可以直接用組合數算出來
然後用dp計算相同長度的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e3+9;
const ll mod = 998244353;
ll C[maxn][maxn];
void add(ll &x,ll y){
x+=y;
if(x>=mod)x-=mod;
}
void init(){
C[0][0]=1;
for(int i=1;i<=3000;i++){
C[i][0]=1;
for(int j=1;j<=i;j++){
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
}
}
}
char s[maxn],t[maxn];
ll dp[maxn][maxn];
int main(){
init();
int tt;
scanf("%d",&tt);
while(tt--){
int n,m;
scanf("%d%d",&n,&m);
scanf("%s%s",s+1,t+1);
ll ans=0;
for(int i=n-m;i;i--){
if(s[i]!='0')
for(int j=m;j<=n-i;j++){
add(ans,C[n-i][j]);
}
}
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
dp[i][j]=0;
}
}
// cout<<ans<<endl;
for(int i=m;i;i--){
for(int j=n;j;j--){
add(dp[j][i],dp[j+1][i]);
if(t[i]<s[j]){
add(dp[j][i],C[n-j][m-i]);
}
else if(t[i]==s[j]){
add(dp[j][i],dp[j+1][i+1]);
}
}
}
add(ans,dp[1][1]);
printf("%lld\n",ans);
}
return 0;
}