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

猜你喜欢

转载自www.cnblogs.com/philo-zhou/p/13379046.html