[洛谷P4081][USACO17DEC]Standing Out from the Herd
题目大意:给你$n$个字符串,对每个字符串求出只在这个字符串中出现的字串的个数
题解:先建广义$SAM$,然后对每个点统计一下它的子树中是不是都是在同一个字符串中的,是的话,就把这个点标成这一个字符串,计算贡献
卡点:无
C++ Code:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 100010 long long ans[maxn];
namespace SAM {
#define N (maxn << 1)
int R[N], fail[N], nxt[N][26];
int lst = 1, idx = 1, bel[N]; void append(int ch, int tg) {
int p = lst, np = lst = ++idx; R[np] = R[p] + 1, bel[np] = tg;
for (; p && !nxt[p][ch]; p = fail[p]) nxt[p][ch] = np;
if (!p) fail[np] = 1;
else {
int q = nxt[p][ch];
if (R[p] + 1 == R[q]) fail[np] = q;
else {
int nq = ++idx;
fail[nq] = fail[q], R[nq] = R[p] + 1, fail[q] = fail[np] = nq;
std::copy(nxt[q], nxt[q] + 26, nxt[nq]);
for (; p && nxt[p][ch] == q; p = fail[p]) nxt[p][ch] = nq;
}
}
} int head[N], cnt;
struct Edge {
int to, nxt;
} e[N];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
}
void dfs(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
dfs(v);
if (~bel[v]) {
if (!bel[u]) bel[u] = bel[v];
else if (bel[u] != bel[v]) bel[u] = -1;
} else bel[u] = -1;
}
}
void work() {
for (int i = 2; i <= idx; i++) addedge(fail[i], i);
dfs(1);
for (int i = 2; i <= idx; i++) if (bel[i] != -1) ans[bel[i]] += R[i] - R[fail[i]];
}
#undef N
} int n;
char s[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%s", s);
SAM::lst = 1;
for (register char *ch = s; *ch; ++ch) SAM::append(*ch - 'a', i);
}
SAM::work();
for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
return 0;
}
[洛谷P4081][USACO17DEC]Standing Out from the Herd的更多相关文章
- P4081 [USACO17DEC]Standing Out from the Herd
思路 对所有串建立广义SAM,之后记录SZ,统计本质不同子串时只统计SZ=1的即可 代码 #include <cstdio> #include <algorithm> #inc ...
- P4081 [USACO17DEC]Standing Out from the Herd P
知识点: 广义 SAM 原题面 Luogu 「扯」 随便「口胡」一下居然「过」了. 比较考验「代码能力」,第一次感觉「大模拟」没有白写((( 还有这个「符号」实在是太「上头」了. 前置知识 在线构造广 ...
- 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd
here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...
- [USACO17DEC]Standing Out from the Herd(广义后缀自动机)
题意 定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数.如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 ...
- 【[USACO17DEC]Standing Out from the Herd】
题目 不会广义\(SAM\)啊 但信仰插入特殊字符就可以搞定一切了 我们先把所有的串搞在一起建出一个\(SAM\),记得在中间插入特殊字符 对于\(parent\)树上的一个节点,只有当其\(endp ...
- Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)
建出广义SAM,通过parent树对每个节点求出其是否仅被一个子串包含及被哪个包含. 写了无数个sam板子题一点意思都没啊 #include<bits/stdc++.h> using na ...
- 【LuoguP4081】[USACO17DEC]Standing Out from the Herd
题目链接 题意 给定多个字符串,每个串中仅在该串中出现的本质不同的子串个数. Sol 多串匹配想到用广义SAM. 之后从串的匹配角度不是很好做.发现一个本质不同的串最多只会贡献到一个串的答案里. 那么 ...
- 洛谷P2879 [USACO07JAN]区间统计Tallest Cow
To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. ...
- 洛谷P2947 [USACO09MAR]仰望Look Up
P2947 [USACO09MAR]仰望Look Up 74通过 122提交 题目提供者洛谷OnlineJudge 标签USACO2009云端 难度普及/提高- 时空限制1s / 128MB 提交 ...
随机推荐
- Android stado 运行项目,apk does not exist on disk.
报错如下: 03/12 21:38:56: Launching iReader The APK file F:\git\iReader_nubia\iReader\build\outputs\apk\ ...
- 蓝牙入门知识-CC2541知识
蓝牙是为了能够通信,想要通信就必须遵守一定的规则, Profile 就可以理解为相互约定的规则,因为每个协议栈demo 都会有一个Profile 与之对应, 我们这里的SimpleBLExxx 对应的 ...
- Unity AssetBundle工作流
一.创建AssetBundle 1.在资源的Inspector视图下有一个AssetBundle的UI,第一个选项表示AssetBundle名称,第二个用于设置AssetBundle Variant, ...
- http tcp udp
HTTP连接 1.HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...
- Jmeter登录接口返回 status415
1.现象:在查看结果树中看到: Request Headers:Connection: keep-aliveContent-Type: application/x-www-form-urlencode ...
- JavaScript实现无刷新评论及在IE下的剪切板访问(学习)
1.无刷新评论 tips: appendChild:将新元素作为父元素的最后一个子元素进行添加. insertBefore:在一个指定的子节点之前插入一个节点 实现: <!DOCTYPE htm ...
- 【WXS全局对象】consloe
consloe对象 方法: 原型:console.log( [String] ) 说明:用于在 console 窗口输出信息,一般用于程序调试使用示例: console.log支持arguments类 ...
- 三个线程ABC,交替打印ABC
转载与:https://www.cnblogs.com/x_wukong/p/4009709.html 创建3个线程,让其交替打印ABC . 输出如下: ABCABCABCABC. 方法:使用syn ...
- [leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- POJ 3415 Common Substrings(后缀数组)
Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. Given t ...