LCIS POJ 2172 Greatest Common Increasing Subsequence

 

题目传送门

 1 /*  2  LCIS裸题:if(a[i]==b[j]) dp[j]=max(dp[j],dp[k]); (1<=k<j&&b[k]<b[j])  3  打印路径时按照b[i]来输出  4  5  详细解释:http://www.tuicool.com/articles/UBNvU3  6 */  7 #include <cstdio>  8 #include <iostream>  9 #include <algorithm> 10 #include <cstring> 11 #include <map> 12 using namespace std; 13 14 const int MAXN = 5e2 + 10; 15 const int INF = 0x3f3f3f3f; 16 int a[MAXN], b[MAXN], dp[MAXN][MAXN]; 17 int rx[MAXN][MAXN], ry[MAXN][MAXN]; 18 int cnt = 0; 19 20 void print(int x, int y, int last) 21 { 22 if (x == 0 || y == 0) return ; 23 24  print (rx[x][y], ry[x][y], y); 25 if (y != last) 26  { 27 ++cnt; 28 if (cnt == 1) printf ("%d", b[y]); 29 else printf (" %d", b[y]); 30  } 31 } 32 33 void LCIS(int n, int m) 34 { 35 memset (dp, 0, sizeof (dp)); 36 memset (rx, 0, sizeof (rx)); 37 memset (ry, 0, sizeof (ry)); 38 39 int ans = 0; int k = 0; 40 int sx = 0, sy = 0; 41 for (int i=1; i<=n; ++i) 42  { 43 k = 0; 44 for (int j=1; j<=m; ++j) 45  { 46 dp[i][j] = dp[i-1][j]; 47 rx[i][j] = i - 1; ry[i][j] = j; 48 if (a[i] == b[j]) 49  { 50 if (dp[i][j] < dp[i][k] + 1) 51  { 52 dp[i][j] = dp[i][k] + 1; 53 rx[i][j] = i; ry[i][j] = k; 54  } 55  } 56 else if (a[i] > b[j]) 57  { 58 if (dp[i][j] > dp[i][k]) k = j; 59  } 60 if (ans < dp[i][j]) 61  { 62 ans = dp[i][j]; 63 sx = i; sy = j; 64  } 65  } 66  } 67 68 cnt = 0; 69 printf ("%d\n", ans); 70 print (sx, sy, -1); puts (""); 71 } 72 73 int main(void) //POJ 2172 Greatest Common Increasing Subsequence 74 { 75 //freopen ("POJ_2172.in", "r", stdin); 76 77 int n, m; 78 79 while (scanf ("%d", &n) == 1) 80  { 81 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 82 83 scanf ("%d", &m); 84 for (int i=1; i<=m; ++i) scanf ("%d", &b[i]); 85 86  LCIS (n, m); 87  } 88 89 return 0; 90 }
 1 /*

 2  LCIS(无打印路径)  3  详细解释:http://www.cnblogs.com/ka200812/archive/2012/10/15/2723870.html

 4 */

 5 #include <cstdio>

 6 #include <iostream>

 7 #include <algorithm>

 8 #include <cstring>

 9 #include <map>

10 using namespace std; 11 

12 const int MAXN = 5e2 + 10; 13 const int INF = 0x3f3f3f3f; 14 int a[MAXN], b[MAXN], dp[MAXN][MAXN]; 15 

16 void LCIS(int n, int m) 17 { 18     memset (dp, 0, sizeof (dp)); 19 

20     int ans = 0;    int k = 0; 21     for (int i=1; i<=n; ++i) 22  { 23         k = 0; 24         for (int j=1; j<=m; ++j) 25  { 26             dp[i][j] = dp[i-1][j];        //以a[]为主循环,每个a[i],去找每个b[j]

27             if (a[i] == b[j] && dp[i][j] < dp[i][k] + 1)        //满足LCS

28  { 29                 dp[i][j] = dp[i][k] + 1;        //在0 ~ j-1找到b[k] < a[i],满足LIS,在b[k]基础上更新dp

30  } 31             else if (a[i] > b[j] && dp[i][j] > dp[i][k])    k = j;        //找到最优的k

32 

33             if (ans < dp[i][j])    ans = dp[i][j];        //更新所有dp中的最大值

34  } 35  } 36 

37     printf ("%d\n", ans); 38 } 39 

40 int main(void)        //HDOJ 1423 Greatest Common Increasing Subsequence

41 { 42     //freopen ("HDOJ_1423.in", "r", stdin);

43 

44     int n, m; 45     int t; 46     scanf ("%d", &t); 47     while (t--) 48  { 49         scanf ("%d", &n); 50         for (int i=1; i<=n; ++i)    scanf ("%d", &a[i]); 51 

52         scanf ("%d", &m); 53         for (int i=1; i<=m; ++i)    scanf ("%d", &b[i]); 54 

55  LCIS (n, m); 56         if (t)    puts (""); 57  } 58 

59     return 0; 60 }
LICS(无打印版)

 

你可能感兴趣的:(sequence)