zoukankan      html  css  js  c++  java
  • 2019徐州网络赛 M Longest subsequence 序列自动机

    题目链接https://nanti.jisuanke.com/t/41395

    题意:给两个字符串,s和t,在s中求字典序严格大于t的最长子序列。

    思路:分类讨论即可。先建个s的序列自动机。

    1 如果有字符大于t当前位置,那么后边的全都行。

    2 如果有字符等于t当前位置,继续往后边匹配。

    遍历过程更新答案最大值即可。注意还需要判断一下s的子序列和t串匹配相等的情况下是否有剩余。

    #include <bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    #define met(a, b) memset(a, b, sizeof(a))
    #define rep(i, a, b) for(int i = a; i <= b; i++)
    #define bep(i, a, b) for(int i = a; i >= b; i--)
    #define pb push_back
    #define mp make_pair
    #define debug cout << "KKK" << endl
    #define ls num*2
    #define rs num*2+1
    #define re return
    using namespace std;
    const ll mod = 1e9 + 7;
    const double PI = acos(-1);
    const ll INF = 2e18+1;
    const int inf = 1e9+5;
    const double eps = 1e-7;
    const int maxn = 1e6 + 5;
    int ne[maxn][27];
    char s[maxn], t[maxn];
    int main(){
        // ios::sync_with_stdio(false);
        // cin.tie(0); cout.tie(0);
        int n, m; scanf("%d%d",&n,&m);
        scanf("%s %s", s+1, t+1);
        rep(i, 0, 25) ne[n][i] = -1;
        bep(i, n, 1){
            rep(j, 0, 25) ne[i-1][j] = ne[i][j];
            ne[i-1][s[i] - 'a'] = i;
        }
        int ans = -1, pos = 0;
        rep(i, 1, m){
            int x = t[i] - 'a';
            rep(j, x+1, 25) if(~ne[pos][j]) ans = max(ans, i+n-ne[pos][j]);
            pos = ne[pos][x];
            if(pos == -1) break;
            if(i == m && pos != n) ans = max(ans, m+n-pos);
        }
        cout << ans << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    Linux sed命令实例详解
    hadoop2.0 和1.0的区别
    linux如何修改主机名
    hadoop主节点(NameNode)备份策略以及恢复方法
    Hadoop 添加删除数据节点(datanode)
    Hadoop常见错误及处理方法
    【转】ImageView.ScaleType属性
    MonoBehaviour.print和Debug.Log是同样的作用
    unity自带寻路Navmesh入门教程
    前向渲染路径细节 Forward Rendering Path Details
  • 原文地址:https://www.cnblogs.com/philo-zhou/p/13379046.html
Copyright © 2011-2022 走看看