Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for (auto&i:v) cin >> i;
- #define cout(v) for (auto&i:v) cout << i << " ";
- #define clr(memo, x) memset(memo, x, sizeof memo)
- #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- void Zainab(){
- // #ifndef ONLINE_JUDGE
- freopen("longpath.in", "r", stdin);
- freopen("longpath.out", "w", stdout);
- // #endif
- }
- int n , m;
- string s,t;
- vector < vector < int > > dp;
- int rec(int i , int j ){
- if(i >=n or j >= m) return 0;
- int & ret = dp[i][j];
- if(~ret)
- return ret ;
- ret =0;
- if(s[i] == t[j])
- ret = max(ret , 1 + rec(i +1 , j+1));
- ret = max(ret , rec(i+1 , j));
- ret = max(ret , rec(i , j +1));
- return ret;
- }
- void build(int i , int j ){
- if(i >=n or j >= m) return ;
- int ret = dp[i][j];
- if(s[i] == t[j] and ret == dp[i+1][j+1] +1 ) {
- cout << s[i] ;
- build(i+1 , j+1);
- return ;
- }
- if(ret == dp[i][j+1]){
- build(i , j+1);
- }
- else if(ret == dp[i+1][j]){
- build(i+1 , j);
- }
- }
- void myCode(){
- cin >> s >> t;
- n=sz(s) , m = sz(t);
- dp.assign(n+1 , vector < int > (m+1 , -1));
- int len= rec(0,0);
- cout << len << nl;
- build(0,0);
- }
- int main(){
- FastCode ;
- // Zainab() ;
- int testCase=1;
- // cin >> testCase ;
- for(int i=1 ; i<= testCase ; i++)
- myCode();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement