DP,题目很长,题意就是求LCS。
#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 100+1 using namespace std; int a[SIZE],b[SIZE],dp[SIZE][SIZE]; int main() { int la,lb; int ca=1; while(~sf("%d%d",&la,&lb),la||lb) { FOR(i,0,la) sf("%d",&a[i]); FOR(i,0,lb) sf("%d",&b[i]); CLR(dp,0); 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+1][j],dp[i][j+1]); } pf("Twin Towers #%d\n",ca++); pf("Number of Tiles : %d\n\n",dp[la][lb]); } }