码迷,mamicode.com
首页 > 其他好文 > 详细

LCS & LCIS

时间:2021-06-29 15:57:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:out   操作   tar   end   pid   put   http   href   cpp   

LCS

操作对象:两个长度不一定相等的字符串。

例题

string s, t;
int f[maxn][maxn];

int main()
{
    cin >> s >> t;
    int ls = s.length(), lt = t.length();
    for (int i = 1; i <= ls; i++)
        for (int j = 1; j <= lt; j++)
        {
            f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            if (s[i - 1] == t[j - 1])
                f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
        }
    cout << f[ls][lt] << endl;
    return 0;
}

LCIS

操作对象:两个长度不一定相等的数列。

CF10D

const int maxn = 1005;
ll n, m, a[maxn], b[maxn], ans;
ll f[maxn][maxn], lcis[maxn][maxn];

int main()
{
    n = read();
    for (int i = 1; i <= n; ++i)
        a[i] = read();
    m = read();
    for (int i = 1; i <= m; ++i)
        b[i] = read();

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1, k = 0; j <= m; ++j)
        {
            if (a[i] == b[j])
            {
                f[i][j] = f[i - 1][k] + 1;
                for (int p = 1; p <= f[i - 1][k]; ++p)
                    lcis[j][p] = lcis[k][p];
                lcis[j][f[i][j]] = a[i];
            }
            else
                f[i][j] = f[i - 1][j];
            if (b[j] < a[i] && f[i][j] > f[i][k])
                k = j;
        }
    }

    for (int i = 1; i <= m; ++i)
        if (f[n][i] > f[n][ans])
            ans = i;

    printf("%lld\n", f[n][ans]);
    for (int p = 1; p <= f[n][ans]; ++p)
        printf("%lld ", lcis[ans][p]);
    puts("");
    return 0;
}

LCS & LCIS

标签:out   操作   tar   end   pid   put   http   href   cpp   

原文地址:https://www.cnblogs.com/EdisonBa/p/14948712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!