Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- using namespace std;
- using ll = long long;
- using ld = long double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- vector <vector<int>> dp;
- string s,t;
- int n,m;
- void cvv(vector <vector <int> > &v){
- cout<<" ";
- for (int i = 0; i < m;++i)cout<<t[i]<<' ';
- cout<<"\n";
- for (int i=0;i<v.size();++i){
- if (i >= 1) cout<<s[i-1]<<" ";
- cv(dp[i]);
- }
- cout<<"\n";
- }
- int main()
- {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- cin>>s>>t;
- n = s.size();
- m = t.size();
- dp.resize(n+1, vector<int>(m+1));
- for (int i=0;i<n+1;++i) dp[i][0] = 0;
- for (int j=0;j<m+1;++j) dp[0][j] = 0;
- vector <vector<int>> pr(n+1, vector<int>(m+1,-1));
- //cout<<"dp\n"; cvv(dp);
- vector <int> ans_s, ans_t;
- for (int i = 1; i < n+1;++i){
- for (int j=1;j<m+1;++j){
- //cout<<"i j = "<<i<<' '<<j<<"\n";
- //cout<<"s[i] = "<<s[i]<<" t[i] = "<<t[i]<<"\n";
- 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));
- }
- }
- int i = n, j=m;
- //cout<<"dp\n"; cvv(dp);
- while (i>0 && j >0){
- /*cout<<"i j = "<<i<<' '<<j<<"\n";
- cout<<"dp[i][j] = "<<dp[i][j]<<"\n";
- cout<<"dp[i-1][j] = "<<dp[i-1][j]<<"\n";
- cout<<"dp[i][j-1] = "<<dp[i-1][j]<<"\n";
- 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";*/
- if (dp[i][j] == dp[i-1][j]){
- i--;
- }
- else if (dp[i][j] == dp[i][j-1]){
- j--;
- }else{
- ans_t.push_back(j);
- ans_s.push_back(i);
- i--;
- j--;
- }
- }
- //sort()
- cout<<dp[n][m]<<"\n";
- sort(ans_s.begin(), ans_s.end());
- sort(ans_t.begin(), ans_t.end());
- cv(ans_s);
- cv(ans_t);
- }
- /*
- abcd
- cxbydz
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement