2019牛客多校赛第五场G.subsequence 1(DP)

G.subsequence

传送门

题目描述
You are given two strings s and t composed by digits (characters ′ 0 ′ ∼ ′ 9 ′ '0' \sim '9' 09). 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 ≤ 3000 1≤m≤n≤3000 1mn3000.

  • sum of n in all tests ≤ 3000 ≤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
说明
For the last test, there are 6 subsequences “11”, 4 subsequcnes “111” and 1 subsequence “1111” that are valid, so the answer is 11.

题目大意:
给你两个数字字符串是s,t,求字符串s的子序列比字符串t大的个数。

题目思路:
一看就是dp的题,但是当时写了好久都没写出来T-T.
首先,题目我们可以分成长度与t相等的字符串数量和长度大于t的字符串数量。
计算长度与t相等的字符串数量可以用dp解决。

设dp[i][j]表示 s的前i个,匹配 t 的前 j 个的种类数

  • if(s[i] == t[j]) dp[i][j] = dp[i -1][j] + dp[i - 1][j - 1]; //等于的话就等于加上与不加上相加

  • if(s[i] < t[j]) dp[i][j] = dp[i - 1][j]; //小于的话就不用算进去了,长度相等时一定会小于,同时可以排除前导零

  • if(s[i] > s[j]) ans+=dp[i - 1][j - 1] * C[len1 - i][len2 - j].//前面匹配j-1的种类数*后面随便选len2-j个,大于后长度相等时一定大于

计算长度大于t的字符串数量可以直接用组合数解决。
直接枚举第一个(排除前导零)因为长度比t大的,一定会大于t,所以ans+=C[len1-i][j]);直接用组合数(i是枚举第一个数,j是枚举后面取几个)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=4010;
int N,M;
ll dp[maxn][maxn]; //s的前i个,匹配t的钱j个的种类数
char s[maxn],t[maxn];
ll fac[maxn];
ll C[maxn][maxn];


int main(){
    int T,len1,len2;
    ll ans=0;
    scanf("%d",&T);
    ll a,b;
      C[0][0]=C[1][0]=C[1][1]=1;  //组合数
	for(int i=2;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;
	}
    while(T--){
        scanf("%d %d",&N,&M);
		scanf("%s %s",s+1,t+1);
		len1=N;
		len2=M;
		for(int i=0;i<=len1;i++) //初始化
			dp[i][0]=1;
		ans=0;
		for(int i=1;i<=len1;i++)
            for(int j=1;j<=min(len2,i);j++){
                dp[i][j]=dp[i-1][j];  //若小于的时候,只能取前面的
                if(s[i]==t[j])  //若等于的时候,匹配或者不匹配
                    dp[i][j]=(dp[i][j]+dp[i-1][j-1])%mod;
                if(s[i]>t[j])   //若大于则可用随机数处理,后面的所有数取len2-j个,这是计算长度相等时的数量
                    ans=(ans+(ll)dp[i-1][j-1]*C[len1-i][len2-j])%mod;
            }
        for(int i=1;i<=len1;i++){
            if(s[i]=='0') continue;  //排去前导0
            for(int j=len2;j<=len1-i;j++)
                ans=(ans+C[len1-i][j])%mod; //从i开始,剩下的取j个
        }
        printf("%lld\n",ans);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值