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. @RequestMapping-@PathVariable小误区

    去掉勾选就可以演示出错误了,一般勾选是为了方便我们Debug调试 会出现500错误: 正确的写法:

  2. luogu P4006 小 Y 和二叉树

    luogu loj 可以发现度数\(< 3\)的点可以作为先序遍历的第一个点,那么就把度数\(< 3\)的编号最小的点作为第一个点.然后现在要确定它的左右儿子(或者是右儿子和父亲).我们把 ...

  3. java web实现同一账号在不同浏览器不能同时登录

    网上看了很多方法,个人也看了,自己也总结了几个比较常用的: 前提都是用session监听器,对session的创建与销毁进行监听 一.在用户登录时保存该用户的状态有这几种保存方式: 1.保存到内存中( ...

  4. echarts图表数据信息动态获取

    第一步准备一个json文件echarts.json(名字无所谓),用来模拟从后台获取数据 { "name":["直达","营销广告",&qu ...

  5. Slimvoice能代替JavaScript?

    对于Slimvoice(https://slimvoice.co/),我想反对JavaScript的炒作,并对整个应用程序进行服务器端渲染.您可能会说:“用户必须在使用应用程序时重新加载每个页面,这必 ...

  6. vue项目-axios封装、easy-mock使用

    vue全家桶概括下来就是 项目构建工具(vue-cli) 路由(vue-router) 状态管理(vuex) http请求工具 vue有自己的http请求工具插件vue-resource,但是vue2 ...

  7. 直通BAT必考题系列:JVM性能调优的6大步骤,及关键调优参数详解

    JVM内存调优 对JVM内存的系统级的调优主要的目的是减少GC的频率和Full GC的次数. 1.Full GC 会对整个堆进行整理,包括Young.Tenured和Perm.Full GC因为需要对 ...

  8. 线程安全的Singleton要点

    1.privat static Singleton 要加votatile关键字修饰,防止对象的初始化代码与引用赋值代码进行重排序. 2.getInstance方法,最外层要加if (instance ...

  9. linux命令详解——umask

    当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是umask干的事情.umask设置了用户创建文件的默认 权限,它与chmod的效果刚好相反,umask设置的是权限& ...

  10. Delphi 监视数据的值