Advertisement
albnayem

LCS

Apr 2nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. /*
  2.    __Allahu Akbar__
  3.    __All_praises_to_Allah__
  4.    __Bismillahir_Rahmanir_Rahim__
  5.  
  6.    Author: Abdullah Al Nayem
  7.    Studying B.Sc in CSE at Leading University.
  8.  
  9.    Practice, like you've never won. Perform, like you've never lost.
  10.    Practice hard, play hard. Be hard to beat.
  11.  
  12. */
  13.  
  14. #include <bits/stdc++.h>
  15. #include <iostream>
  16. #include <cstdio>
  17. #include <algorithm>
  18. #include <math.h>
  19. #include <bitset>
  20. using namespace std;
  21.  
  22. #define MAX               32000
  23. #define ll                long long
  24. #define pb                push_back
  25. #define mkp               make_pair
  26. #define F                 first
  27. #define S                 second
  28. #define Sort_arr(arr,x)   sort(arr,arr+x)
  29. #define Sortr_arr(arr,x)  sort(arr, arr+n,greater<int>())
  30. #define SortS(str)        sort(str.begin(), str.end())
  31. #define SortRS(str)       sort(str.rbegin(), str.rend())
  32. #define arr_rev(n)        sort(n.begin(), n.end(), greater<int>())
  33. #define gin(a)            getline(cin,a)
  34. #define Sz(a)             a.size()
  35. #define Ignore            cin.ignore()
  36. #define sq(n)             (n*n)
  37. #define cube(n)           (n*n*n)
  38. #define min3(a, b, c)     min(a, min(b, c))
  39. #define max3(a, b, c)     max(a, max(b, c))
  40. #define YES               cout << "YES" << endl
  41. #define NO                cout << "NO" << endl
  42. #define pYES              printf("YES\n")
  43. #define pNO               printf("NO\n")
  44. #define bs                binary_search
  45. #define lb                lower_bound
  46. #define ub                upper_bound
  47. #define all(a)            a.begin(),a.end()
  48. #define Fast_read         ios_base::sync_with_stdio(false);
  49.  
  50. //#define SIZE_N 32000
  51. //bitset <SIZE_N> bs;
  52. //ll int primes[SIZE_N+5];
  53. //vector<int> primes;
  54.  
  55. /*-----------------------Useful functions-----------------------*/
  56.  
  57. //ll int seive(){ll int i,j,total=0,val;for(i=2;i<=SIZE_N;i++) bs[i]=1;val=sqrt(SIZE_N)+1;for(i=2;i<val;i++)if(bs[i])for(j=i;j*i<=SIZE_N;j++)bs[i*j]=0;for(i=2;i<=SIZE_N;i++)if(bs[i])primes[total++]=i;return total;}
  58. //void Sseive() {bool isPrime[MAX];for (int i = 0; i < MAX; ++i) isPrime[i] = true;for (int i = 3; i * i <= MAX; i += 2) {if (isPrime[i]) {for (int j = i * i; j <= MAX; j += i) {isPrime[j] = false;}}}primes.push_back(2);for (int i = 3; i < MAX; i += 2) {if (isPrime[i]) primes.push_back(i);}}void segSieve (ll l, ll r) {bool isPrime[r-l+1];for (int i = 0; i < r - l + 1; ++i) isPrime[i] = true;if (l == 1) isPrime[0] = false;for (int i = 0; primes[i]*primes[i] <= r; ++i) {int currentPrime = primes[i];ll base = (l/currentPrime)*currentPrime;if (base < l) base += currentPrime;for (ll j = base; j <= r; j += currentPrime) {isPrime[j-l] = false;}if (base == currentPrime) isPrime[base-l] = true;}for (int i = 0; i < r - l + 1; ++i) {if (isPrime[i]) cout << (i+l) << endl;}puts("");}
  59. //ll int divisor_mcs(ll int N){ll int count=0,i;for (i=1;i<=sqrt(N);i++){if (N%i==0){count++;if (N/i!=i) count++;}}return count;}
  60. //int divisor(int N){int i,val,count,sum;val=sqrt(N)+1;sum=1;for(i=0;primes[i]<val;i++){if(N%primes[i]==0){count=0;while(N%primes[i]==0){N/=primes[i];count++;}sum*=(count+1);}}if(N>1)sum=sum*2;return sum;}
  61. //vector <int> primefactor(int n){int i;vector<int> primefact;for (i=2;i<=sqrt(n);i++){if (n%i==0){int count=0;while(n%i==0){n=n/i;count++;}while(count>0){primefact.pb(i);count--;}}}if (n!=1) primefact.pb(n);return primefact;}
  62. //vector <int> all_divisor(int n){int i;vector<int> all_div;for (i=2;i<=sqrt(n);i++){if (n%i==0) all_div.pb(i);if (n/i!=i && n%i==0) all_div.pb(n/i);}sort(all_div.begin(),all_div.end());return all_div;}
  63. //ll int GCD (ll int x, ll int y){if (x%y==0) return y; else return (GCD(y,x%y));}
  64. //ll int LCM (ll int a,ll int b) {return (a/GCD(a,b))*b;}
  65. //bool is_prime(ll int n){for (ll int i=2;i<=sqrt(n);i++){if (n%i==0) return false;}return true;}
  66.  
  67. /*------------------------------------------------------------------*/
  68.  
  69.  
  70.  
  71. string LCS( string str1, string str2)
  72. {
  73.     int m=str1.size();
  74.     int n=str2.size();
  75.     int L[m+1][n+1];
  76.  
  77.     for (int i=0; i<=m; i++)
  78.     {
  79.         for (int j=0; j<=n; j++)
  80.         {
  81.         if (i == 0 || j == 0)
  82.             L[i][j] = 0;
  83.         else if (str1[i-1] == str2[j-1])
  84.             L[i][j] = L[i-1][j-1] + 1;
  85.         else
  86.             L[i][j] = max(L[i-1][j], L[i][j-1]);
  87.         }
  88.     }
  89.     int index = L[m][n];
  90.  
  91.     string res="";
  92.     int i = m, j = n;
  93.     while (i > 0 && j > 0)
  94.     {
  95.         if (str1[i-1] == str2[j-1])
  96.         {
  97.             res+=str1[i-1];
  98.             i--; j--; index--;
  99.         }
  100.  
  101.         else if (L[i-1][j] > L[i][j-1])
  102.             i--;
  103.         else
  104.             j--;
  105.     }
  106.     reverse(res.begin(),res.end());
  107.     return res;
  108. }
  109.  
  110. int main()
  111. {
  112.     string str1,str2;
  113.     cin>>str1>>str2;
  114.     cout<<LCS(str1,str2)<<endl;
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement