vakho

LCS_LENGTH

Dec 16th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<vector<char>> B(8, 7);
  8.  
  9. void LCS_LENGTH(const vector<char> &X, const vector<char> &Y);
  10. void PRINT_LCS(const vector<vector<char>> &b, const vector<char> &X, int i, int j);
  11.  
  12. int main(string args[])
  13. {
  14.     vector<char> X(7);
  15.     X[0] = 'A';
  16.     X[1] = 'B';
  17.     X[2] = 'C';
  18.     X[3] = 'B';
  19.     X[4] = 'D';
  20.     X[5] = 'A';
  21.     X[6] = 'B';
  22.  
  23.     vector<char> Y(6);
  24.     Y[0] = 'B';
  25.     Y[1] = 'D';
  26.     Y[2] = 'C';
  27.     Y[3] = 'A';
  28.     Y[4] = 'B';
  29.     Y[5] = 'A';
  30.  
  31.     LCS_LENGTH(X, Y);
  32.  
  33.     PRINT_LCS(B, X, 6, 6);
  34.     cout << endl;
  35.  
  36.     system("PAUSE");
  37.     return (0);
  38. }
  39.  
  40. void LCS_LENGTH(const vector<char> &X, const vector<char> &Y)
  41. {
  42.     int m = X.size(); cout << "X.size() = " << X.size() << endl;
  43.     int n = Y.size(); cout << "Y.size() = " << Y.size() << endl << endl;
  44.  
  45.     vector<vector<int>> c(m+1, n+1); cout << "c(" << c.size() << ", " << c[0].size() << ")" << endl;
  46.     vector<vector<char>> b(m+1, n+1); cout << "b(" << b.size() << ", " << b[0].size() << ")" << endl << endl;
  47.  
  48.     for (int i = 0; i <= m; i++) c[i][0] = 0;
  49.     for (int j = 1; j <= n; j++) c[0][j] = 0;
  50.  
  51.     for (int i = 1; i <= m; i++)
  52.     {
  53.         for (int j = 1; j <= n; j++)
  54.         {
  55.             if (X[i-1] == Y[j-1])
  56.             {
  57.                 c[i][j] = c[i-1][j-1] + 1;
  58.                 b[i][j] = '\\';
  59.             }
  60.             else
  61.             {
  62.                 if (c[i-1][j] >= c[i][j-1])
  63.                 {
  64.                     c[i][j] = c[i-1][j];
  65.                     b[i][j] = '^';
  66.                 }
  67.                 else
  68.                 {
  69.                     c[i][j] = c[i][j-1];
  70.                     b[i][j] = '<';
  71.                 }
  72.             }
  73.             cout << " " << c[i][j] << " ";
  74.             cout << b[i][j] << "\t";
  75.         }
  76.         cout << endl;
  77.     }
  78.     cout << endl;
  79.  
  80.     B = b;
  81. }
  82.  
  83. void PRINT_LCS(const vector<vector<char>> &b, const vector<char> &X, int i, int j)
  84. {
  85.     if (i == 0 || j == 0) return;
  86.     if (b[i][j] == '\\')
  87.     {
  88.         PRINT_LCS(b, X, i-1, j-1);
  89.         //print X[i-1];
  90.         cout << X[i-1] << " ";
  91.     }
  92.     else
  93.     {
  94.         if (b[i][j] == '^') PRINT_LCS(b, X, i-1, j);
  95.         else PRINT_LCS(b, X, i, j-1);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment