poj 2127Greatest Common Increasing Subsequence(LCS)
题目链接:http://poj.org/problem?id=2127
#include<cstdio> #include<cstring> #include<algorithm> #include<utility> using namespace std; const int maxn = 510; int dp[maxn][maxn]; int a[maxn],b[maxn]; pair<int,pair<int,int> > Max[maxn]; pair<int,int> pre[maxn][maxn]; void print(int x,int y){ if(x>0 && y>0 && dp[x][y] > 0){ print(pre[x][y].first,pre[x][y].second); printf("%d ",a[x]); } } int main() { //freopen("E:\\acm\\input.txt","r",stdin); int M,N; scanf("%d",&M); for(int i=1;i<=M;i++) scanf("%d",&a[i]); scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&b[i]); fill(Max,Max+N+1,make_pair(0,make_pair(-1,-1))); int maxx=0 , maxy=0; for(int i=1;i<=M;i++){ pair<int,pair<int,int> > temp = make_pair(0,make_pair(-1,-1)); for(int j=1;j<=N;j++){ if(b[j] < a[i]){ temp = max(temp,Max[j]); } if(b[j] == a[i]){ dp[i][j] = temp.first + 1; pre[i][j] = temp.second; Max[j] = make_pair(dp[i][j],make_pair(i,j)); } else{ dp[i][j] = 0; pre[i][j] = make_pair(-1,-1); } if(dp[i][j] > dp[maxx][maxy]){ maxx = i; maxy = j; } } } printf("%d\n",dp[maxx][maxy]); print(maxx,maxy); } }
自己没写出来,参考别人的。