看了半天题 不知道怎么用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. ntpdate[35450]: the NTP socket is in use, exiting

    当前主机已是NTP服务器,需关闭当前NTP服务,再同步其他NTP服务器的时间 service ntpd stop 然后ps -ef | grep ntp看进程是否已杀掉 然后再次ntpdate Ser ...

  2. nand flash 的oob 及坏块管理

    0.NAND的操作管理方式      NAND FLASH的管理方式:以三星FLASH为例,一片Nand flash为一个设备(device),1 (Device) = xxxx (Blocks),1 ...

  3. sqlite3 的insert记录项思路

    sqlite3 的insert记录项思路 1.组合一个insert的sql语句 2.判断是否需要立即执行,若不是立刻执行的语句,则插入到待处理的链表中,供后续事务处理时提交.必须有一个专门线程来对事务 ...

  4. MVC 视图页对数字,金额 用逗号 隔开(数字格式化)

    cshtml页面代码: <tr> <th>@Model.BankName</th> <th>@Model.Month</th> <th ...

  5. Django-模板继承、包含和静态文件配置

    一.模板继承 模板继承可以减少页面内容的重复定义,实现页面内容的重用 典型应用:网站的头部.尾部是一样的,这些内容可以定义在父模板中,子模板不需要重复定义 block标签:在父模板中预留区域,在子模板 ...

  6. 数据库——mysql如何获取当前时间

    1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp() curren ...

  7. Python_oldboy_自动化运维之路(四)

    本节内容 集合 字符编码与转码 函数语法及基本特性 函数参数与局部变量 返回值和嵌套函数 递归 匿名函数 高阶函数 1.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变 ...

  8. python面向对象(三)之继承

    继承 介绍 继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力.继承即常说的is-a关系.子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承 ...

  9. iframe框架加载完成后执行函数

    var iframe = document.createElement("iframe"); iframe.src = "http://www.baidu.com/&qu ...

  10. WinScp几个极大提高开发效率的小功能

    WinSCP 是一个 Windows 环境下使用 SSH 的开源图形化 SFTP 客户端.同时支持 SCP 协议.它的主要功能就是在本地与远程计算机间安全的复制文件. 最近研究了一下winscp的一些 ...