[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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现