Light oj 1157 - LCS Revisited(lcs 个数 记忆化搜索)
来源:互联网 发布:义乌哪里有淘宝培训 编辑:程序博客网 时间:2024/04/24 20:06
LCS means 'Longest Common Subsequence' that means two non-empty strings are given; the longest subsequence that are common. Subsequence means removing 0 or more characters from a string.
Now you are given two non-empty strings s and t, your task is to find the number of distinct LCS of s and t. Since the result can be very big, print the result modulo 1000007.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains two lines, the first line is the string s and the second line is the string t. You may assume that the strings are non-empty and consist only of lowercase letters and the length of the each string is at most 1000.
Output
For each case, print the case number and the number of distinct LCS of s and t modulo 1000007.
Sample Input
Output for Sample Input
4
acbd
acbd
vnvn
vn
ab
ba
xyz
abc
Case 1: 1
Case 2: 1
Case 3: 2
Case 4: 1
/*参考:http://www.xuebuyuan.com/zh-tw/2100947.html*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef long long ll;using namespace std;#define mod 1000007#define INF 0x3f3f3f3f#define N 1005int f[N][N],dp[N][N];int len,lena,lenb;char a[N],b[N];int last1[N][N];int last2[N][N];void inint(){ int i,j; memset(dp,0,sizeof(dp)); for(i=1;i<=lena;i++) for(j=1;j<=lenb;j++) if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}void pre(){ int i,j; memset(last1,0,sizeof(last1)); memset(last2,0,sizeof(last2)); for(int i=1;i<=lena;i++) for(char c='a';c<='z';c++) { last1[i][c]=last1[i-1][c]; if(a[i]==c) last1[i][c]=i; } for(int i=1;i<=lenb;i++) for(char c='a';c<='z';c++) { last2[i][c]=last2[i-1][c]; if(b[i]==c) last2[i][c]=i; }}int dfs(int lena,int lenb,int len){ if(len<=0) return 1; if(f[lena][lenb]!=-1) return f[lena][lenb]; int ans=0; if(lena>0&&lenb>0) { for(char c='a';c<='z';c++) { int t=last1[lena][c]; int tt=last2[lenb][c]; if(dp[t][tt]>=len) ans+=dfs(t-1,tt-1,len-1); ans%=mod; } } return f[lena][lenb]=ans;}int main(){ int i,j,t,ca=0; scanf("%d",&t); while(t--) { scanf("%s%s",a+1,b+1); lena=strlen(a+1); lenb=strlen(b+1); inint(); pre(); memset(f,-1,sizeof(f)); printf("Case %d: %d\n",++ca,dfs(lena,lenb,dp[lena][lenb])); } return 0;}
- Light oj 1157 - LCS Revisited(lcs 个数 记忆化搜索)
- Light OJ 1157 LCS Revisited (DP)
- Light oj 1110 - An Easy LCS(LCS)
- light oj 1159 - Batman LCS
- light oj An Easy LCS
- light oj 1013 LCS 应用
- 10723Cyborg Genes (LCS + 记忆化搜索)
- 记忆化搜索之LCS实践
- loj 1013(LCS+记忆化搜索)
- lightoj 1157 - LCS Revisited LCS求方案数
- Light OJ 1110 - An Easy LCS (LCS+字典序最小路径记录)
- light oj 1110 LCS 记录路径
- Light oj 1159 - Batman(三维 LCS)
- Light oj 1110 LCS带打印路径
- light oj 1033 - Generating Palindromes (LCS)
- light oj 1033 - Generating Palindromes 【LCS】
- poj 2250 Compromise (DP:LCS加强版+记忆化搜索)
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
- HDU 5443 The Water Problem(RMQ)2014 多校
- intellij IDEA如何将Web项目导出为WAR包,直接部署到tomcat或者Jboss上
- 转:cygwin简单应用及Nutch之Crawler工作流程
- 阶段性总结
- 黑马程序员--Java基础--02-基础语法4
- Light oj 1157 - LCS Revisited(lcs 个数 记忆化搜索)
- 将容器中的零放在容器的后面,前面的数据顺序不变
- 黑马程序员——JAVA之面向对象(一)
- PHP+Sphinx+Mysql做搜索引擎
- 有关无法打开预编译头文件错误的思考
- ListView的9个重要属性
- bootstrap.css和esayUi.css样式冲突的解决
- hdu4740The Donkey of Gui Zhou dfs水题
- 不能约束两个视图,在没有一个公共superview的情况下