POJ 1458 Common Subsequence
来源:互联网 发布:淘宝商城希恩卫浴 编辑:程序博客网 时间:2024/06/11 13:36
题目链接: http://poj.org/problem?id=1458
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
abcfbc abfcabprogramming contest abcd mnp
Sample Output
420
Hint: 这个问题是被称为最长公共子序列问题(LCS,Longest Common Subsequence)的著名问题。不妨用如下定义:
dp[i][j]:=S1....Si 和t1...tj 对应的LCS的长度
由此,S1...Si+1 和 t1....t j+1 的公共列可能是
当Si+1 == t j+1 时,在S1....Si 和t1...tj的公共子列末尾追加上Si+1
S1....Si 和 t1....tj+1 的公共子列
S1...Si+1 和 t1...t j 的公共子列
三者中的某一个。所以就有如下的递推关系式成立:
dp[i+1][j+1]= dp[i][j]+1 (当Si+1==t j+1)
max (dp[i][j+1],dp[i+1][j]) (其他)
这个递推式可用O(nm)计算出来,dp[n][m]就是LCS的长度。
代码如下:
#include<iostream>#include<fstream>#include<vector>#include<string>#include<map>#include<iterator>#include<algorithm>#include<numeric>#include<cmath>#include<sstream>#include<bitset>using namespace std;const int MaxLen = 1000;int dp[MaxLen][MaxLen];int main(){ #ifdef ONLINE_JUDGE #else freopen("D:\\in.txt", "r", stdin); freopen("D:\\out.txt", "w", stdout); #endif // ONLINE_JUDEG string s1, s2; while (cin >> s1 >> s2) { int n = s1.size(); int m = s2.size(); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s1[i] == s2[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } cout << dp[n][m] << endl; } return 0;}
另外一种思路:
#include<iostream>#include<fstream>#include<vector>#include<string>#include<map>#include<iterator>#include<algorithm>#include<numeric>#include<cmath>#include<sstream>#include<bitset>using namespace std;int LongestCommonSubsequenceLength(const std::string& first, const std::string& second);int main(){ #ifdef ONLINE_JUDGE #else freopen("D:\\in.txt", "r", stdin); freopen("D:\\out.txt", "w", stdout); #endif // ONLINE_JUDEG string s1; string s2; int num(0); while (cin >> s1 >> s2) { num = LongestCommonSubsequenceLength(s1, s2); cout << num << endl; } return 0;}//求两个字符串的最长公共子序列int LongestCommonSubsequenceLength(const std::string& first, const std::string& second){ const std::string& longer = first.size() > second.size() ? first: second; const std::string& shorter = first.size() > second.size() ? second : first; int longer_len = longer.size(); int shorter_len = shorter.size(); std::vector<int> previous(shorter_len + 1, 0); std::vector<int> current(shorter_len + 1, 0); int foo = previous.size() + current.size(); for (int i = 0; i < longer_len; i++) { for (int j = 0; j < shorter_len; j++) { if (toupper(longer[i]) == toupper(shorter[j])) { current[j + 1] = previous[j] + 1; } else { current[j + 1] = std::max(current[j], previous[j + 1]); } } for (int j = 0; j < shorter_len; j++) { previous[j + 1] = current[j + 1]; } } return current[shorter_len];}
0 0
- Common Subsequence--poj--1458
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- Poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ-1458-Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- Cocos2d-x HelloLua 介绍
- 离职
- Ganymed SSH-2 for Java系列6之删除远程服务器上的非空目录
- 社会工程学的特点和趋势
- VO、DTO、DO、PO的概念、差别和用处
- POJ 1458 Common Subsequence
- Linux目录结构
- Jsp和js倒计时跳转页面
- 在同一网关下ping不通其他电脑
- Android 自定义 地图 室内
- Lunix下MySql的安装及启动
- 有关字符串函数
- Java Collection 集合类和Java Map接口
- 一个新的开始,一个旧的结束,致我最爱的青春