【2019牛客暑期多校训练营(第五场)】G subsequence 1 [组合数+基础DP]

原题传送门

Problem Description

You are given two strings s and t composed by digits (characters’0’∼’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.

Input

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.
  • sum of n in all tests ≤3000.
  • the first character of both s and t aren’t ‘0’.
Output

For each test, output one integer in a line representing the answer modulo 998244353.

Sample Input

3
4 2
1234
13
4 2
1034
13
4 1
1111
2

Sample Output

9
6
11

题目大意

给出一个 n 长的字符串 sm 长的字符串 t(s,t皆由’0’~'9’组成,并保证不会以’0’开头)。从 s 中找出大于 t 的所有不以’0’开头的子序列( 注意1.大于是两者看作整数时比较 2.不同位置上相同的数可以组成不同的子序列,例如"1223"中存在2个"23")。将结果模998244353后输出。

思路
  • s中长度 大于 m的非’0’开头子序列显然大于t,可使用组合数公式求出。
  • s中长度 等于 m的非’0’开头子序列,显然是使用dp啦 …然而本蒟蒻并不会…
    i–用来枚举起始位置(就是s)
    j–用来枚举t中的数字
    if(s[i]==t[j]) dp[j]+=dp[j-1]; 啥都干不了那就+=呗
    if(s[i] > t[j]) yeah!终于大于了好快乐 说明此位置可取,使用组合数计算在s的i位置之后取m-j个的数字 C[n-i][m-j]
  • s中长度 小于 m的非’0’开头子序列 ,没人要了的可怜孩子
样例解释


4 2
1 2 3 4
1 3

为例:
“14”
“23”
“24”
“34”
“123”
“124”
“134”
“234”
“1234”

Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 3000 + 5;
 
int n, m;
char s[maxn], t[maxn];
ll C[maxn][maxn];
 
void pre()      //预处理组合数
{
    for (int i = 0; i < maxn; i++) {
        C[i][0] =C[i][i]= 1;
        for (int j = 1; j < i; j++) {
            C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod;
        }
    }
}
 
ll dp[maxn];
void solve()
{
    ll ans = 0;
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    for (int i = 1; i <= n; i++)
    {      
        //s中长度 > m的序列  组合数
        if (s[i] != '0') for (int j = m; j <= n - i ; j++) (ans+=C[n - i][j])%=mod;// i 枚举开头位置    j 枚举子序列长度
        //s中长度 == m的序列
        for (int j = min(i, m); j >= 1; j--) 
        {
        	if (s[i] > t[j]) (ans += C[n - i][m - j] * dp[j - 1]) %= mod;
       	    if (s[i] == t[j]) (dp[j] += dp[j - 1]) %= mod;
   		}
    }
    printf("%lld\n", ans);
}
 
int main()
{
    pre();      //预处理组合数
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        scanf("%s%s", s+1, t+1);
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值