Advertisement
skb50bd

Longest Common Subsequence

Oct 27th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. // Longest Common Subsequence
  2. /*
  3. Input: 2 Strings: X and Y
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <iomanip>
  9. #define TAB 3
  10. #define SIZE 100
  11. using namespace std;
  12.  
  13. char B[SIZE][SIZE];
  14. int C[SIZE][SIZE];
  15.  
  16. void LCS_length(string X, string Y, int m, int n) {
  17.     for (int i = 1; i <= m; i++)
  18.         C[i][0] = 0;
  19.     for (int i = 0; i <= n; i++)
  20.         C[0][i] = 0;
  21.     for (int i = 1; i <= m; i++)
  22.         for (int j = 1; j <= n; j++) {
  23.             if (X[i - 1] == Y[j - 1]) {
  24.                 C[i][j] = 1 + C[i - 1][j - 1];
  25.                 B[i][j] = '\\';
  26.             }
  27.             else if (C[i - 1][j] >= C[i][j - 1]) {
  28.                 C[i][j] = C[i - 1][j];
  29.                 B[i][j] = '^';
  30.             }
  31.             else {
  32.                 C[i][j] = C[i][j - 1];
  33.                 B[i][j] = '<';
  34.             }
  35.         }
  36. }
  37.  
  38.  
  39. void print_LCS(string X, int i, int j) {
  40.     if (i == 0 || j == 0)
  41.         return;
  42.     if (B[i][j] == '\\') {
  43.         print_LCS(X, i - 1, j - 1);
  44.         cout << X[i - 1];
  45.     }
  46.     else if (B[i][j] == '^')
  47.         print_LCS(X, i - 1, j);
  48.     else
  49.         print_LCS(X, i, j - 1);
  50. }
  51.  
  52.  
  53. void match(string X, string Y, int m, int n) {
  54.     string Xi = X, Yj = Y;
  55.     for (int i = m, j = n; i > 0 && j > 0;) {
  56.  
  57.         if (B[i][j] == '\\') {
  58.             Xi[i - 1] = '_';
  59.             Yj[j - 1] = '_';
  60.             i--;
  61.             j--;
  62.         }
  63.         else if (B[i][j] == '^')
  64.             i--;
  65.         else
  66.             j--;
  67.     }
  68.  
  69.     cout << "   ";
  70.     for (int i = 0; X[i]; i++) {
  71.         if (Xi[i] == '_')
  72.             cout << "_";
  73.         else
  74.             cout << " ";
  75.     }
  76.     cout << endl << "X: " << X << endl;
  77.     cout << "   ";
  78.     for (int i = 0; Y[i]; i++) {
  79.         if (Yj[i] == '_')
  80.             cout << "_";
  81.         else
  82.             cout << " ";
  83.     }
  84.     cout << endl << "Y: " << Y << endl << endl;
  85. }
  86.  
  87.  
  88. void print_Table(string X, string Y, int m, int n) {
  89.     cout << "C Table: " << endl;
  90.     cout << setw(TAB * 2) << "";
  91.     for (int i = 0; i <= n; i++)
  92.         cout << setw(TAB) << i;
  93.     cout << endl;
  94.     cout << setw(TAB * 3) << "";
  95.     for (int i = 0; Y[i]; i++)
  96.         cout << setw(TAB) << Y[i];
  97.     cout << endl;
  98.     for (int i = 0; i <= m; i++) {
  99.         cout << setw(TAB) << i;
  100.         if (i == 0)
  101.             cout << setw(TAB) << "";
  102.         else
  103.             cout << setw(TAB) << X[i - 1];
  104.         for (int j = 0; j <= n; j++)
  105.             cout << setw(TAB) << C[i][j];
  106.         cout << endl;
  107.     }
  108.     cout << endl;
  109.  
  110.     cout << "B Table: " << endl;
  111.     cout << setw(TAB * 2) << "";
  112.     for (int i = 1; i <= n; i++)
  113.         cout << setw(TAB) << i;
  114.     cout << endl;
  115.     cout << setw(TAB * 2) << "";
  116.     for (int i = 0; Y[i]; i++)
  117.         cout << setw(TAB) << Y[i];
  118.     cout << endl;
  119.     for (int i = 1; i <= m; i++) {
  120.         cout << setw(TAB) << i;
  121.         if (i == 0)
  122.             cout << setw(TAB) << "";
  123.         else
  124.             cout << setw(TAB) << X[i - 1];
  125.         for (int j = 1; j <= n; j++)
  126.             cout << setw(TAB) << B[i][j];
  127.         cout << endl;
  128.     }
  129.     cout << endl;
  130. }
  131.  
  132.  
  133.  
  134. int main() {
  135.     string X, Y;
  136.     cout << "String X: ";
  137.     getline(cin, X);
  138.     cout << "String Y: ";
  139.     getline(cin, Y);
  140.     int m, n;
  141.     m = X.length();
  142.     n = Y.length();
  143.  
  144.     LCS_length(X, Y, m, n);
  145.     cout << "Length of Longest Common Subsequence: " << C[m][n] << endl;
  146.     cout << "Longest Common Subsequence: ";
  147.     print_LCS(X, m, n);
  148.     cout << endl;
  149.     match(X, Y, m, n);
  150.     print_Table(X, Y, m, n);
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement