zoukankan      html  css  js  c++  java
  • F

    Problem Statement

    You are given strings ss and tt. Find one longest string that is a subsequence of both ss and tt.

    Notes

    subsequence of a string xx is the string obtained by removing zero or more characters from xx and concatenating the remaining characters without changing the order.

    Constraints

    • ss and tt are strings consisting of lowercase English letters.
    • 1|s|,|t|30001≤|s|,|t|≤3000

    Input

    Input is given from Standard Input in the following format:

    ss
    tt
    

    Output

    Print one longest string that is a subsequence of both ss and tt. If there are multiple such strings, any of them will be accepted.


    Sample Input 1 Copy

    Copy
    axyb
    abyxb
    

    Sample Output 1 Copy

    Copy
    axb
    

    The answer is axb or ayb; either will be accepted.


    Sample Input 2 Copy

    Copy
    aa
    xayaz
    

    Sample Output 2 Copy

    Copy
    aa
    

    Sample Input 3 Copy

    Copy
    a
    z
    

    Sample Output 3 Copy

    Copy
    
    

    The answer is  (an empty string).


    Sample Input 4 Copy

    Copy
    abracadabra
    avadakedavra
    

    Sample Output 4 Copy

    Copy
    aaadara
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod =1e9+7;
    const int N = 1e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }      
    
    int main()
    {
        string s, t;
        cin >> s >> t;
        vector<vector<int>> a(s.size() + 1, vector<int>(t.size() + 1));
        rep(i, 1, s.size())
        {
            rep(j, 1, t.size())
            {
                if (s[i - 1] == t[j - 1])
                {
                    a[i][j] = a[i - 1][j - 1] + 1;
                }
                else
                {
                    a[i][j] = max(a[i - 1][j], a[i][j - 1]);
                }
            }
        }
        int j = t.size(),i=s.size();
        vector<char> res;
        while (j > 0 && i > 0)
        {
            while (j>0 && a[i][j] == a[i][j - 1])
                j--;
            while (i>0 && a[i][j] == a[i - 1][j])
                i--;
            if(i>0)
                res.push_back(s[i-1]);
            i--, j--;
        }
        for (int i = res.size() - 1; i >= 0; i--)
            cout << res[i];
        return 0;
    }
  • 相关阅读:
    洛谷 P1260 工程规划(差分约束)
    洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
    [模板]单调队列
    [模板]LIS(最长上升子序列)
    洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
    如何求数字n的因数个数及因数和
    [模板]tarjan缩点+拓扑排序
    itext生成pdf(附带页眉,页脚,页码)
    工作总结03
    工作总结02(海报上传模块)
  • 原文地址:https://www.cnblogs.com/dealer/p/13140292.html
Copyright © 2011-2022 走看看