LIS POJ 2250 Compromise

 

题目传送门

 1 /*  2  LIS模板题:题目看错了,是求单词的最长上升子序列!  3  编程好累:)  4 */  5 #include <cstdio>  6 #include <iostream>  7 #include <cstring>  8 #include <algorithm>  9 #include <string> 10 using namespace std; 11 12 const int MAXN = 1e2 + 10; 13 const int MAXM = 50; 14 const int INF = 0x3f3f3f3f; 15 char s1[MAXM][MAXN], s2[MAXM][MAXN]; 16 int dp[MAXN][MAXN]; 17 int rt[MAXN][MAXN]; 18 int flag; 19 20 void print(int x, int y) 21 { 22 if (!x && !y) return ; 23 24 if (rt[x][y] == 0) 25  { 26 print (x-1, y-1); 27 if (!flag) {printf ("%s", s1[x-1]); flag = 1;} 28 else printf (" %s", s1[x-1]); 29  } 30 else if (rt[x][y] == 1) print (x-1, y); 31 else print (x, y-1); 32 } 33 34 void LIS(int len1, int len2) 35 { 36 memset (dp, 0, sizeof (dp)); 37 memset (rt, 0, sizeof (rt)); 38 for (int i=0; i<=len1; ++i) rt[i][0] = 1; 39 for (int i=0; i<=len2; ++i) rt[0][i] = -1; 40 41 for (int i=1; i<=len1; ++i) 42  { 43 for (int j=1; j<=len2; ++j) 44  { 45 if (!strcmp (s1[i-1], s2[j-1])) 46  { 47 dp[i][j] = dp[i-1][j-1] + 1; 48 rt[i][j] = 0; 49  } 50 else if (dp[i-1][j] >= dp[i][j-1]) 51  { 52 dp[i][j] = dp[i-1][j]; 53 rt[i][j] = 1; 54  } 55 else 56  { 57 dp[i][j] = dp[i][j-1]; 58 rt[i][j] = -1; 59  } 60  } 61  } 62 63 flag = 0; 64 print (len1, len2); puts (""); 65 } 66 67 int main(void) //POJ 2250 Compromise 68 { 69 //freopen ("POJ_2250.in", "r", stdin); 70 71 while (scanf ("%s", s1[0]) == 1) 72  { 73 int len1 = 1, len2 = 1; 74 while (strcmp (s1[len1-1], "#")) scanf ("%s", &s1[len1++]); 75 len1 -= 1; scanf ("%s", s2[0]); 76 while (strcmp (s2[len2-1], "#")) scanf ("%s", &s2[len2++]); 77 78  LIS (len1, len2); 79  } 80 81 return 0; 82 } 83 84 /* 85 die einkommen der abgeordneten muessen dringend verbessert werden 86 */

 

你可能感兴趣的:(Promise)