看了半天题 不知道怎么用SAM维护 于是借(chao)鉴(xi)的一发神犇的 只要判断这个子串之前被标记的记号(也就是他属于第几个串)和这次转移到的是否相同 如果不同就说明该子串属于多个串 直接标记-1 依次转移就好咧 最后统计就是ans[f[i]]+=sam[i].mx−sam[par[i]].mx;f[i]就是他属于那个串

#include<bits/stdc++.h>
#define bug(x) cout<<(#x)<<" "<<(x)<<endl
#define ll long long
using namespace std;
const int N=2e5+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct Edge{
int v,nxt;
}e[N*2];
struct data{
int a[26],c;
}tr[N];
struct SAM{
int par,mx,a[26];
}sam[N];
int tot,rt,rttot,cnt=1,n,len,r[N],head[N];
ll ans[N];
char s[N];
void ins(int u,int v){
e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
}
void update(int&c,int op){
if(c==0) c=op;
else c=(c==op)?c:-1;
}
int add(int p,int c,int op){
int np=++rttot;
sam[np].mx=sam[p].mx+1;
update(r[np],op);
for(;p&&!sam[p].a[c];p=sam[p].par) sam[p].a[c]=np;
if(!p) sam[np].par=rt;
else{
int q=sam[p].a[c];
if(sam[q].mx==sam[p].mx+1) sam[np].par=q;
else{
int nq=++rttot;
sam[nq]=sam[q];
sam[nq].mx=sam[p].mx+1;
sam[q].par=sam[np].par=nq;
for(;p&&sam[p].a[c]==q;p=sam[p].par) sam[p].a[c]=nq;
}
}
return np;
}
void build(int op){
int len=strlen(s+1),x=1;
for(int i=1;i<=len;i++){
update(tr[x].c,op);
int c=s[i]-'a';
if(!tr[x].a[c]) tr[x].a[c]=++cnt;
x=tr[x].a[c];
}
update(tr[x].c,op);
}
void built(int x,int c,int p){
int np;
if(x==1) np=1;
else np=add(p,c,tr[x].c);
//bug(np);
for(int i=0;i<26;i++) if(tr[x].a[i]) built(tr[x].a[i],i,np);
}
void dfs(int x){
for(int i=head[x];i;i=e[i].nxt){
int j=e[i].v;
dfs(j);
update(r[x],r[j]);
}
if(x!=1&&r[x]!=-1) ans[r[x]]+=sam[x].mx-sam[sam[x].par].mx;
}
int main(){
#ifdef Devil_Gary
freopen("in.txt","r",stdin);
#endif
rttot=rt=1;
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%s",s+1),
build(i);
}
built(1,0,0);
for(int i=2;i<=rttot;i++) ins(sam[i].par,i);
dfs(1);
for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
return 0;
}


BZOJ5137[Usaco2017 Dec]Standing Out from the Herd的更多相关文章

  1. 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 ...

  2. BZOJ 5137: [Usaco2017 Dec]Standing Out from the Herd(后缀自动机)

    传送门 解题思路 这个似乎和以前做过的一道题很像,只不过这个是求本质不同子串个数.肯定是先把广义\(SAM\)造出来,然后\(dfs\)时把子节点的信息合并到父节点上,看哪个只被一个串覆盖,\(ans ...

  3. 【BZOJ5137】Standing Out from the Herd(后缀自动机)

    [BZOJ5137]Standing Out from the Herd(后缀自动机) 题面 BZOJ 洛谷 题解 构建广义后缀自动机 然后对于每个节点处理一下它的集合就好了 不知道为什么,我如果按照 ...

  4. 【BZOJ5138】[Usaco2017 Dec]Push a Box(强连通分量)

    [BZOJ5138][Usaco2017 Dec]Push a Box(强连通分量) 题面 BZOJ 洛谷 题解 这题是今天看到萝卜在做然后他一眼秒了,我太菜了不会做,所以就来做做. 首先看完题目,是 ...

  5. BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)

    5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 182  Solved: 131[ ...

  6. 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd

    here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...

  7. P4081 [USACO17DEC]Standing Out from the Herd

    思路 对所有串建立广义SAM,之后记录SZ,统计本质不同子串时只统计SZ=1的即可 代码 #include <cstdio> #include <algorithm> #inc ...

  8. BZOJ.5137.Standing Out from the Herd(广义后缀自动机)

    题目链接 \(Description\) 对于每个串,求在\(n\)个串中只在该串中出现过的子串的数量. \(Solution\) 建广义SAM.对每个串插入时新建的np标记其属于哪个串. 然后在pa ...

  9. BZOJ5142: [Usaco2017 Dec]Haybale Feast 线段树或二分答案

    Description Farmer John is preparing a delicious meal for his cows! In his barn, he has NN haybales ...

随机推荐

  1. 【杂谈】需要mark的一些东西

    https://riteme.github.io/blog/2017-10-28/oi-coding-guidelines.html https://www.luogu.org/blog/34238/ ...

  2. Linux configure关于交叉编译的参数设置【转】

    转自:http://blog.csdn.net/darennet/article/details/9003005 configure的参数众多,一般包括如下 --srcdir=DIR 这个选项对安装没 ...

  3. Tslib步骤以及出现问题的解决方案【转】

    转自:http://forum.eepw.com.cn/thread/267828/1 嵌入式设备中触摸屏使用非常广泛,但触摸屏的坐标和屏的坐标是不对称的,需要校准.校准广泛使用的是开源的tslib. ...

  4. Percona XtraBackup 实现全备&增量备份与恢复【转】

    percona-xtrabackup主要是有两个工具,其中一个是xtrabackup,一个是innobackupex,后者是前者封装后的一个脚本.在针对MySQL的物理备份工具中,大概是最流行也是最强 ...

  5. Deploy Openstack all-in-one Shell Script

    Deploy Openstack all-in-one Shell Script At present(2015/10), the RDO deploment method can only inst ...

  6. thinkphp5高亮当前页(仅针对个人项目记录,不做通用参考)

    <div class="navbg"> <ul class="menu"> <li> <a href="/& ...

  7. java 学习网站

    http://how2j.cn/  教学网站 慕课视频下载网站 http://www.feemic.cn/mooc //慕课搜索和下载的网站http://www.soshoulu.com/tools/ ...

  8. SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client

    在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...

  9. (三)HttpClient 抓取图片

    第一节: HttpClient 抓取图片 这里pom.xml需要用到io输入输出: <dependency> <groupId>commons-io</groupId&g ...

  10. 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...