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&& ...
随机推荐
- Delphi TextFile读取文本文件
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- (转)Windows下zookeeper安装及配置
转:https://blog.csdn.net/qq_36332827/article/details/79700239 zookeeper有单机.伪集群.集群三种部署方式,可根据自己对可靠性的需求选 ...
- DCloud-MUI-JS:相关摘录
ylbtech-DCloud-MUI-JS:相关摘录 1.返回顶部 1.JS initFun: function() { var oldBack = mui.back; mui.back = func ...
- spring4.1.8扩展实战之三:广播与监听
提到广播与监听,我们常常会想到RabbitMQ.Kafka等消息中间件,这些常用于分布式系统中多个应用之间,有时候应用自身内部也有广播和监听的需求(例如某个核心数据发生变化后,有些业务模块希望立即被感 ...
- 用 Flask 来写个轻博客 (23) — 应用 OAuth 来实现 Facebook 第三方登录
目录 目录 前文列表 扩展阅读 第三方登录流程 OAuth 应用 OAuth 实现 Facebook 第三方登录 实现效果 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask ...
- apr not found,APR-util not found,pcre,
1.下载所需软件包(此下载链接经由作者验证可使用): wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz wget http://arch ...
- COALESCE 函数作用
用途. 将空值替换成其他值 返回第一个非空值. 任意一个不为空的值.比较有用.
- 指针的value
全局变量的默认初始化值为0,char为空字符——'\0',string为"\0 " char c; string s; ; int main() { cout << p ...
- inline-block,inline,block,table-cell,float
float:left ---------------------------------------------------------------------------------------- ...
- js获取下拉框的value值
var Resultstr=""; var param = { action: "MoneyList" };//参数拼接 $.ajax({ type: &quo ...