Falak_Ahmed_Shakib

lcs

Nov 11th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<ll,ll> pll;
  5. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. ll const MOD=1000000007;
  10. const int M= 1e5 +10;
  11. #define eb emplace_back
  12. ll ara[100][100];
  13. string a,b;
  14. void lcs(int n, int m )
  15. {
  16.  
  17. for(ll i=1;i<=n;i++)
  18. {
  19. for(ll j=1;j<=m;j++)
  20. {
  21. if(a[i]==b[i])ara[i][j]=1+ara[i-1][j-1];
  22. else ara[i][j]=max(ara[i-1][j],ara[i][j-1]);
  23. }
  24.  
  25. }
  26.  
  27. cout<<ara[n][m]<<endl;
  28.  
  29. int i=n, j=m;
  30.  
  31. while (i > 0 && j > 0)
  32. {
  33. cout<<i<<" "<<j<<endl;
  34.  
  35. cout<<a[i-1]<<" "<<b[j-1]<<endl;
  36.  
  37. if(a[i-1]==b[j-1])
  38. {
  39. cout<<a[i-1]<<endl;;
  40. i--; j--;
  41.  
  42. }
  43.  
  44. else if (ara[i-1][j] > ara[i][j-1]) i--;
  45.  
  46. else
  47. j--;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. int main()
  62. {
  63.  
  64.  
  65.  
  66. cin>>a>>b;
  67.  
  68.  
  69. ll n=a.size();
  70. ll m=b.size();
  71.  
  72. lcs(n,m);
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
Add Comment
Please, Sign In to add comment