Advertisement
shamiul93

LightOJ - 1110 - An Easy LCS

May 19th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. /**
  2. @author - Rumman BUET CSE'15
  3.  
  4. problem - 1110 - An Easy LCS
  5.  
  6. Concept - DP , LCS
  7.  
  8. Always remember -
  9.  
  10.             1. For LCS always make the dp array on paper and pen. Because , it will help you to solve it.
  11.             2. For printing LCS or others , you should use iterative dp. Otherwise it
  12.             will be a problem.
  13.  
  14. */
  15.  
  16.  
  17. // LightOJ always needs this format for sure..so I made a copy of it...
  18. #include <bits/stdc++.h>
  19. #include<vector>
  20. #define ll                                      long long int
  21. #define fi                                      freopen("in.txt", "r", stdin)
  22. #define fo                                      freopen("out.txt", "w", stdout)
  23. #define m0(a) memset(a , 0 , sizeof(a))
  24. #define m1(a) memset(a , -1 , sizeof(a))
  25.  
  26. #define mx 150000
  27. using namespace std;
  28.  
  29. string s1, s2 ;
  30. int l1, l2 ;
  31.  
  32. int dp[110][110] = {} ;
  33. string str[110][110] = {} ;
  34.  
  35. void lcs()
  36. {
  37.     for(int i = 0 ; i < 110 ; i++)
  38.     {
  39.         dp[0][i] = 0 ;
  40.         dp[i][0] = 0 ;
  41.         str[i][0] = "" ;
  42.         str[0][i] = "" ;
  43.     }
  44.  
  45.     for(int i = 1 ; i <= l1 ; i++)
  46.     {
  47.         for(int j = 1 ; j <= l2 ; j++)
  48.         {
  49.             if(s1[i-1] == s2[j-1])
  50.             {
  51.                 dp[i][j] = dp[i-1][j-1] + 1 ;
  52.                 str[i][j] = str[i-1][j-1] + s1[i-1] ;
  53.             }
  54.             else if(dp[i-1][j] > dp[i][j-1])
  55.             {
  56.                 dp[i][j] = dp[i-1][j] ;
  57.                 str[i][j] = str[i-1][j] ;
  58.             }
  59.             else if(dp[i-1][j] < dp[i][j-1])
  60.             {
  61.                 dp[i][j] = dp[i][j-1] ;
  62.                 str[i][j] = str[i][j-1] ;
  63.             }
  64.             else if(dp[i-1][j] == dp[i][j-1])
  65.             {
  66.                 dp[i][j] = dp[i-1][j] ;
  67.                 str[i][j] = min(str[i-1][j], str[i][j-1]) ;
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76. //    fi ;
  77. //    fo ;
  78.     ll T, t = 0 ;
  79. //    scanf("%lld",&T) ;
  80.     cin >> T ;
  81.  
  82.  
  83.  
  84.     while(T--)
  85.     {
  86.         t++ ;
  87.  
  88.         cin >> s1 >> s2 ;
  89.         l1 = s1.length() ;
  90.         l2 = s2.length() ;
  91.         printf("Case %lld: ",t) ;
  92.  
  93.         lcs() ;
  94.         if(str[l1][l2] == "")
  95.         {
  96.             cout << ":(" << endl ;
  97.         }
  98.         else
  99.             cout << str[l1][l2] << endl ;
  100.     }
  101.     return 0 ;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement