Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences:
abcdgh
aedfhr
is adh of length 3.
Input consists of pairs of lines. The first line of a pair contains the first string and the second line contains the second string. Each string is on a separate line and consists of at most 1,000 characters
For each subsequent pair of input lines, output a line containing one integer number which satisfies the criteria stated above.
Sample input
a1b2c3d4e
zz1yy2xx3ww4vv
abcdgh
aedfhr
abcdefghijklmnopqrstuvwxyz
a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p0q0r0s0t0u0v0w0x0y0z0
abcdefghijklmnzyxwvutsrqpo
opqrstuvwxyzabcdefghijklmn
Output for the sample input
4
3
26
14
其实思路还是很简单的,就是边界的问题有点糊涂了,看看别人的文章突然醒悟过来,囧完了=。=
还有那个超时问题,以前也写个while (1)再跳出来的题,呜呜,被笑话了,最后老老实实改了,这题写得我心酸酸的。
LCS(s1, s2) =
{ max( LCS(sub1, s2), LCS(s1, sub2) ) , when e1 != e2
{ LCS(sub1, sub2) + e1 , when e1 == e2
这个挺重要的,我好像还没有学会推出这个式子。。明天再战!
#include<iostream>
#include<cstring>
#include<cstdio>
#define max(x,y) x>y?x:y;
using namespace std;
char str1[1050];
char str2[1050];
int a[1050][1050];//保存数
int main()
{
int i,j;
int len1,len2;
while (gets(str1))
{
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i-1] == str2[j-1])
{
a[i][j] = a[i-1][j-1]+1;
}
else
{
a[i][j] = max(a[i-1][j],a[i][j-1]);
}
}
}
cout << a[len1][len2] << endl;
}
return 0;
}
当我们只要求出lcs的长度而不需要打印出lcs时,我们还可以更加节省记录的空间。事实上,我们需要知道当前位置及其左上,左方,上方的值即可,也就是我们只要2行的二维数组即可(此时的计算顺序是每行从左到右,一行一行一次算),再交替使用这个二维数组即可。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char str1[1050];
char str2[1050];
int a[1050][1050];
int prev[2][1050];
int main()
{
int len1,len2,i,j;
while (gets(str1))
{
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
for (i = 0; i < 2; i++)
{
for (j = 0; j < len2; j++)
{
a[i][j] = 0;
}
}
for (i = 1; i <= len1; i++)//行
{
for (j = 1; j <= len2; j++)//列
{
if (str1[i-1] == str2[j-1])
{
a[2][j] = a[2][j-1]+1;
a[1][j] = a[2][j];
}
else
{
a[2][j] = max(a[2][j-1],a[1][j]);
a[1][j] = a[2][j];
}
}
}
cout << a[2][len2] << endl;
}
return 0;
}
如果要把lcs打印出来,我们就用一个二维数组回溯记录是从什么方向来的。
完整代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char str1[1050];
char str2[1050];
int a[1050][1050];
int prev[1050][1050];
void print_lcs(int i,int j)
{
if (!i || !j)
{
return;
}
if (prev[i][j] == 1)
{
print_lcs(i-1,j-1);
cout << str1[i-1];
}
if (prev[i][j] == 2)
{
print_lcs(i-1,j);
}
if (prev[i][j] == 3)
{
print_lcs(i,j-1);
}
}
int main()
{
int len1,len2,i,j;
while (gets(str1))
{
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i-1] == str2[j-1])
{
a[i][j] = a[i-1][j-1]+1;
prev[i][j] = 1;
}
else
{
if (a[i-1][j] >= a[i][j-1])
{
a[i][j] = a[i-1][j];
prev[i][j] = 2;
}
else
{
a[i][j] = a[i][j-1];
prev[i][j] = 3;
}
}
}
}
cout << a[len1][len2] << endl;
cout << "lcs为"<< endl;
print_lcs(len1,len2);
cout << endl;
}
return 0;
}