subsequence 1
题意:给你两个字符串都是数字,让你求第一个字符串的子序列中大于第二个字符串的个数。
思路: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的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
随机推荐
- 【SPOJ1811】Longest Common Substring(后缀自动机)
题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度 n<=250000 思路: #include<bits/stdc++.h> using namespace std; t ...
- APP测试之-网址
App测试那么多机型怎么搞? http://www.jianshu.com/p/1a9aa2cf0d85 移动App的分类 http://www.jianshu.com/p/01f5db8958d2 ...
- [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)
题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...
- 【SpringBoot】 一种解决接口返回慢的方式
前言 使用springboot开发后台代码的时候,很核心的一个功能是为前端提供接口,那么很可能你会遇到如下问题: 1. 接口里面调用的service层是第三方库或者第三方后台程序,导致访问很慢. 2. ...
- selenium2-java 浏览器的三种弹窗处理
alert弹窗 confirm弹窗 prompt弹窗 点击确定 // 选取警告弹窗 Alert alert=driver.switchTo().alert(); ...
- 配置github的pull request触发jenkins自动构建
参照: https://www.cnblogs.com/zanjiahaoge666/p/6402738.html 之前的配置,都是向master分支push操作触发jenkins进行构建,但是在一般 ...
- 测开之路四十七:Django之请求静态资源与模板
框架必要的配置 import sysfrom django.conf.urls import urlfrom django.conf import settingsfrom django.http i ...
- Bootstrap+Ajax+HTML 动态表格分页
1.HTML页面:(bootstrap的模态框) 触发模态框: <div class="col-lg-6"> <div class="input-g ...
- 正则findall的使用
import re title = 'hello, 你好,world' print(title) title = u'hello, 你好,world' print(title) #汉字匹配 +的意思是 ...
- 转 使用Python的logging.config.fileConfig配置日志
Python的logging.config.fileConfig方式配置日志,通过解析conf配置文件实现.文件 logglogging.conf 配置如下: [loggers]keys=root,f ...