题目链接

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

思路: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. linux0.11内核源码——boot和setup部分

    https://blog.csdn.net/KLKFL/article/details/80730131 https://www.cnblogs.com/joey-hua/p/5528228.html ...

  2. 【HDOJ6595】Everything Is Generated In Equal Probability(期望DP)

    题意:给定一个N,随机从[1,N]里产生一个n, 然后随机产生一个n个数的全排列,求出n的逆序数对的数量并累加ans, 然后随机地取出这个全排列中的一个子序列,重复这个过程,直到为空,求ans在模99 ...

  3. excel 中相乘函数

    excel  中相乘函数   “PRODUCT”并且是公式的框框,格式要是 常规,不能是文本

  4. python练习题之随机生成验证码

    #引用random模块下的randint项目#定义验证码函数.定义一个空字符串变量,分三种情况,随机产生的大写字母,随机产生的小写字母,随机产生的数字.然后#每一次执行哪一种情况,条件也是随机的,就是 ...

  5. 实验三 《敏捷开发与XP实践》实验报告

    一.实验内容 任务一 1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题 ...

  6. 用shell脚本实现MongoDB数据库自动备份

    一.创建MongoDB备份目录 用来存放数据 mkdir -p /data/mongodb_bak/mongodb_bak_now mkdir -p /data/mongodb_bak/mongodb ...

  7. vector的自定义实现

    #pragma warning(disable:4996) #include<iostream> #include<string> #include<vector> ...

  8. Python字节码与解释器学习

    参考:http://blog.jobbole.com/55327/ http://blog.jobbole.com/56300/ http://blog.jobbole.com/56761/ 1. 在 ...

  9. 【html】 两栏对比网页,同时滚动

    有的时候需要左右对比环境,而且希望能同时滚动,如下这么拼接就可以了 <html> <head><meta http-equiv="content-type&qu ...

  10. UVA12174_Shuffle

    Shuffle 大致题意: 你有一个随机播放的播放器,有s首歌,在这s首播放完之前不会重新打乱顺序,现在给出一段只含有1~s的n长度序列,现在问你下次随机排序发生的时间有多少种可能 其实就是问你这个播 ...