题解 CF1482H 【Exam】
Lice
·
·
题解
Description
给定 n 个字符串 s_1, s_2, \cdots, s_n,互不相同。求无序对 (i,j) 的个数,满足:
-
-
- 不存在一个 k(k\ne i 且 k\ne j),使得 s_j 是 s_k 的子串且 s_k 又是 s_i 的子串。
Hint
### Solution
想复杂了,实际上是道并没有多难的题。
先考虑没有第三条限制的情况。这个比较简单:先对所有串建立 AC 自动机并整出 fail 树,然后对于每个串在自动机上跑一遍,树状数组随便维护一下就行。
然后思考第三条限制在何时会起作用。对于当前串 $s_i$,考虑到一个可以作为其子串的 $s_j$ 时,假如存在一个 $s_k$ 使得 $s_k$ 在 $s_i$ 上覆盖到的区间将 $s_j$ 完全包含,那么 $s_j\leftrightarrow s_i$ 之间的贡献就不能被记入答案。并且在所有出现的位置中,有一个存在这样的情况就不行。如果没有出现,并不代表 $s_j$ 就一定不是 $s_k$ 的子串,仅仅是在 $s_i$ 中没有体现而已。但这样在 $s_j\leftrightarrow s_i$ 没有体现,那么就不会像第三条限制那样夹在中间。
首先对于 fail 树中的每个结点,多维护一个 ```trans[x]```,表示 $x$ 最近的可以表示一个整串的对应字符串的编号。由于跳 fail 链就是找最长真后缀,再找下去一定不符合限制 3。
那么枚举 $s_i$ 的前缀,从长到短,然后维护一个字段 ```min_sp``` 表示先前匹配到的字符串中,左端点最靠左的左端点。这时“包含”即为,当前前缀的最长匹配到其他串的后缀,对应的起始点反而在更后侧的前缀匹配到的 ```min_sp``` 的后面。

再维护一个计数器 ```cnt[]```,仅当匹配到的左端点 ```< min_sp``` 时更新。已经很显然了:只有计数器 ```cnt``` 的值恰好等于其在 $s_i$ 中的出现次数时答案 +1。出现次数可以用常规的 dfs 序 + 树状数组维护。
时间复杂度 $O(n\log n)$。
### Code
求赞 /kel
```cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
const int N = 1e6 + 5;
const int S = 26;
int n, ac_loc[N];
std::string s[N];
int ch[N][S], total = 1;
int fail[N], end_of[N];
int insert(const std::string& s) {
int x = 1;
for (auto c : s) {
if (!ch[x][c - 'a']) ch[x][c - 'a'] = ++total;
x = ch[x][c - 'a'];
}
return x;
}
std::vector<int> adj[N];
int idfn[N], odfn[N], timer = 0;
int trans[N];
void dfs(int x, int f) {
trans[x] = end_of[x] ? end_of[x] : trans[f];
idfn[x] = ++timer;
for (auto y : adj[x]) dfs(y, x);
odfn[x] = timer;
}
void build() {
std::queue<int> que;
std::fill(ch[0], ch[0] + S, 1);
que.push(1);
while (!que.empty()) {
int x = que.front(); que.pop();
for (int i = 0; i < S; i++)
if (!ch[x][i]) ch[x][i] = ch[fail[x]][i];
else que.push(ch[x][i]), fail[ch[x][i]] = ch[fail[x]][i];
}
for (int i = 2; i <= total; i++)
adj[fail[i]].push_back(i);
dfs(1, 0);
}
struct {
int tr[N];
void add(int x, int v) {
for (; x <= timer; x += x & -x) tr[x] += v;
}
int get(int x) {
int ret = 0;
for (; x; x -= x & -x) ret += tr[x];
return ret;
}
int calc(int l, int r) {
return get(r) - get(l - 1);
}
} fenwick;
int cnt[N];
bool vis[N];
int ans = 0;
signed main() {
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cin >> s[i];
end_of[ac_loc[i] = insert(s[i])] = i;
}
build();
std::fill(fenwick.tr + 1, fenwick.tr + 1 + timer, 0);
for (int i = 1; i <= n; i++) {
std::vector<int> left;
int x = 1;
for (auto c : s[i]) {
x = ch[x][c - 'a'];
fenwick.add(idfn[x], 1);
left.push_back(trans[x]);
}
left.back() = trans[fail[x]];
int min_sp = (int)s[i].size();
for (int j = (int)s[i].size() - 1; j >= 0; j--) if (left[j]) {
int sp = j - (int)s[left[j]].size() + 1;
if (min_sp <= sp) continue;
min_sp = sp, ++cnt[left[j]];
}
for (auto id : left) if (id && !vis[id]) {
vis[id] = true;
if (cnt[id] == fenwick.calc(idfn[ac_loc[id]], odfn[ac_loc[id]]))
++ans;
}
for (auto id : left) if (id)
cnt[id] = 0, vis[id] = false;
x = 1;
for (auto c : s[i]) {
x = ch[x][c - 'a'];
fenwick.add(idfn[x], -1);
}
}
std::cout << ans << std::endl;
return 0;
}
```