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. Java中创建的对象多了,必然影响内存和性能

    1, Java中创建的对象多了,必然影响内存和性能,所以对象的创建越少越好,最后还要记得销毁.

  2. MySQL中的SQL的常见优化策略

    MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 1 避免全表扫描对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索 ...

  3. SQL数据库字段数据类型详细说明

    这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:20 ...

  4. java实现spark常用算子之distinct

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  5. Express multer 文件上传

    npm multer 文件上传 Express app 范本就不写了,仅记录一下上传部分的代码. const fs = require('fs'); const express = require(' ...

  6. python 装饰器,生成器,迭代器

    装饰器 作用:当我们想要增强原来已有函数的功能,但不想(无法)修改原函数,可以使用装饰器解决 使用: 先写一个装饰器,就是一个函数,该函数接受一个函数作为参数,返回一个闭包,而且闭包中执行传递进来的函 ...

  7. Linux中环境变量文件profile、bashrc、bash_profile之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. 英文描述为: # /etc/pr ...

  8. linux centos 7安装 apache php 及mariadb

    1安装Apache, PHP, MySQL以及php库组件. yum -y install httpd php mysql  php-mysql 2 安装apache扩展 yum -y install ...

  9. 华为服务器XH628配置软RAID

    1.       硬RAID 1.1.       配置准备 本机型号为华为XH628,配有两块400GSSD,12块1.2TSAS盘.其中2块SSD做RAID1为系统盘,12块SAS盘做RAID5, ...

  10. usb四种传输模式bulk

    当USB插入USB总线时,USB控制器会自动为该USB设备分配一个数字来标示这个设备.另外,在设备的每个端点都有一个数字来表明这个端点.USB设备驱动向USB控制器驱动请求的每次传输被称为一个事务(T ...