bzoj3926 广义后缀自动机
来源:互联网 发布:淘宝cos道具定制店 编辑:程序博客网 时间:2024/05/15 13:32
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3926
大意:给一棵树,每个节点代表一个字符,任意2个节点之间可以表示为一个字符串,问不同字符串的个数。(数据保证叶子节点<=20)
思路同bzoj2780
先吐槽一下,这3题建边简直要炸了,幸好学到了种新姿势,在结构体里打内置函数(用到外面的变量名 ::变量名),这种题错了完全不想看,幸好没出什么傻逼问题。
回归正题
从叶子节点开始遍历,答案就是遍历出的很多字符串的子串的不同个数(好绕口。。。)。
加到广义后缀自动机中,相同父亲的节点加入时的last也是相同的。(我一开始竟然神经病的将它们加入到trie中,再加到广义后缀自动机中。。。。。。)
统计答案的时候,第一次我是按照以前做的题的思路,先更新len大的,统计它可以走到哪几个不同串。答案就是根节点的值-1;后来%了下神犇的代码。发现只要统计len[x]-len[fa[x]]的值就好了(蒟蒻终究还是太弱了。。。)
%完神犇后的代码
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,Co,Col[100040],Ru[100050];struct SAM { int son[17],len,fa;}C[3000050];int tot,last,V[3000040],q[3000040];long long sum[3000040],ans;inline void Add(int x,int &l){ int p=l; if (C[p].son[x]) { int q=C[p].son[x]; if (C[p].len+1==C[q].len) l=q; else { int nq=++tot; C[nq]=C[q]; C[nq].len=C[p].len+1; C[q].fa=nq; for (;p&&C[p].son[x]==q;p=C[p].fa) C[p].son[x]=nq; l=nq; } } else { int np=++tot; l=tot; C[np].len=C[p].len+1; for (;p&&!C[p].son[x];p=C[p].fa) C[p].son[x]=np; if (!p) C[np].fa=1; else { int q=C[p].son[x]; if (C[p].len+1==C[q].len) C[np].fa=q; else { int nq=++tot; C[nq]=C[q]; C[nq].len=C[p].len+1; C[q].fa=C[np].fa=nq; for (;p&&C[p].son[x]==q;p=C[p].fa) C[p].son[x]=nq; } } }}inline void Pre(){ for (int i=1;i<=tot;++i) ++V[C[i].len]; for (int i=1;i<=tot;++i) V[i]+=V[i-1]; for (int i=tot;i;--i) q[V[C[i].len]--]=i; int x,y; for (int i=tot;i;--i) { x=q[i]; sum[x]=1; for (int j=0;j<Co;++j) if (y=C[x].son[j]) sum[x]+=sum[y]; }// for (int i=2;i<=tot;++i) ans+=C[i].len-C[C[i].fa].len;}struct Trie { int son[14];}T[3000040];int Tsum;void Dfs(int x,int l) { int y,r; for (int i=0;i<Co;++i) if (y=T[x].son[i]) { r=l; Add(i,r); Dfs(y,r); }}struct sef { int ne,en; };struct Graph { sef h[200050]; int tot,first[100050]; inline void setl(int x,int y) { h[++tot].ne=first[x]; h[tot].en=y; first[x]=tot; } void Dfs(int x,int fa,int rt) { int y; if (!T[rt].son[Col[x]]) T[rt].son[Col[x]]=++Tsum; int ret=T[rt].son[Col[x]]; for (int i=first[x];y=h[i].en,i;i=h[i].ne) if (y!=fa) Dfs(y,x,ret); }}G;int main(){ scanf("%d%d",&n,&Co); for (int i=1;i<=n;++i) scanf("%d",Col+i); int x,y; for (int i=1;i<n;++i) { scanf("%d%d",&x,&y); ++Ru[x]; ++Ru[y]; G.setl(x,y); G.setl(y,x); } for (int i=1;i<=n;++i) if (Ru[i]==1) G.Dfs(i,0,0); tot=last=1; Dfs(0,1); Pre(); printf("%lld\n",sum[1]-1);}
加到trie中的傻逼代码
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,Co,Col[100040],Ru[100050];struct SAM { int son[17],len,fa;}C[3000050];int tot,last,V[3000040],q[3000040];long long sum[3000040],ans;inline void Add(int x,int &l){ int p=l; if (C[p].son[x]) { int q=C[p].son[x]; if (C[p].len+1==C[q].len) l=q; else { int nq=++tot; C[nq]=C[q]; C[nq].len=C[p].len+1; C[q].fa=nq; for (;p&&C[p].son[x]==q;p=C[p].fa) C[p].son[x]=nq; l=nq; } } else { int np=++tot; l=tot; C[np].len=C[p].len+1; for (;p&&!C[p].son[x];p=C[p].fa) C[p].son[x]=np; if (!p) C[np].fa=1; else { int q=C[p].son[x]; if (C[p].len+1==C[q].len) C[np].fa=q; else { int nq=++tot; C[nq]=C[q]; C[nq].len=C[p].len+1; C[q].fa=C[np].fa=nq; for (;p&&C[p].son[x]==q;p=C[p].fa) C[p].son[x]=nq; } } }}inline void Pre(){ for (int i=1;i<=tot;++i) ++V[C[i].len]; for (int i=1;i<=tot;++i) V[i]+=V[i-1]; for (int i=tot;i;--i) q[V[C[i].len]--]=i; int x,y; for (int i=tot;i;--i) { x=q[i]; sum[x]=1; for (int j=0;j<Co;++j) if (y=C[x].son[j]) sum[x]+=sum[y]; }// for (int i=2;i<=tot;++i) ans+=C[i].len-C[C[i].fa].len;}struct Trie { int son[14];}T[3000040];int Tsum;void Dfs(int x,int l) { int y,r; for (int i=0;i<Co;++i) if (y=T[x].son[i]) { r=l; Add(i,r); Dfs(y,r); }}struct sef { int ne,en; };struct Graph { sef h[200050]; int tot,first[100050]; inline void setl(int x,int y) { h[++tot].ne=first[x]; h[tot].en=y; first[x]=tot; } void Dfs(int x,int fa,int rt) { int y; if (!T[rt].son[Col[x]]) T[rt].son[Col[x]]=++Tsum; int ret=T[rt].son[Col[x]]; for (int i=first[x];y=h[i].en,i;i=h[i].ne) if (y!=fa) Dfs(y,x,ret); }}G;int main(){ scanf("%d%d",&n,&Co); for (int i=1;i<=n;++i) scanf("%d",Col+i); int x,y; for (int i=1;i<n;++i) { scanf("%d%d",&x,&y); ++Ru[x]; ++Ru[y]; G.setl(x,y); G.setl(y,x); } for (int i=1;i<=n;++i) if (Ru[i]==1) G.Dfs(i,0,0); tot=last=1; Dfs(0,1); Pre(); printf("%lld\n",sum[1]-1);}
0 0
- bzoj3926 广义后缀自动机
- bzoj3926(广义后缀自动机)
- bzoj3926 [广义后缀自动机]
- 【BZOJ3926】【Zjoi2015】诸神眷顾的幻想乡 广义后缀自动机
- BZOJ3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- BZOJ3926 [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- bzoj3926[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- bzoj3926 [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)
- 广义后缀自动机
- 广义后缀自动机hiho1457
- Bzoj3473:字符串:广义后缀自动机
- smoj2010(广义后缀自动机)
- 广义后缀自动机与后缀树
- [BZOJ3926]ZJOI2015诸神眷顾的幻想乡|后缀自动机
- bzoj3926 诸神眷顾的幻想乡 后缀自动机
- 【BZOJ3926】【Zjoi2015】诸神眷顾的幻想乡 后缀自动机
- 【bzoj3926】[Zjoi2015]诸神眷顾的幻想乡 后缀自动机
- 后缀自动机 【Zjoi2015】诸神眷顾的幻想乡 bzoj3926
- 【STL】next_permutation的实现
- caffe源码阅读——Blob类
- tomcat启动一闪而过
- 记一次花费一天时间debug的过程
- java广度优先遍历
- bzoj3926 广义后缀自动机
- Collection Data Structures In Swift
- Valid Palindrome
- 多线程 工厂 > 商店(仓库) > 客户模式 消费者模式 实现多仓库 多产品 多工厂 多客户
- 产品需求管理
- BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇
- 在CentOS 6.6 64bit上升级GCC到4.8.2版本
- POJ 3046 Ant Counting(dp—多重集组合数问题)
- C语言概述【重学C之一】