看了半天题 不知道怎么用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. C 之回调函数

    软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用.回调和异步调用.同步调用是一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用:回调是一种双向调用模式,也就是 ...

  2. https://www.yunpanjingling.com/

    https://www.yunpanjingling.com/ 账号  kein20 passwd a1234

  3. Python和MySQL数据库交互PyMySQL

    Python数据库操作 对于关系型数据库的访问,Python社区已经指定了一个标准,称为Python Database API SepcificationV2.0.MySQL.Qracle等特定数据库 ...

  4. python网络编程-optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  5. 经典面试题:js继承方式下

    上一篇讲解了构造函数的继承方式,今天来讲非构造函数的继承模式. 一.object()方法 json格式的发明人Douglas Crockford,提出了一个object()函数,可以做到这一点. fu ...

  6. UFLDL 教程学习笔记(二)

    课程链接:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 这一节主要讲的是梯度的概念,在实验部分,比较之前的线性回归 ...

  7. Hive(十)Hive性能调优总结

    一.Fetch抓取 1.理论分析 Fetch抓取是指,Hive中对某些情况的查询可以不必使用MapReduce计算.例如:SELECT * FROM employees;在这种情况下,Hive可以简单 ...

  8. 破损的键盘(UVa 11988)

    s[] 数组用来保存原有的字符序列 nex[] 数组记录打印的下标顺序 C++11 代码如下: #include<iostream> #include<cstring> usi ...

  9. lnmp 一键安装包

    系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要5GB以上硬盘剩余空间 需要128MB以上内存(如果为128MB的小内存VPS,Xe ...

  10. mysql find_in_set函数详解

    Mysql函数FIND_IN_SET()的使用方法 有了FIND_IN_SET这个函数.我们可以设计一个如:一只手机即是智能机,又是Andriod系统的. 比如:有个产品表里有一个type字段,他存储 ...