openjudge-NOI 2.6-2000 最长公共子上升序列
题目链接:http://noi.openjudge.cn/ch0206/2000/
题解:
裸题,不解释(题目有毒)
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define MAXN 10010 5 int lena,lenb,a[MAXN],b[MAXN],ans1,ans2[MAXN]; 6 struct node 7 { 8 int x,nextx,nexty; 9 }f[MAXN][MAXN]; 10 int main() 11 { 12 scanf("%d",&lena);for(int i=1;i<=lena;++i)scanf("%d",&a[i]); 13 scanf("%d",&lenb);for(int i=1;i<=lenb;++i)scanf("%d",&b[i]); 14 swap(a,b); 15 int t=lena;lena=lenb;lenb=t; 16 for(int i=1;i<=lena;++i) 17 { 18 node max=(node){0,0,0}; 19 for(int j=1;j<=lenb;++j) 20 { 21 f[i][j]=(node){f[i-1][j].x,i-1,j}; 22 if(b[j]<a[i]&&f[i-1][j].x>max.x)max=(node){f[i-1][j].x,i-1,j}; 23 else if(a[i]==b[j])f[i][j]=(node){max.x+1,max.nextx,max.nexty}; 24 } 25 } 26 int k1=0,k2; 27 for(int i=1;i<=lenb;++i) 28 { 29 if(f[lena][i].x>k1) 30 { 31 k1=f[lena][i].x; 32 k2=i; 33 } 34 } 35 printf("%d\n",k1); 36 int x=lena,y=k2; 37 while(x&&y) 38 { 39 node temp=f[x][y]; 40 if(f[temp.nextx][temp.nexty].x==temp.x-1)ans2[++ans1]=a[x]; 41 x=temp.nextx;y=temp.nexty; 42 } 43 for(int i=ans1;i>=1;--i)printf("%d ",ans2[i]); 44 }