多校训练营(第五场)

链接:https://ac.nowcoder.com/acm/contest/885/G
来源:牛客网
 

subsequence 1

时间限制: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

说明

For the last test, there are 6 subsequences "11", 4 subsequcnes "111" and 1 subsequence "1111" that are valid, so the answer is 11.

组合数预处理(递推?)

	for(int i=1;i<3001;i++)//组合数 i 个数中取 j 个 
	{
		c[i][0]=c[i][i]=1;//特殊情况 
		for(int j=1;j<i;j++)
		{
			c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
			//前i-1 个中选j个 (自己不选) 
			//前i-1 中选j-1 个(自己选)
		} 
	}

组合数后缀和 Ci,x(j<=x<=i) 

for(int i=1;i<=3000;i++)//后缀和  大于j个数得所有组合数 
	{
		for(int j=i;j>=0;j--)
		{
			c[i][j]=(c[i][j]+c[i][j+1])%mod;
		}
	}
#include<bits/stdc++.h>
#define ll long long 
#define mod 998244353
using namespace std;
int T;
int sl,tl;
char s[3004],t[3004];
long long dp[3004][3004],f[3004][3004],c[3004][3004],ans=0;
//dp[i][j] 前i个数中长度为[j]合法子序列 数目 
//f[i][j] 前i个数中长度为j 相同的子序列数 
int main()
{
	scanf("%d",&T);
	for(int i=1;i<3001;i++)//组合数 i 个数中取 j 个 
	{
		c[i][0]=c[i][i]=1;//特殊情况 
		for(int j=1;j<i;j++)
		{
			c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
			//前i-1 个中选j个 (自己不选) 
			//前i-1 中选j-1 个(自己选)
		} 
	}
	for(int i=1;i<=3000;i++)//后缀和  大于j个数得所有组合数 
	{
		for(int j=i;j>=0;j--)
		{
			c[i][j]=(c[i][j]+c[i][j+1])%mod;
		}
	}
	while(T--)
	{
		long long ans=0;
		long long ans1=0;
		scanf("%d%d",&sl,&tl);
		for(int i=0;i<=sl;i++)
		{
			s[i]=0;
		}
		for(int i=0;i<=tl;i++)
		{
			t[i]=0;
		}
		for(int i=0;i<sl;i++)
		{
			for(int j=0;j<tl;j++)
			{
				dp[i][j]=0;
				f[i][j]=0;
			}
		}		
		scanf("%s",s);
		scanf("%s",t);
		for(int i=0;i<sl-tl;i++)
		{
			if(s[i]-'0')//s[i]不为零 后面选tl-1个数 
			{
				ans=(ans+c[sl-1-i][tl])%mod;
			}
		}
		for(int i=1;i<=sl;i++)
		{
			for(int j=1;j<=min(i,tl);j++)
			{
				f[i][j]+=f[i-1][j];
				if(s[i-1]==t[j-1])
				{
					if(j==1) f[i][j]+=1;
					f[i][j]+=f[i-1][j-1];//选自己			
				}
				f[i][j]%=mod; 
				dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
				        //不选自己  +  选自己 
				if(s[i-1]>t[j-1])
				{
					if(j==1) dp[i][j]+=1;
					dp[i][j]+=f[i-1][j-1];
					//如果大于可以选前面相同的 
				}
				dp[i][j]%=mod;
			}
		} 
		printf("%lld\n",(ans+dp[sl][tl])%mod);	
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值