fork download
  1. #include<bits/stdc++.h>
  2. //#include <boost/multiprecision/cpp_int.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7. #define fast std::ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  8. #define clr0(a) memset((a), 0, sizeof(a))
  9. #define clr1(a) memset((a), -1, sizeof(a))
  10. #define srtin1 cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  11. #define strin2 getline(cin, text);
  12. #define ll long long
  13. #define test cout<<"archit\n"
  14. #define debug(x) cout<<x<<" "
  15. #define debug1(x) cout<<x<<"\n"
  16. #define debug2(x,y) cout<<x<<" "<<y<<"\n"
  17. #define debug3(x, y, z) cout<<x<<" "<<y<<" "<<z<<"\n"
  18. #define pb push_back
  19. #define pi pair<int,int>
  20. #define fi first
  21. #define si second
  22. #define mod (ll)1000000007
  23. #define mxn 1000005
  24. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  25. using namespace std;
  26. //using namespace boost::multiprecision;
  27. using namespace __gnu_pbds;
  28. int dp[1005][1005];
  29. int solve(string a, string b, int n, int m, int i, int j)
  30. {
  31. if(i==n || j==m){
  32. return 0;
  33. }
  34. if(dp[i][j] > -1){
  35. return dp[i][j];
  36. }
  37. int ans=0;
  38. if(a[i] == b[j]){
  39. ans = 1+solve(a, b, n, m, i+1, j+1);
  40. }
  41. else{
  42. ans = solve(a, b, n, m, i+1, j);
  43. ans = max(ans, solve(a, b, n, m, i, j+1));
  44. }
  45. return dp[i][j] = ans;
  46. }
  47. int main()
  48. {
  49. string a,b;
  50. cin>>a>>b;
  51. int n=a.length();
  52. int m=b.length();
  53. memset(dp, 0, sizeof(dp));
  54. for(int i=1;i<=n;i++){
  55. for(int j=1;j<=m;j++){
  56. if(a[i-1] == b[j-1]){
  57. dp[i][j] = 1+dp[i-1][j-1];
  58. }
  59. else{
  60. dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
  61. }
  62. }
  63. }
  64. string ans="";
  65. int i=n,j=m;
  66. while(i>=0 && j>=0){
  67. if(dp[i][j] == 1+dp[i-1][j-1]){
  68. ans+=a[i-1];
  69. i-=1;
  70. j-=1;
  71. }
  72. else if(dp[i][j] == dp[i-1][j]){
  73. --i;
  74. }
  75. else{
  76. --j;
  77. }
  78. }
  79. std::reverse(ans.begin(), ans.end());
  80. cout<<ans;
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0s 19184KB
stdin
Standard input is empty
stdout
Standard output is empty