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.

算法:dp + 排列组合

题意:给你两个字符串s和t。找出字符串s中多有多少个子串大于字符串t。

题解:dp的作用是计算字符串s的子串与字符串t相同长度时的数量,而下面那个循环式计算字符串s的子串长度大于字符串t时的数量,两者相加就是最终所求的数量

注意:杨辉三角就是按照组合数的性质来的,读者可以自行证明。

#include <iostream>
#include <cstdio>
#include <memory.h> using namespace std; const int maxn = ;
const int mod = ; typedef long long ll; ll C[maxn][maxn]; //以杨辉三角的形式来存取组合数,表示C(i, j)
ll dp[maxn][maxn]; //表示字符串s从第i个位置开始,字符串t从第j个位置开始,有多少个字串所匹配
char s[maxn], t[maxn]; int main() {
//预处理组合数
for(int i = ; i <= ; i++) {
for(int j = ; j <= i; j++) {
if(i == j || j == ) {
C[i][j] = ;
} else {
C[i][j] = (C[i - ][j - ] + C[i - ][j]) % mod;
}
}
}
int T;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d %d", &n, &m);
scanf("%s %s", s + , t + );
for(int i = ; i < n + ; i++) {
for(int j = ; j < m + ; j++) {
dp[i][j] = ;
}
}
//从后往前推,这样便于计算数量
for(int j = m; j > ; j--) {
for(int i = n; i > ; i--) {
dp[i][j] = dp[i + ][j]; //把上一次记录的值加进来
if(s[i] == t[j]) { //当相同时,你就不需要算当前这两个相同的字符的值,并把上一次没有算那两个字符的值加进来
dp[i][j] = (dp[i][j] + dp[i + ][j + ]) % mod;
}
if(s[i] > t[j]) { //当大于时,你就需要找出需要填充的组合数
dp[i][j] = (dp[i][j] + C[n - i][m - j]) % mod;
}
}
}
ll ans = dp[][];
//下面这个循环是找出在s中大于字符串t长度的子串数量
for(int i = ; i <= n; i++) {
if(s[i] == '') { //当第一个字符为0时,不用计算
continue;
}
for(int j = m; j <= n; j++) { //每次需要添加m到n个字符
ans = (ans + C[n - i][j]) % mod;
}
}
cout << ans << endl;
}
return ;
}

G.subsequence 1(dp + 排列组合)的更多相关文章

  1. 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

    [题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...

  2. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas

    [题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...

  3. LightOJ1005 Rooks(DP/排列组合)

    题目是在n*n的棋盘上放k个车使其不互相攻击的方案数. 首先可以明确的是n*n最多只能合法地放n个车,即每一行都指派一个列去放车. dp[i][j]表示棋盘前i行总共放了j个车的方案数 dp[0][0 ...

  4. HDU 5816 状压DP&排列组合

    ---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  5. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

  6. ACdream 1412 DP+排列组合

    2-3 Trees Problem Description 2-3 tree is an elegant data structure invented by John Hopcroft. It is ...

  7. 【noi 2.6_9288】&【hdu 1133】Buy the Ticket(DP / 排列组合 Catalan+高精度除法)

    题意:有m个人有一张50元的纸币,n个人有一张100元的纸币.他们要在一个原始存金为0元的售票处买一张50元的票,问一共有几种方案数. 解法:(学习了他人的推导后~) 1.Catalan数的应用7的变 ...

  8. 【BZOJ-1974】auction代码拍卖会 DP + 排列组合

    1974: [Sdoi2010]auction 代码拍卖会 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 305  Solved: 122[Submit ...

  9. HDU 5151 Sit sit sit 区间DP + 排列组合

    Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...

随机推荐

  1. 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...

  2. Scala学习二——控制结构和函数

    一.if表达式有值 val s=if(x>0) 1 else -1,相当于Java中x>0?1:-1(不过不拿呢个在?:中插入语句),而且Scala中可以用混合类型(如if (x>0 ...

  3. Windows2003服务器IIS启用Gzip压缩的设置

    http://jingyan.baidu.com/article/148a192178ec834d71c3b12b.html     步骤 1 2 3 本文介绍的HTTP压缩方式,采用的是Window ...

  4. SpringMVC整体架构

    总结: 1. 用户发起请求到前端控制器(DispatchServlet): 2. 前端控制器没有处理业务逻辑的能力,需要找到具体的模型对象处理(Handler),到处理器映射器中查找Handler对象 ...

  5. Python多线程异步任务队列

    原文地址 python的多线程异步常用到queue和threading模块 #!/usr/bin/env python # -*- coding: UTF-8 -*- import logging i ...

  6. VMware安装CentOS6.3

    这里测试的是 CentOS-6.3-x86_64-bin-DVD1 链接:点击进入提取码: zs32 如果这里CentOS下载太慢的话 点击进入下载

  7. springboot启动端口占用问题,报错org.apache.catalina.LifecycleException: Protocol handler start failed

    解决办法,找到被占用的端口

  8. deep_learning_Function_tf.control_dependencies([])

    tf.control_dependencies([])函数含义及使用 2019.02.23 14:01:14字数 60阅读 420 tf.control_dependencies([controls_ ...

  9. 解决引导内核遇到undefined instruction的错误

    其实在上一篇随笔之前,就是在启动linux 内核的时候,出了点问题 刚Starting kernel ...就出现了undefined instrction,这是什么问题呢? 在网上也搜了不少资料,有 ...

  10. Hive Server2(五)

    HiveServer2 基本概念介绍 1.HiveServer2基本介绍 HiveServer2 (HS2) is a server interface that enables remote cli ...