题解:Luogu P14775 [ICPC 2024 Seoul R] String Rank

题意

\(Q_k(s)\)\(s\) 的所有长度 \(\leq k\) 的本质不同子序列构成的集合。给定小写字母串 \(s\),求最小的正整数 \(x\) 使得 \(\forall 1\leq i<j\leq n,\ Q_x(s_{i\sim n})\neq Q_x(s_{j\sim n})\)\(1\leq |s|\leq 3\times 10^6\)

题解

被爆了,那咋办?

容易发现 \(Q_x(s_{i\sim n})\supseteq Q_x(s_{i+1\sim n})\),条件等价于 \(\forall 1\leq i<n,Q_x(s_{i\sim n})\neq Q_x(s_{i+1\sim n})\),这相当于存在 \(s_{i\sim n}\) 的子序列 \(t\) 使得其不是 \(s_{i+1\sim n}\) 的子序列,可以发现 \(t\) 必然以 \(s_i\) 开头。

比较难注意到,这等价于 \(t\)\(s\)最靠后的出现位置的起始下标就是 \(i\)。因此考虑反向建出子序列自动机,即对于每个 \(i\in [1,n+1]\) 和字符 \(ch\),从节点 \(i\)\(pre_{i,ch}\) 连一条有向边,其中 \(pre_{i,ch}\) 表示 \(s_{1\sim i-1}\)\(ch\) 最靠后的出现位置,若不存在则记为 \(0\)。根据子序列自动机的贪心性质,对于任意一条从 \(n+1\) 走到 \(i\) 的路径,其对应的子序列 \(t\) 一定满足要求。

考虑求出 \(dis_u\) 表示 \(n+1\)\(u\) 的最短路,那么对于 \(i\) 来说,\(t\) 的最短长度就是 \(dis_i\)。因此答案即为 \(\max\limits_{i=1}^{n-1}dis_i\)。BFS 求最短路即可,时间复杂度 \(\mathcal{O}(n|\Sigma|)\)

代码
#include <bits/stdc++.h>

using namespace std;

#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int N = 3e6 + 5, C = 26;

template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }

int n, pre[N][C], pos[C], dis[N];
string s;
queue<int> q;

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> s, n = s.size();
	s = '#' + s;
	fill(pos, pos + C, 0);
	for (int i = 1; i <= n; ++i)
		copy(pos, pos + C, pre[i]), pos[s[i] - 'a'] = i;
	copy(pos, pos + C, pre[n + 1]);
	fill(dis + 1, dis + n + 1, -1);
	q.push(n + 1), dis[n + 1] = 0;
	while (!q.empty()) {
		int x = q.front(); q.pop();
		for (int i = 0; i < C; ++i) {
			int y = pre[x][i];
			if (dis[y] == -1) dis[y] = dis[x] + 1, q.push(y);
		}
	}
	cout << *max_element(dis + 1, dis + n) << '\n';
	return 0;
}
posted @ 2025-12-25 11:13  P2441M  阅读(0)  评论(0)    收藏  举报