poj1458 求最长公共子序列 经典DP
2016-04-01 16:04
423 查看
Common Subsequence
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 45763 | Accepted: 18737 |
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0 题目大意:给你两个字符串,求两个字符串最长公共子串的长度,结合题意应该很容易理解题意。 思路分析:啥也不说,经典DP,首先确定状态以及状态的储存方法,用一个二维数组f 来进行储存,f[i][j]表示第一个字符串的前i位与后一个字符串的前j位最长公共 子序列长度,然后需要思考状态如何进行转移,若s1[i]==s2[j],f[i][j]=f[i-1][j-1]+1;若s1[i]!=s2[j],则f[i][j]应该与f[i][j-1]或者f[i-1][j]一致,具体 看那一个大,即f[i][j]=max{f[i][j-1],f[i-1][j]},这样状态转移方程也确定了。在代码实现的时候需要注意几点,首先字符串长度并没有给你,开到210就可以了,另外 初始状态要合理进行处理,否则会出现数组越界的错误! 代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <cmath> using namespace std; const int maxn=210; char s1[maxn],s2[maxn]; int dp[maxn][maxn]; int main() { int i,j; while(scanf("%s%s",s1,s2)!=EOF) { int l1=strlen(s1); int l2=strlen(s2); for(i=0;i<l1;i++) { for(j=0;j<l2;j++) { if(s1[i]==s2[j]) { if(i>=1&&j>=1) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=1; } else { if(i>=1&&j>=1) dp[i][j]=max(dp[i-1][j],dp[i][j-1]); else if(j>=1) dp[i][j]=dp[i][j-1]; else if(i>=1) dp[i][j]=dp[i-1][j]; else dp[i][j]=0; } } } int t=dp[0][0]; for(i=0;i<l1;i++) for(j=0;j<l2;j++) if(t<dp[i][j]) t=dp[i][j]; cout<<t<<endl; } return 0; }
相关文章推荐
- tomcat安全配置
- java多线程的同步和死锁
- Java —— Hibernate4 No Session found for current thread
- java设计模式之创建型模式-工厂方法模式
- Android 之ActivityThead、ActivityManagerService 与activity的管理和创建
- C++访问限定符
- pig语法学习 FOREACH GENERATE group AS
- 大O符号与时间复杂度
- winpython安装与使用
- 导出数据到Excel --使用ExcelReport有感
- js中对字符串的操作
- ARM exception
- 【转载】循环结构中break、continue、return和exit的区别
- Activity的启动流程
- js循环绑定事件解决方案
- ArcGIS 帮助(10.2、10.2.1 和 10.2.2)收集
- 社会化分享 使用as
- Tcpdump抓包工具
- 老男孩linux决心书
- Spring中的用到的设计模式大全