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. Collection<E>接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html public interface Collection<E ...

  2. oa_mvc_easyui_后台布局(3)

    1.新建HomeController控制器,并创建视图,后台的主页 2.easyUI的引用: <link href="~/Content/default/easyui.css" ...

  3. PHP 网站大数据大流量与高并发 笔记

    前端: 1.域名开启cdn 2.大文件使用oss php: 1.模板编译缓存 服务器: 1.负载均衡 数据库: 1.读写分离 待完善

  4. pip 报错找不到pip问题

    具体报错如下 解决办法: wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificate 使用当前python3运行

  5. 什么是DDoS攻击?

    本文转载自知道创宇云安全的知乎回答:DDoS 的肉鸡都是哪来的? 说到DDoS攻击,我们就不得不说“肉鸡”. “肉鸡”可谓是DDoS攻击的核心大杀器,作为一个要发起DDoS攻击的黑客来说,没有肉鸡就是 ...

  6. shell脚本中的一些特殊符号

    在shell中常用的特殊符号罗列如下:  # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $*  \"string\"* **  ...

  7. 代码报错--------EOFError: Compressed file ended before the end-of-stream marker was reached

    背景:运行LeNet识别CIFAR-10的图像的代码时,报错: EOFError: Compressed file ended before the end-of-stream marker was ...

  8. Linux centos :root密码忘记怎么办?

    1 重启系统后出现GRUB界面在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入“e” 来进入编辑模式. 2 接下来你可以看到如下图所示的画面,然后你再用上下键选择最新的内核(这里是第二行 ...

  9. dfs序 线段树 dfs序列 主席树

    并查集 #include<stdio.h> ]; void sset(int x) { ;i<=x;i++) stt[i]=i; } int ffind(int x) { if(x= ...

  10. Steiner tree

    Gym - 101908J Joining Capitals #include<bits/stdc++.h> using namespace std; typedef long long ...