Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string s1,s2,sLCS;
  9. int ** L,i,j,m,n;
  10.  
  11. cin >>s1;
  12. cin >> s2;
  13.  
  14. m = s1.length();
  15. n = s2.length();
  16.  
  17.  
  18. L = new int * [m + 1];
  19. for(i = 0; i <= m; i++) L[i] = new int[n + 1];
  20.  
  21.  
  22. for(i = 0; i <= m; i++) L[i][0] = 0;
  23. for(j = 0; j <= n; j++) L[0][j] = 0;
  24. for(i = 0; i < m; i++)
  25. for(j = 0; j < n; j++)
  26. if(s1[i] == s2[j])
  27. L[i + 1][j + 1] = 1 + L[i][j];
  28. else
  29. L[i + 1][j + 1] = max(L[i + 1][j],L[i][j + 1]);
  30.  
  31. sLCS = ""; i = m - 1; j = n - 1;
  32. while((i >= 0) && (j >= 0))
  33. if(s1[i] == s2[j])
  34. {
  35. sLCS = s1[i] + sLCS;
  36. i--; j--;
  37. }
  38. else if(L[i + 1][j] > L[i][j + 1]) j--;
  39. else i--;
  40. cout << L[m][n] << endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement