LCS(인쇄 경로) POJ 2264 Advanced Fruits

10815 단어 Advanced

제목 전송문
 1 /*  2  LCS( ) : , ;  3   :)  4 */  5 #include <cstdio>  6 #include <algorithm>  7 #include <cmath>  8 #include <cstring>  9 #include <iostream> 10 using namespace std; 11 12 const int MAXN = 1e2 + 10; 13 const int INF = 0x3f3f3f3f; 14 char s[MAXN], t[MAXN]; 15 int dp[MAXN][MAXN]; 16 int rt[MAXN][MAXN]; 17 int len_s, len_t; 18 19 void LCS(void) 20 { 21 for (int i=1; i<=len_s; ++i) 22  { 23 for (int j=1; j<=len_t; ++j) 24  { 25 if (s[i-1] == t[j-1]) 26  { 27 dp[i][j] = dp[i-1][j-1] + 1; 28 rt[i][j] = 0; 29  } 30 else if (dp[i-1][j] >= dp[i][j-1]) 31  { 32 dp[i][j] = dp[i-1][j]; 33 rt[i][j] = 1; 34  } 35 else 36  { 37 dp[i][j] = dp[i][j-1]; 38 rt[i][j] = -1; 39  } 40  } 41  } 42 } 43 44 void print(int x, int y) 45 { 46 if (!x && !y) return ; 47 48 if (rt[x][y] == 0) 49  { 50 print (x-1, y-1); printf ("%c", s[x-1]); 51  } 52 else if (rt[x][y] == 1) 53  { 54 print (x-1, y); printf ("%c", s[x-1]); 55  } 56 else 57  { 58 print (x, y-1); printf ("%c", t[y-1]); 59  } 60 } 61 62 int main(void) //POJ 2264 Advanced Fruits 63 { 64 //freopen ("POJ_2264.in", "r", stdin); 65 66 while (scanf ("%s %s", &s, &t) == 2) 67  { 68 len_s = strlen (s); len_t = strlen (t); 69 memset (dp, 0, sizeof (dp)); 70 memset (rt, 0, sizeof (rt)); 71 for (int i=0; i<=len_s; ++i) rt[i][0] = 1; 72 for (int i=0; i<=len_t; ++i) rt[0][i] = -1; 73 74  LCS (); 75  print (len_s, len_t); 76 puts (""); 77  } 78 79 return 0; 80 } 81 82 /* 83 appleach 84 bananas 85 pearch 86 */

좋은 웹페이지 즐겨찾기