[ARC081E] Don't Be a Subsequence
考虑建出子序列自动机,然后在上面广搜即可。因为是广搜,所以可以保证找出的是最短的符合条件的字符串。注意广搜时要判 vis。复杂度线性。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 5e5 + 5;
string s;
int n;
int son[N][26], nxt[26];
int pre[N];
bool vis[N];
void bfs()
{
queue<pair<int, string> > q;
q.push(make_pair(0, ""));
while (q.size())
{
if (vis[q.front().first])
{
q.pop();
continue;
}
vis[q.front().first] = 1;
for (int i = 0; i < 26; i++)
{
q.front().second += (i + 'a');
if (!son[q.front().first][i])
{
cout << q.front().second << "\n";
return;
}
q.push(make_pair(son[q.front().first][i], q.front().second));
q.front().second.pop_back();
}
q.pop();
}
//cout << vv.size() << "\n";
}
int main()
{
ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
cin >> s;
n = s.size();
s = (string)" " + s;
for (int i = 0; i < 26; i++) nxt[i] = 1;
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < 26; j++)
{
nxt[j] = max(nxt[j], i + 1);
for (int k = nxt[j]; k <= n; k++)
{
nxt[j] = k;
if (s[k] - 'a' == j)
{
son[i][j] = k;
break;
}
}
}
}
bfs();
return 0;
}

浙公网安备 33010602011771号