题目链接:https://nanti.jisuanke.com/t/41395
题意: 给出两个串s 和 t 在求出s中求出一个长度最大的序列其字典序严格大于t。
思路: 枚举s串能够和t串匹配的最长公共前缀序列长度i,再求出s串比t的第i+1个字符大的最近 的位置pos, 其长度为(i - 1)+ n - pos + 1. 最后特殊注意一下s的最长公共前缀不能和t完全相同。
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
const int mx = 26;
char s[maxn], t[maxn];
int nxt[maxn][mx];
int main(){
int n, m;
scanf("%d%d", &n, &m);
scanf("%s%s", s + 1, t + 1);
for(int i = 0; i < mx; ++i) nxt[n + 1][i] = -1;
for(int i = n + 1; i >= 1; --i){
for(int j = 0; j < mx; ++j) nxt[i - 1][j] = nxt[i][j];
if(i != n + 1) nxt[i - 1][s[i] - 'a'] = i;
}
int ans = -1, cur = 0, pre = 0;
for(int i = 1; i <= m; ++i){
for(int j = t[i] - 'a' + 1; j < mx; ++j){
if(nxt[cur][j] == -1) continue ;
ans = max(ans, i + n - nxt[cur][j]);
}
if(nxt[cur][t[i] - 'a'] == -1) break ;
cur = nxt[cur][t[i] - 'a'];
if(i == m && n > m) ans = max(ans, i + n - cur); //note!!!
}
printf("%d
", ans);
return 0;
}