poj 1458 Common Subsequence

// 题意:求两个字符串的最长公共子序列
#include<iostream> //最长公共子序列
#include<string>

using namespace std;
string str1,str2;
int f[500][500];
int lcs(int i,int j)
{
if(i==-1||j==-1)
return 0;
if(f[i][j]!=-1)
return f[i][j];
else if(str1[i]==str2[j])
f[i][j]=lcs(i-1,j-1)+1;
else
f[i][j]=max(lcs(i-1,j),lcs(i,j-1));
return f[i][j];
}
int main()
{
while(cin>>str1>>str2)
{
memset(f,-1,sizeof(f));
cout<<lcs(str1.size()-1,str2.size()-1)<<endl;
}
return 0;
}

posted on 2011-07-22 16:28  sysu_mjc  阅读(103)  评论(0编辑  收藏  举报

导航