subsequence 1(2019 牛客多校)

题目链接: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 \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.
题意:给出串一串二的字符信息和数量,并且两字符串是两个不含前导零的数串,问:串一中有多少不含前导零的子数串比串二大?(字串的构成字符并非严格连续,数据量模上998244353)
思路:长度大于串二的的、首位非零的子数串均可直接利用组合数计算出来,也就是枚举每个可行位作为子数串的首位再计算组合数即可。
长度等于串二的,利用dp的思想进行计算。设 dp[i][j] 为串一前 i 位能匹配串二 前j位的个数.
状态转移方程 :

\left\{\begin{matrix} dp[i][j] = dp[i-1][j], \\ dp[i][j] += dp[i-1][j-1](s1[i] == s2[j]), \\ dp[i][0] = 1; \end{matrix}\right.

这些状态的意思看代码应该就很好懂了,第一条的意思是当前串一所匹配的这个位置所能完整按顺序匹配的个数,因为这个位置至少一定与我之前匹配的个数相同;第二条的意思说,如果串一当前这个位置又和串二相对的这个位置相等,那么串二在这个位置之前出现的相对字符又可以被全部匹配了,所以得累加上去;最后一条是初始化,若找到一个起点,那么至少为可连续匹配的字符为1,这其实也是为了就前两条dp状态方程,而不写过多的条件判断式。


const int MOD = 998244353;
 ll dp[3001][3001];
  ll c[3001][3001];
    void init()
    {
      c[0][0] = 1;
         for(int i = 1; i <= 3000 ; ++ i)
         {
             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];
                    c[i][j] %= MOD;
               }
         }
    }
 int main()
 {
    init();
      int T;
       cin >> T;
     while(T--)
     {
        
            char s1[3001] , s2[3001];
              int len1 , len2;
               ll sum = 0;
               scanf("%d%d",&len1,&len2);
                scanf("%s%s",s1+1,s2+1);
                 dp[0][0] = 1;
            for(int i = 1 ; i <= len1 ; ++ i)
            {
                 dp[i][0] = 1;
                   for(int j = 1 ; j <= min(len2,i) ; ++ j)
                    {
                        dp[i][j] = dp[i-1][j];
                         if( s1[i] == s2[j] )
                               dp[i][j] += dp[i-1][j-1],
                                  dp[i][j] %= MOD;
                             if( s1[i] > s2[j])
                               sum += ( dp[i-1][j-1] * c[len1-i][len2-j] ) % MOD;
                    }
            }
         for(int i = 1 ; i <= len1; ++ i)
            if( s1[i] != '0')
            {
               for(int k = len2 ; k <= len1 - i; ++ k)
                  sum += c[len1-i][k],
                    sum %= MOD;
            }
          printf("%lld\n",sum);
     }
 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值