UVA 10405 Longest Common Subsequence

来源:互联网 发布:clip studio for mac 编辑:程序博客网 时间:2024/06/09 17:18

经典的DP,

本来可以开一个二维大数组的。

但是由于以前做过,现在再来做。。。

直接滚动数组水之(原谅我的无耻、、、)

还要注意,用scanf会Wa

#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;#define M 1003char s[M],t[M];int main(){while(gets(s+1)){gets(t+1);int dp[2][M]={0},len1=strlen(s+1),len2=strlen(t+1);int p,i,j;for(i=1;i<=len1;i++){p=i&1;for(j=1;j<=len2;j++){if(s[i]==t[j])dp[p][j]=dp[!p][j-1]+1;elsedp[p][j]=dp[p][j-1]>dp[!p][j]?dp[p][j-1]:dp[!p][j];}}cout<<dp[len1&1][len2]<<endl;}return 0;}