UVa 10405 - Longest Common Subsequence
/*
最长 公共子序列 记得用 gets();
用scanf坑了一次
*/
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn=1005;
char str[2][maxn];
int N[2][maxn];
int main()
{
while(gets(str[0])){
gets(str[1]);
int n=strlen(str[0]);
int m=strlen(str[1]);
memset(N,0,sizeof(N));
int d=0;
for(int i=1;i<=n;i++){
d=d^1;
for(int j=1;j<=m;j++)
if(str[0][i-1]==str[1][j-1])
N[d][j]=N[d^1][j-1]+1;
else N[d][j]=N[d^1][j]>N[d][j-1]?N[d^1][j]:N[d][j-1];
}
printf("%d\n",N[d][m]);
}
return 0;
}

浙公网安备 33010602011771号