日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

發布時間:2023/12/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Greatest Common Increasing Subsequence

題目鏈接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1432

題目大意:給出兩串數字,求他們的最長公共上升子序列(LCIS),并且打印出來。

Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
1 4

分析:神奇就神奇在是LIS與LCS的組合

令dp[i][j]表示A串的前i個,與B串的前j個,并以B[j]為結尾的LCIS 的長度.

狀態轉移方程:

  f(A[i]==B[j])   dp[i][j]=max(dp[i-1][k])+1;? ( 1 <= k < j )

  else   dp[i][j]=dp[i-1][j];

然后選擇循環順序,就可以將算法的復雜度降為n*n.

代碼如下:

?

1 /*這個代碼結果雖然對,跟樣例的輸出都不一樣,而且兩個輸出數據之間有空行都沒有實現,卻能AC,有點匪夷所思*/ 2 # include<stdio.h> 3 # include<string.h> 4 #define MAX 550 5 6 struct node{ 7 int x,y; 8 }path[MAX][MAX]; 9 10 int dp[MAX][MAX]; 11 int s[MAX],t[MAX]; 12 13 int main(){ 14 int T,i,j; 15 scanf("%d",&T); 16 while(T--) 17 { 18 memset(path,0,sizeof(path)); 19 int n,m; 20 scanf("%d",&n); 21 for(i=1; i<=n; i++) 22 scanf("%d",&s[i]); 23 scanf("%d",&m); 24 for(i=1; i<=m; i++) 25 scanf("%d",&t[i]); 26 memset(dp,0,sizeof(dp)); 27 int max = 0; 28 for(i=1; i<=n; i++) 29 { 30 max = 0; 31 int tx = 0,ty = 0; 32 for(j=1; j<=m; j++) 33 { 34 dp[i][j] = dp[i-1][j]; 35 path[i][j].x = i-1; 36 path[i][j].y = j; 37 if( s[i] > t[j] && max < dp[i-1][j]) 38 { 39 max = dp[i-1][j]; 40 tx = i-1; 41 ty = j; 42 } 43 if( s[i] == t[j] ) 44 { 45 dp[i][j] = max+1; 46 path[i][j].x = tx; 47 path[i][j].y = ty; 48 } 49 } 50 } 51 max = -1; 52 int id; 53 for(i=1; i<=m; i++) 54 if(dp[n][i]>max) 55 { 56 max = dp[n][i]; 57 id = i; 58 } 59 int save[MAX]; 60 int cnt=0; 61 int tx,ty; 62 tx=n; ty=id; 63 while(dp[tx][ty] != 0 ) 64 { 65 int tmpx,tmpy; 66 tmpx = path[tx][ty].x; 67 tmpy = path[tx][ty].y; 68 if(dp[tx][ty] != dp[tmpx][tmpy]) 69 { 70 save[cnt++]=t[ty]; 71 } 72 tx = tmpx; ty = tmpy; 73 } 74 printf("%d\n",max); 75 for(i=cnt-1; i>=0; i--) 76 printf("%d ",save[i]); 77 printf("\n"); 78 } 79 return 0; 80 }

?

?

?

?

轉載于:https://www.cnblogs.com/acm-bingzi/p/3263820.html

總結

以上是生活随笔為你收集整理的ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。