velimir

LongestCommonSubsequence

Mar 1st, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int i, j, dolz, dist;
  8.     string zbor, word;
  9.     cin >> zbor >> word;
  10.     dolz = zbor.length();
  11.     dist = word.length();
  12.     int mat[dolz+1][dist+1];
  13.  
  14.     for(i=0; i<=dolz; i++)
  15.         for(j=0; j<=dist; j++)
  16.             mat[i][j] = 0;
  17.  
  18.     for(i=0; i<dolz; i++)
  19.         for(j=0; j<dist; j++)
  20.         {
  21.             if(zbor[i]==word[j])
  22.                 mat[i+1][j+1] = mat[i][j]+1;
  23.             else
  24.                 mat[i+1][j+1] = max(mat[i+1][j], mat[i][j+1]);
  25.         }
  26.     /*for(i=0; i<=dolz; i++){
  27.         for(j=0; j<=dist; j++)
  28.         cout << mat[i][j] << "   ";
  29.         if(j==dist) cout << endl;
  30.     }*/
  31.     cout << mat[dolz][dist];
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment