【动态规划】subsequence 1
题目链接:https://ac.nowcoder.com/acm/contest/885/G
题意:
两个串,s t,求s的所有子串中大于 t 的数目
题解:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N = 3005; 5 const ll mod = 998244353 ; 6 char s[N],t[N]; 7 ll C[N][N]; 8 ll dp[N][N]; 9 int main() 10 { 11 //打表杨辉三角 12 C[0][0] = 1 ; 13 C[1][0] = C[1][1] = 1 ; 14 for(int i=2;i<=3000;i++){ 15 C[i][0] = 1 ; 16 for(int j=1;j<=i;j++){ 17 C[i][j] = C[i-1][j-1] + C[i-1][j] ; 18 if(C[i][j]>=mod) C[i][j] -= mod ; 19 } 20 } 21 22 int T,n,m; 23 for( scanf("%d",&T) ; T ; T-- ){ 24 scanf("%d%d",&n,&m); 25 scanf("%s%s",s+1,t+1); 26 27 for(int i=0;i<=n;i++){ 28 dp[i][0] = 1 ; 29 } 30 //计算长度相同时,某一位置比t的位置大,对答案的贡献 31 ll ans = 0 ; 32 for(int i=1;i<=n;i++){ 33 for(int j=1;j<=min(i,m);j++){ 34 dp[i][j] = dp[i-1][j] ; 35 if( s[i] == t[j] ){ 36 dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % mod ; 37 } 38 else if( s[i] > t[j] ){ 39 ans = (ans + (ll)dp[i-1][j-1] * C[n-i][m-j]) %mod ; 40 } 41 } 42 } 43 //计算长度大于t串长度时,对答案的贡献 44 for(int i=1;i<=n;i++){ 45 if( s[i] =='0' ) continue ; 46 for(int j=m;j<=n-i;j++){ 47 ans = (ans + C[n-i][j])%mod ; 48 } 49 } 50 printf("%lld\n",ans); 51 } 52 return 0; 53 }
View Code
转载于:https://www.cnblogs.com/Osea/p/11297874.html
【动态规划】subsequence 1相关推荐
- 动态规划—最长公共子序列问题 HDU-1159 Common Subsequence
动态规划-最长公共子序列问题 Common Subsequence [ HDU - 1159 ] A subsequence of a given sequence is the given sequ ...
- leetcode 376. Wiggle Subsequence | 376. 摆动序列(动态规划)
题目 https://leetcode.com/problems/wiggle-subsequence/ 题解 刷题大概确实是有效果的吧- 印象中,这算是今日第二道,全局第三道没看答案写出来的 dp ...
- HDU 1159 Common Subsequence 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...
- 【动态规划 回文串11】LeetCode 516. Longest Palindromic Subsequence
LeetCode 516. Longest Palindromic Subsequence 本博客转载自:http://www.cnblogs.com/grandyang/p/6493182.html ...
- Rosalind Java|Longest Increasing Subsequence动态规划算法
Rosalind编程问题之计算集合中最长的递增元素子集. Longest Increasing Subsequence Problem: A subsequence of a permutation ...
- Codeforces 1000D Yet Another Problem On a Subsequence 动态规划
D. Yet Another Problem On a Subsequence time limit per test 2 seconds memory limit per test 256 mega ...
- LeetCode Increasing Triplet Subsequence(动态规划)
问题:问数组中是否存在一个上升的三元子序列 思路:方法一是使用动态规划求解上升子序列,如果子序列长度等于3,说明存在 方法二是使用small,mid分别记录最小值,第二小值,如果当前值比small小则 ...
- LeetCode Longest Increasing Subsequence(动态规划、二分法)
问题:求数组的最长上升子序列问题 思路:第一种方法使用动态规划方法,用dp(i)来表示从0到i之间的最长上升子序列的长度.状态转移方程为dp(i)=max{dp(j)+1},其中0<=j< ...
- leetcode 300. Longest Increasing Subsequence | 300. 最长递增子序列(动态规划)
题目 https://leetcode.com/problems/longest-increasing-subsequence/ 题解 难得有官方题解的一道题. 参考:https://leetcode ...
- HDU 1159.Common Subsequence【动态规划DP】
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
最新文章
- leetcode 46 java,leetcode46.java
- DBMS_STATS.GATHER_TABLE_STATS详解
- 2021.12.16自制齿条
- Eating Soup
- 下载kaggle数据集的小妙招
- 去黑头的7个必胜秘方
- jzoj4049-排序【搜索】
- 阿里P8高级架构师教你如何通过BAT面试顺利拿到offer
- Gson 与 fastJson 在使用上的差异(fastJson的优点)
- 电子系统中的品质因数
- 【学习笔记】程序员学操作系统
- java中check和uncheck异常处理
- 成功男友因具备的品质
- Orcad Capture CIS
- 计算机专业2017大学排名,全国计算机专业大学排名2017计算机专业大学排名
- 关于电影票对接公众号cps
- Xming(windows下的X Server)的使用,在windows下运行你的终端和所有基于XWindow的程序
- java8和java9的对比
- OpenCL编程初探
- SUST OJ 1641: 电子狗的心事