又一道lcs题目,这里要注意的是,字符串可能含有空格...
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 1001
int dp[MAXN][MAXN];
char stra[MAXN], strb[MAXN];
int lcs(void)
{
int len_a(strlen(stra)), len_b(strlen(strb));
for(int i = 1; i <= len_a; i ++) {
for(int j = 1; j <= len_b; j ++) {
if( stra[i-1] == strb[j-1] ) {
dp[i][j] = dp[i-1][j-1]+1; continue;
}
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[len_a][len_b];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
while( NULL != gets(stra) ) {
gets(strb);
memset(dp, 0, sizeof(dp)); printf("%d\n", lcs());
}
return 0;
}
uva_10405 Longest Common Subsequence
最新推荐文章于 2018-12-02 18:10:28 发布