Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21529 Accepted Submission(s): 9351
Problem 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.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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
【题意】
就是最长公共子序列。
(看的时候最好画个表帮助理解)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dp[1000][1000];//dp[i][j]表示由s1的前i位与s2的前j位所得到的最长公共子序列长度(注意dp从1开始,字符串下标从0开始)
int main()
{
char s1[1000],s2[1000];
while(scanf("%s%s",s1,s2)!=EOF)
{
memset(dp,0,sizeof(dp));
int i,j,lenth1=strlen(s1),lenth2=strlen(s2);
for(i=0;i<lenth1;i++)
{
for(j=0;j<lenth2;j++)
{
if(s1[i]==s2[j])//若s1的第i(从0开始)位与s2的第j位相匹配。
dp[i+1][j+1]=dp[i][j]+1;//则s1的前i位与s2的前j位所得到的最长公共子序列长度=s1的前i-1位与s2的前j-1位所得到的最长公共子序列长度+1
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);//不匹配的话就有两种情况,取最大值就可以了。
}
}
printf("%d\n",dp[lenth1][lenth2]);
}
return 0;
}
注意到:
dp[i][j]取值只与dp[i-1][j-1],dp[i-1][j],dp[i][j-1]相关。即只与当前行,上一行有关。那么就想到是否可以像一般的动态规划一样用一维dp[1000](表示字符串s1,与dp[j](s2的前i位组成的字符串的最长公共子序列长度),通过不断更新dp[1000]来得到最后的结果呢?
假设可以,观察这两个表达式 dp[i-1][j-1] dp[i][j-1]。当我们计算完dp[j-1]后,丢失了dp[i-1][j-1]的数据,当计算dp[j]的时候,我们可能无法得到正确的结果。
得证,无法用一个一维数组dp[N]是程序算出
正确
结果。又观察到dp[i][j]的确只与当前行,上一行有关,就想到用两个数组来分别存储当前dp[N]、上一行dp[N]。这样就可以写出正确的动态转移方程。
即:
if(s1[i]==s2[j])
dp[当前行][j+1]=dp[上一行][j]+1;
else
dp[当前行][j+1]=max(dp[当前行][j],dp[上一行][j+1]);
滚动数组
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dp[2][1000];//滚动数组,dp[0],dp[1]交替表示上一次,当前的dp。
int main()
{
char s1[1000],s2[1000];
while(scanf("%s%s",s1,s2)!=EOF)
{
memset(dp,0,sizeof(dp));
int i,j,lenth1=strlen(s1),lenth2=strlen(s2);
int e=0;
for(i=0;i<lenth1;i++)
{
e=1-e;//e是当前行
for(j=0;j<lenth2;j++)
{
if(s1[i]==s2[j])
dp[e][j+1]=dp[1-e][j]+1;//1-e是上一行
else
dp[e][j+1]=max(dp[e][j],dp[1-e][j+1]);
}
}
printf("%d\n",dp[e][lenth2]);
}
return 0;
}