DP。还是LCS问题。
应该是有空格或者 Tab 。用scanf 就WA了。gets 就AC了。
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<vector> #include<cmath> #define INF 0x7fffffff #define eps 1e-8 #define LL long long #define PI 3.141592654 #define CLR(a,b) memset(a,b,sizeof(a)) #define FOR(i,a,n) for(int i= a;i< n ;i++) #define FOR0(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mp make_pair #define ft first #define sd second #define sf scanf #define pf printf #define acfun std::ios::sync_with_stdio(false) #define SIZE 1000+1 using namespace std; char a[SIZE],b[SIZE]; int dp[SIZE][SIZE]; int main() { while(gets(a)!=NULL) { gets(b); CLR(dp,0); int la=strlen(a); int lb=strlen(b); FOR(i,0,la) FOR(j,0,lb) { if(a[i]==b[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]); } pf("%d\n",dp[la][lb]); } }
UVA 10405 Longest Common Subsequence
原文地址:http://blog.csdn.net/dongshimou/article/details/40782977