解决组合类型的dp

2019暑期牛客多校第5场-G.subsequence 1

题目描述
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 \le m \le n \le 30001≤m≤n≤3000.

  • sum of n in all tests \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
说明
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 串长度,肯定ok,那么我们枚举第一个数字的位置并且用组合数搞一搞就可以了,接下来我们用dp搞长度与 t 串相等且大于 t 的数字数量即可,设d[i][j]为 s 串前 i 个数字中,选了 j 个数字且大于 t 串的前 j 个数组的方案数,f[i][j]为与 t 串相等的方案数,对于 s 串第 i 个数字,我不选 :d[i][j] = d[i - 1][j],f[i][j] = f[i - 1][j],选:d[i][j] += d[i - 1][j - 1],如果s[i] > t[j],那么d[i][j] += f[i - 1][j - 1],如果s[i] = t[j],那么f[i][j] += f[i - 1][j - 1]

题解

#include<iostream>
#include<string.h>
using namespace std;
const int MAXN=3e3+10;
const int MOD=998244353;
long long d[MAXN][MAXN],f[MAXN][MAXN]; //d[i][j]表示s串中从左往右前i个选j个元素,组成的子串大于t串中前j个元素组成的子串 
char s[MAXN],t[MAXN];                  // f[i][j]表示从s串中前i个元素选j个组成的串等与t串中前j个元素组成的子串 
long long c[MAXN][MAXN];     //c[i][j]表示从i个元素里选j个元素的选法数 
void C()
{

//    memset(c,0,sizeof(c));
    for(int i=1;i<=3000;i++)    //计算组合数 
    {   c[i][0]=1;
        c[i][i]=1;
    	for(int j=1;j<i;j++)
    	{
    		c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
		}
	}
	for(int i=1;i<=3000;i++)    //计算组合数的后缀和 
	{
		for(int j=i;j>0;j--)
		c[i][j]=(c[i][j]+c[i][j+1])%MOD; 
	}
	return ;
}
int main()
{
	int T;
	scanf("%d",&T);
    C();
	while(T--)
	{
//		memset(d,0,sizeof(d));
//		memset(f,0,sizeof(f));
		int n,m;
		scanf("%d%d",&n,&m);
		scanf("%s%s",s+1,t+1);
   
		for(int i=1;i<=n;i++)
		{
			if(s[i]>t[1])
			{
				d[i][1]=(d[i-1][1]+1)%MOD;
				f[i][1]=f[i-1][1];
			}
			if(s[i]==t[1])
			{
			 	d[i][1]=d[i-1][1];
			 	f[i][1]=(f[i-1][1]+1)%MOD;
			}
			if(s[i]<t[1])
			{
				d[i][1]=d[i-1][1];
				f[i][1]=f[i-1][1];
			} 
		
		    for(int j=2;j<=m;j++)
			if(i>=j)  
			{
				if(s[i]>t[j]) 
			    {

				    d[i][j]=(d[i-1][j]+d[i-1][j-1]+f[i-1][j-1])%MOD;
				    f[i][j]=(f[i-1][j])%MOD;
			    }
			    if(s[i]==t[j]) 
			    {

				    d[i][j]=(d[i-1][j]+d[i-1][j-1])%MOD;
				    f[i][j]=(f[i-1][j]+f[i-1][j-1])%MOD;
			    } 
			    if(s[i]<t[j]) 
			    {
			    	 d[i][j]=(d[i-1][j]+d[i-1][j-1])%MOD;
			    	 f[i][j]=(f[i-1][j])%MOD;   
				}
//				cout<<"d["<<i<<"]["<<j<<"]="<<d[i][j]<<endl;
//				cout<<"f["<<i<<"]["<<j<<"]="<<f[i][j]<<endl;
			}
		}
		long long ans=0;
		for(int i=1;i<=n-m;i++)
           if(s[i]!='0')
			   ans=(ans+c[n-i][m])%MOD;
 

//		cout<<ans<<endl;
//		cout<<"d[n][m]="<<d[n][m]<<endl;
		ans=(ans+d[n][m])%MOD;
		cout<<ans<<endl;	 
		
		
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值