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
原创粉丝点击