看了半天题 不知道怎么用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. 关于Linux内核版本

    Linux内核可分为实验版本和产品化版本.每一个版本号由三位数字“x.y.z”组成,第二位数字说明版本类型:偶数表示产品化版本,奇数表示实验版本.产品化版本只修改错误,而实验版本最初是产品化版本的拷贝 ...

  2. Material Design In Action——重构bilibili客户端

    前言 哔哩哔哩动画是中国大陆的一家弹幕视频网站,在中国二次元用户中颇受欢迎. 哔哩哔哩动画之前推出过采用 Android Design 的 Android 客户端,虽然有使用了部分过时控件(例如 Sc ...

  3. Flip Bits

    Determine the number of bits required to flip if you want to convert integer n to integer m. Notice ...

  4. aarch64_o1

    OCE-devel-0.18.1-1.fc26.aarch64.rpm 2017-05-16 03:37 5.4M fedora Mirroring Project OCE-draw-0.18.1-1 ...

  5. Python开发环境(1):Eclipse+PyDev插件

    电脑:小米笔记本电脑Pro 15.6寸(i5-8250U),操作系统:Windows 10,JDK版本:1.8.0_152(环境变量已配置) Step 1.下载Eclipse 根据我的CPU型号,选择 ...

  6. tf.metrics.accuracy ==>坑货

    tf.metrics.accuracy输出两个值,第一个值为上几步的平均精度,第二值是上几步与该步的精度的平均值. 正常的计算单个batch正确率的代码 self.correct_prediction ...

  7. Codeforces 580D Kefa and Dishes(状态压缩DP)

    题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...

  8. Zookeeper原理架构与搭建

    一.Zookeeper到底是什么!? 学一个东西,不搞明白他是什么东西,哪还有心情学啊!! 首先,Zookeeper是Apache的一个java项目,属于Hadoop系统,扮演管理员的角色. 然后看到 ...

  9. C++有super关键字么?

    很多人在学习Java之后,看到Java里面有super关键字,用来表示父类,那么C++里面有super关键字么? 答案是否定的.这也很容易理解,C++由于支持多继承,所以假设存在super关键字,那么 ...

  10. ASP.NET MVC之验证终结者篇

    有时候我觉得,很多人将一个具体的技术细节写的那么复杂,我觉得没有必要,搞得很多人一头雾水的,你能教会别人用就成了,具体的细节可以去查MSDN什么的,套用爱因斯坦的名言:能在网上查到的就不要去记,用的时 ...