//第一道LCS,最开始我以为是简单的遍历字符串就可以,但是提交上去WA了,再看讨论,才知道用LCS的,
//然后就上网找关于LCS的资料,在似懂非懂的情况之下敲了出来!还需要这方面的练习!
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
const int MAXLEN = 1000;
int ans[MAXLEN][MAXLEN];
void LCSLength(string str1, string str2, int ans[][MAXLEN])
{
int i, j, len1, len2;
len1 = str1.length();
len2 = str2.length();
for (i = 1; i <= len1; i++){
for (j = 1; j <= len2; j++){
if (str1[i-1] == str2[j-1]){
ans[i][j] = ans[i-1][j-1] + 1;
}
else if (ans[i-1][j] >= ans[i][j-1]){
ans[i][j] = ans[i-1][j];
}
else
ans[i][j] = ans[i][j-1];
}
}
cout << ans[len1][len2] << endl;
}
int main()
{
string str1, str2;
while (cin >> str1 >> str2){
memset(ans, 0, sizeof(ans));
LCSLength(str1, str2, ans);
}
system("pause");
}
poj 1458 Common Subsequence
最新推荐文章于 2019-07-05 08:49:53 发布