Sanath12345

Untitled

May 13th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. //Problem link :
  2. //done by sanath kumar
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define ld long double
  7. #define mod 1000000007
  8. #define inf 1e9
  9. #define endl "\n"
  10. #define pb push_back
  11. #define vi vector<ll>
  12. #define vs vector<string>
  13. #define pii pair<ll,ll>
  14. #define ump unordered_map
  15. #define mp make_pair
  16. #define pq_max priority_queue<ll>
  17. #define pq_min priority_queue<ll,vector<ll>,greater<ll>>
  18. #define all(v) v.begin(),v.end()
  19. #define ff first
  20. #define ss second
  21. #define mid(l,r) (l+(r-l))/2
  22. #define bitc(x) __builtin_popcount(x)
  23. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  24. #define looprev(i,a,b) for(int i=(a);i>=(b);i--)
  25. #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
  26. #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
  27. #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  28. template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
  29. template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
  30.  
  31. void err(istream_iterator<string> it){}
  32. template<typename T,typename... Args>
  33. void err(istream_iterator<string> it,T a,Args... args){
  34. cout<<*it<<" = "<<a<<endl;
  35. err(++it,args...);
  36. }
  37.  
  38. void file_i_o(){
  39. ios_base::sync_with_stdio(false);
  40.  
  41. cin.tie(0);
  42. cout.tie(0);
  43. #ifndef ONLINE_JUDGE
  44. freopen("input.txt","r",stdin);
  45. freopen("output.txt","w",stdout);
  46. #endif
  47. }
  48. int main(){
  49. file_i_o();
  50. clock_t begin=clock();
  51. string s,t;
  52. cin>>s;
  53. cin.ignore();
  54. cin>>t;
  55. int m=s.length(),n=t.length();
  56. vector<vector<pair<int,string>>> dp(2,vector<pair<int,string>>(n+2));
  57. loop(i,0,1){
  58. dp[i][0].ff=0;
  59. dp[i][0].ss="";
  60. }
  61. loop(j,0,n){
  62. dp[0][j].ff=0;
  63. dp[0][j].ss="";
  64. }
  65. // log(m*n);
  66. int r=1;
  67. loop(i,1,m){
  68. loop(j,1,n){
  69. if(s[i-1]==t[j-1]){
  70. dp[r][j].ff=1+dp[r^1][j-1].ff;
  71. dp[r][j].ss=dp[r^1][j-1].ss+s[i-1];
  72. }
  73. else{
  74. if(dp[r^1][j].ff>dp[r][j-1].ff){
  75. dp[r][j].ff=dp[r^1][j].ff;
  76. dp[r][j].ss=dp[r^1][j].ss;
  77. }
  78. else{
  79. dp[r][j].ff=dp[r][j-1].ff;
  80. dp[r][j].ss=dp[r][j-1].ss;
  81. }
  82. }
  83. }
  84. r=r^1;
  85. loop(j,1,n){
  86. dp[r][j].ff=0;
  87. dp[r][j].ss="";
  88. }
  89. }
  90. r=r^1;
  91. cout<<dp[r][n].ss<<endl;
  92.  
  93. #ifndef ONLINE_JUDGE
  94. clock_t end=clock();
  95. cout<<"\n\n\nExecuted in :"<<double(end-begin)/CLOCKS_PER_SEC*1000<<" ms"<<endl;
  96. #endif
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment