Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. char s[3123];
  6. char t[3123];
  7. int dp[3123][3123];
  8. string resp;
  9.  
  10. int main() {
  11. scanf(" %s", s+1);
  12. scanf(" %s", t+1);
  13. //printf("%s\n", s+1);
  14. //printf("%s\n", t+1);
  15. dp[0][0] = 0;
  16. int max1 = strlen(s+1);
  17. int max2 = strlen(t+1);
  18.  
  19. for(int i = 1; i<= max1; i++){
  20.  
  21. for(int j = 1; j<=max2; j++){
  22. if(s[i] == t[j]){
  23. dp[i][j] = dp[i-1][j-1] +1;
  24. //printf("i = %d j = %d\n", i, j);
  25.  
  26. }
  27. else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
  28.  
  29. }
  30.  
  31.  
  32. }
  33. int aux = max1;
  34. int aux2 = max2;
  35.  
  36. while(aux > 0 && aux2 > 0){
  37.  
  38. if(s[aux] == t[aux2]){
  39. resp.push_back(s[aux]);
  40. aux--;
  41. aux2--;
  42. }
  43. else{
  44. if(dp[aux][aux2-1]>dp[aux-1][aux2] ) aux2--;
  45. else aux--;
  46.  
  47.  
  48. }
  49. }
  50.  
  51. for(int i = resp.size()-1; i >= 0 ; i-- ) printf("%c", resp[i]);
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement