[Codeforces][后缀自动机]Helvetic Coding Contest 2017 . I Fake News (hard)
来源:互联网 发布:广告联盟js代码 编辑:程序博客网 时间:2024/05/19 07:08
一看是字符串题,就想一想怎么在后缀自动机上搞
有一个跟BZOJ3238 差异差不多的思路,建出原串反串的后缀自动机,那么每个子串的lcp就是对应节点的lca,那么lcp出现次数就是这个节点的right集合,拷了拷之前的代码,改一改就过了……
#include <cstdio>#include <iostream>#include <cstring>#include <string>#define N 500010<<1using namespace std;typedef long long ll;struct SAM_{ int next[N][26],fail[N],stp[N],p,cnt,t[N],r[N]; ll f[N],w[N]; SAM_(){p=cnt=1;} void clear(){ p=cnt=1; memset(next,0,sizeof(next)); memset(fail,0,sizeof(fail)); memset(stp,0,sizeof(stp)); memset(t,0,sizeof(t)); memset(r,0,sizeof(r)); memset(f,0,sizeof(f)); memset(w,0,sizeof(w)); } void Extend(int x){ x-='a'; int np=++cnt;stp[np]=stp[p]+1;w[np]=f[np]=1; while(p&&!next[p][x]) next[p][x]=np,p=fail[p]; if(!p) fail[np]=1; else{ int q=next[p][x]; if(stp[q]==stp[p]+1) fail[np]=q; else{ int nq=++cnt;stp[nq]=stp[p]+1; memcpy(next[nq],next[q],sizeof(next[q])); fail[nq]=fail[q]; fail[q]=fail[np]=nq; while(p&&next[p][x]==q) next[p][x]=nq,p=fail[p]; } } p=np; } void Samort(){ int i; for(i=0;i<=cnt;i++) t[i]=0; for(i=1;i<=cnt;i++) t[stp[i]]++; for(i=1;i<=cnt;i++) t[i]+=t[i-1]; for(i=1;i<=cnt;i++) r[t[stp[i]]--]=i; } void Solve(int n){ for(int i=cnt;i;i--)f[fail[r[i]]]+=f[r[i]]; ll Ans=0; for(int i=2;i<=cnt;i++) Ans+=1LL*(stp[i]-stp[fail[i]])*f[i]*f[i]; printf("%lld\n",Ans); }}SAM;char A[N];int n;int main(){ int t; scanf("%d",&t); while(t--){ SAM.clear(); scanf("%s",A+1);n=strlen(A+1); for(int i=n;i;i--) SAM.Extend(A[i]); SAM.Samort(); SAM.Solve(n); }}
阅读全文
0 0
- [Codeforces][后缀自动机]Helvetic Coding Contest 2017 . I Fake News (hard)
- [构造][杂题]Helvetic Coding Contest 2017. H Fake News && CODE FESTIVAL 2016 Grand Final . G FESTIVAL
- [最大似然估计 MLE] Codeforces 802DEF Helvetic Coding Contest 2017 D. E. F. Marmots
- codeforces Helvetic Coding Contest 2016 D2 The Wall (medium)
- codeforces 802I 后缀自动机
- Helvetic Coding Contest 2016 C2. Brain Network (medium)
- codeforce690A2 Collective Mindsets (medium) (from :Helvetic Coding Contest 2016 online mirror )
- 后缀自动机合集 I
- CodeForces 128B --后缀自动机
- codeforces802H Fake News (medium) -- 构造
- Hard Coding
- hard coding
- codeforces 123 D String(后缀自动机 SAM)
- codeforces 127D Password (后缀自动机 SAM)
- Codeforces 235C Cyclical Quest 后缀自动机
- Codeforces 235C. Cyclical Quest 后缀自动机
- 【后缀自动机】 CodeForces 235C Cyclical Quest
- CodeForces 235C(后缀自动机)
- 自定义Git及Git简单的命令总结
- 进程学习1--atexit
- 关于EL表达式的复习
- io 哈哈哈估计只有我自己能看懂
- hibernate学习笔记01--基本API
- [Codeforces][后缀自动机]Helvetic Coding Contest 2017 . I Fake News (hard)
- 玲珑oj 1125
- JavaScript异步与同步之三-异步详解
- bzoj1428: Christopher
- mysql之 日志体系(错误日志、查询日志、二进制日志、事务日志、中继日志)
- rtmp视频直播的简单实现
- Target Sum
- 微信小程序进阶篇
- 建站之旅-必读