Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)
建出广义SAM,通过parent树对每个节点求出其是否仅被一个子串包含及被哪个包含。
写了无数个sam板子题一点意思都没啊
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 200010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,m,son[N][26],fail[N],len[N],f[N],q[N],tmp[N],cnt=1,last;
ll ans[N];
char s[N];
int ins(int c,int p,int i)
{
int u;
if (son[p][c])
{
int q=son[p][c];
if (len[p]+1==len[q]) u=q;
else
{
int y=++cnt;
len[y]=len[p]+1;
memcpy(son[y],son[q],sizeof(son[q]));
fail[y]=fail[q],fail[q]=y;
while (son[p][c]==q) son[p][c]=y,p=fail[p];
u=y;
}
}
else
{
int x=++cnt;len[x]=len[p]+1;
while (!son[p][c]&&p) son[p][c]=x,p=fail[p];
if (!p) fail[x]=1;
else
{
int q=son[p][c];
if (len[p]+1==len[q]) fail[x]=q;
else
{
int y=++cnt;
len[y]=len[p]+1;
memcpy(son[y],son[q],sizeof(son[q]));
fail[y]=fail[q],fail[x]=fail[q]=y;
while (son[p][c]==q) son[p][c]=y,p=fail[p];
}
}
u=x;
}
if (f[u]==-1) f[u]=i;else if (f[u]!=i) f[u]=0;
return u;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();memset(f,255,sizeof(f));
for (int i=1;i<=n;i++)
{
scanf("%s",s+1);
m=strlen(s+1);last=1;
for (int j=1;j<=m;j++) last=ins(s[j]-'a',last,i);
}
for (int i=1;i<=cnt;i++) tmp[len[i]]++;
for (int i=1;i<=cnt;i++) tmp[i]+=tmp[i-1];
for (int i=1;i<=cnt;i++) q[tmp[len[i]]--]=i;
for (int i=cnt;i>=1;i--)
{
int x=q[i];
if (f[x]>0) ans[f[x]]+=len[x]-len[fail[x]];
if (f[fail[x]]==-1) f[fail[x]]=f[x];
else if (f[fail[x]]!=f[x]) f[fail[x]]=0;
}
for (int i=1;i<=n;i++) printf("%lld\n",ans[i]);
return 0;
//NOTICE LONG LONG!!!!!
}
Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)的更多相关文章
- BZOJ.5137.Standing Out from the Herd(广义后缀自动机)
题目链接 \(Description\) 对于每个串,求在\(n\)个串中只在该串中出现过的子串的数量. \(Solution\) 建广义SAM.对每个串插入时新建的np标记其属于哪个串. 然后在pa ...
- BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)
Description Just like humans, cows often appreciate feeling they are unique in some way. Since Farme ...
- 【BZOJ5137】Standing Out from the Herd(后缀自动机)
[BZOJ5137]Standing Out from the Herd(后缀自动机) 题面 BZOJ 洛谷 题解 构建广义后缀自动机 然后对于每个节点处理一下它的集合就好了 不知道为什么,我如果按照 ...
- 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd
here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...
- P4081 [USACO17DEC]Standing Out from the Herd P
知识点: 广义 SAM 原题面 Luogu 「扯」 随便「口胡」一下居然「过」了. 比较考验「代码能力」,第一次感觉「大模拟」没有白写((( 还有这个「符号」实在是太「上头」了. 前置知识 在线构造广 ...
- bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 对[广义后缀自动机]的一些理解
先说一下对后缀自动机的理解,主要是对构造过程的理解. 构造中,我们已经得到了前L个字符的后缀自动机,现在我们要得到L+1个字符的后缀自动机,什么需要改变呢? 首先,子串$[0,L+1)$对应的状态不存 ...
- BZOJ 3926 && ZJOI 2015 诸神眷顾的幻想乡 (广义后缀自动机)
3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MB Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽 ...
- BZOJ 3277 串 (广义后缀自动机)
3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...
- BZOJ 3473: 字符串 [广义后缀自动机]
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 354 Solved: 160[Submit][Status][Discuss] ...
随机推荐
- JS 定时器的4种写法及介绍
JS提供了一些原生方法来实现延时去执行某一段代码,下面来简单介绍一下setTiemout.setInterval.setImmediate.requestAnimationFrame. 一.什么是定时 ...
- phpstorm设置最多标签数
在settings=>Editor=>General=>Editor Tabs的Tab limit下可以设置当前窗口能够打开的最多标签数目,默认是10,超过设定值时,会自动关闭前面的 ...
- SQL-W3School-函数:SQL LAST() 函数
ylbtech-SQL-W3School-函数:SQL LAST() 函数 1.返回顶部 1. LAST() 函数 LAST() 函数返回指定的字段中最后一个记录的值. 提示:可使用 ORDER BY ...
- Jupyter Notebook 远程连接配置(转载)
转载博客的Jupyter Notebook远程连接配置方法. 0 - 参考资料 https://www.jianshu.com/p/08f276d48669?utm_campaign=maleskin ...
- Qt学习过程
1.常用控件的使用[除了常见的还有QTableWidget.QTreeWidget...]2.信号与槽[需要知道connect函数的最后一个参数Qt::ConnectionType取不同枚举时的含义] ...
- spark 入门学习 核心api
spark入门教程(3)--Spark 核心API开发 原创 2016年04月13日 20:52:28 标签: spark / 分布式 / 大数据 / 教程 / 应用 4999 本教程源于2016年3 ...
- Linux下-bash: Permission denied 或者 sudo: command not found 错误
有时候执行一个脚本或者运行一个可执行文件时,如执行脚本./foo.sh,会报错-bash: ./foo.sh: Permission denied,你会再试sudo ./foo.sh,发现继续报错su ...
- spark在windows的配置
在spark-env.cmd添加一行 FOR /F %%i IN ('hadoop classpath') DO @set SPARK_DIST_CLASSPATH=%%i 修改:log4j.prop ...
- spark osx:WARN NativeCodeLoader:62 - Unable to load native-hadoop library for your platform
spark-env.sh文件中增加,确保${HADOOP_HOME}/lib/native目录下有libhadoop.so文件 export JAVA_LIBRARY_PATH=${HADOOP_HO ...
- python:python2与python3共存时,pip冲突,提示Fatal error in launcher: Unable to create process using '"d:\python27\python2.exe" "D:\Python27\Scripts\pip2.exe" '
问题背景: 机器上同时装了python2.和python3后,导致只能用pip3了,使用pip2时提示:Fatal error in launcher: Unable to create proces ...