题目链接

题意:给你两个字符串都是数字,让你求第一个字符串的子序列中大于第二个字符串的个数。

思路:dp[i][j] 表示 str1的前i个,匹配 str2的前 j 个的种类数,那么 if(s[i] == t[j]) dp[i][j] = dp[i -1][j] + dp[i - 1][j - 1]; else dp[i][j] = dp[i - 1][j];  对于长度大于 t 的没有前导0的都符合,那么就看长度等于t的就可以了,当匹配到 i, j 的时候,if(s[i] > s[j]) 那么该贡献为:前面匹配j-1的种类数*后面随便选len2-j个,即当前的贡献就为dp[i - 1][j - 1] * C[len1 - i][len2 - j]。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#define ll long long
using namespace std;
const int mod=;
char str1[],str2[];
int dp[][];
int C[][];
int main()
{
int t;
C[][] = C[][] = C[][] = ;
for(int i = ; i <= ; i++) {
C[i][] = ;
for(int j = ; j <= i; j++)
C[i][j] = (C[i - ][j] + C[i - ][j - ]) % mod;
}
scanf("%d",&t);
while(t--)
{
int n,m;
long long sum=;
scanf("%d%d",&n,&m);
scanf("%s%s",str1+,str2+);
int len1=strlen(str1+);
int len2=strlen(str2+);
for(int i=;i<=len1;i++)
dp[i][]=;
for(int i=;i<=len1;i++)
{
for(int j=;j<=min(len2,i);j++)
{
dp[i][j]=dp[i-][j];
if(str1[i]==str2[j])
dp[i][j]=(dp[i][j]+dp[i-][j-])%mod;
if(str1[i]>str2[j])
{
sum=(sum+(ll)dp[i-][j-]*C[len1-i][len2-j])%mod;
}
}
}
for(int i=;i<=len1;i++) {
if(str1[i]=='')
continue;
for(int j=len2;j<=len1-i;j++)
sum=(sum+C[len1-i][j])%mod;
}
printf("%lld\n",sum);
}
}

subsequence 1的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  10. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. 二叉查找树BST

    每棵子树头节点的值都比各自左子树上所有节点值要大,也都比各自右子树上所有节点值要小. 二叉查找树的中序遍历序列一定是从小到大排列的. 一个节点的后继节点是指,这个节点在中序遍历序列中的下一个节点.相应 ...

  2. mac使用相关笔记

    1.软件提示已损坏,需要移到废纸篓的解决方法 -> sudo spctl --master-disable xattr -r -d com.apple.quarantine <path&g ...

  3. 建站手册-网站建设: Web 标准

    ylbtech-建站手册-网站建设: Web 标准 1.返回顶部 1. http://www.w3school.com.cn/site/site_standards.asp 2. 2.返回顶部 1. ...

  4. 命令连接redis

    cd到redis的bin目录 ./redis-cli -h 输入info,看是否要验证 清空所有key FLUSHALL

  5. 测开之路三十八:css布局之定位

    常用的布局方式: static:静态定位(默认),什么都不用管,元素会按照默认顺序排列,排不下是会默认换行relative:相对定位(同一层),相对于某一个元素进行定位fixed:绝对定位,指定位置a ...

  6. git 忽略提交及已push过得文件忽略提交

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交 Git 忽略文件提交的方法 这种方式通过在项目的某个文件 ...

  7. django-3-视图(views.py)与网址(urls.py)

    视图与网址 操作文件:urls.py.views.py urls.py 作用:用于处理前台的链接(如前台访问:127.0.0.1:8080/index/1212/21212),其实永远访问的是同一个文 ...

  8. 用webdriver模仿浏览器 爬取豆瓣python书单

    用webdriver模仿浏览器 爬取豆瓣python书单 其中运用到os 模块 作用是生成文件夹 存储爬取的信息 etree 用于xpath解析内容 详细代码如下 可用我的上一篇博客存取到excel当 ...

  9. spring data jpa 一对多查询

    在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...

  10. Linux系统ubuntu17安装jdk8并配置环境变量

    上班多年,一直没有真正在Linux下开发过,没有捣鼓到Linux服务器,成为憾事.最近由于想学习Python,于是开始看书,学习Linux,学习shell编程. 选择Linux,先从最简单的ubunt ...