Advertisement
Korotkodul

НОП_ЗОШ

Jan 6th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15.     for (auto x: v) cout<<x<<' ';
  16.     cout<<"\n";
  17. }
  18. vector <vector<int>> dp;
  19. string s,t;
  20. int n,m;
  21. void cvv(vector <vector <int> > &v){
  22.     cout<<"   ";
  23.     for (int i = 0; i < m;++i)cout<<t[i]<<' ';
  24.     cout<<"\n";
  25.     for (int i=0;i<v.size();++i){
  26.  
  27.         if (i >= 1) cout<<s[i-1]<<" ";
  28.          cv(dp[i]);
  29.     }
  30.     cout<<"\n";
  31. }
  32.  
  33. int main()
  34. {
  35.     /*ios::sync_with_stdio(0);
  36.     cin.tie(0);
  37.     cout.tie(0);*/
  38.      cin>>s>>t;
  39.     n = s.size();
  40.      m = t.size();
  41.     dp.resize(n+1, vector<int>(m+1));
  42.     for (int i=0;i<n+1;++i) dp[i][0] = 0;
  43.     for (int j=0;j<m+1;++j) dp[0][j] = 0;
  44.     vector <vector<int>> pr(n+1, vector<int>(m+1,-1));
  45.     //cout<<"dp\n"; cvv(dp);
  46.     vector <int> ans_s, ans_t;
  47.     for (int i = 1; i < n+1;++i){
  48.         for (int j=1;j<m+1;++j){
  49.             //cout<<"i j = "<<i<<' '<<j<<"\n";
  50.             //cout<<"s[i] = "<<s[i]<<"  t[i] = "<<t[i]<<"\n";
  51.             dp[i][j] = max(max(dp[i-1][j], dp[i][j-1]), (s[i-1] == t[j-1]) * (dp[i-1][j-1] + 1));
  52.         }
  53.     }
  54.     int i = n, j=m;
  55.     //cout<<"dp\n"; cvv(dp);
  56.     while (i>0 && j >0){
  57.             /*cout<<"i j = "<<i<<' '<<j<<"\n";
  58.             cout<<"dp[i][j] = "<<dp[i][j]<<"\n";
  59.             cout<<"dp[i-1][j] = "<<dp[i-1][j]<<"\n";
  60.             cout<<"dp[i][j-1] = "<<dp[i-1][j]<<"\n";
  61.             cout<<"(s[i-1] == t[j-1]) * (dp[i-1][j-1] + 1) = "<<(s[i-1] == t[j-1]) * (dp[i-1][j-1] + 1)<<"\n";*/
  62.         if (dp[i][j] == dp[i-1][j]){
  63.             i--;
  64.         }
  65.         else if (dp[i][j] == dp[i][j-1]){
  66.             j--;
  67.         }else{
  68.             ans_t.push_back(j);
  69.             ans_s.push_back(i);
  70.             i--;
  71.             j--;
  72.         }
  73.     }
  74.  
  75.     //sort()
  76.     cout<<dp[n][m]<<"\n";
  77.     sort(ans_s.begin(), ans_s.end());
  78.     sort(ans_t.begin(), ans_t.end());
  79.     cv(ans_s);
  80.     cv(ans_t);
  81. }
  82. /*
  83. abcd
  84. cxbydz
  85. */
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement