后缀自动机还是只会打板子,已经知道它是个什么东西了,但还是和它的构造联系不起来。。先背板子吧。

后缀自动机有一个很好的特性就是可以涵盖所有不重复的子串,我们利用这一点在它上面dp就行了;

代码参考:http://blog.csdn.net/fuxey/article/details/51050474

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int mod=,maxn=;
char ss[maxn],s[maxn];
int tt[maxn],last,cur=,cnt=,n,len,sum[maxn],ch[maxn][],fa[maxn],dis[maxn];
int c[maxn],q[maxn];
void add(int c,int id){
last=cur;cur=++cnt;
int p=last;dis[cur]=id;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=cur;
if(!p)fa[cur]=;
else{
int q=ch[p][c];
if(dis[q]==dis[p]+)fa[cur]=q;
else{
int nt=++cnt;dis[nt]=dis[p]+;
memcpy(ch[nt],ch[q],sizeof(ch[]));
fa[nt]=fa[q];fa[q]=fa[cur]=nt;
for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nt;
}
}
}
void rsort(){
memset(c,,sizeof(c));
for(int i=;i<=cnt;++i)c[dis[i]]++;
for(int i=;i<=cnt;++i)c[i]+=c[i-];
for(int i=;i<=cnt;++i)q[c[dis[i]]--]=i;
}
int main(){
while(cin>>n){
cnt=;memset(fa,,sizeof(fa));
memset(ch,,sizeof(ch));
for(int i=;i<=n;++i){
scanf("%s",ss+);
cur=;len=strlen(ss+);
for(int j=;j<=len;++j)add(ss[j]-'',j);
}
rsort();//先排一遍序保证计算顺序没有问题,保证一个点在更新别的点之前已经被所有能到它的点更新过;
memset(sum,,sizeof(sum));memset(tt,,sizeof(tt));
sum[]=;tt[]=;
for(int i=,j;i<=cnt;++i){
j=q[i];
for(int k=,t;k<;++k){
if(j==&&(!k))continue;
t=ch[j][k];
(sum[t]+=tt[j]*k+sum[j]*)%=mod;
(tt[t]+=tt[j])%=mod;
//cout<<sum[t]<<' '<<tt[t]<<endl;
}
}
int ans=;
for(int i=;i<=cnt;++i){(ans+=sum[i])%=mod;}
printf("%d\n",ans);
}
//system("pause");
return ;
}
/*
5
101
123
09
000
1234567890
*/

最新文章

  1. App Store最新审核标准,中文版
  2. ini配置文件格式
  3. secureCRT的一些小知识
  4. ---Linux 10 年的硕果累累啊!
  5. php PDO:数据访问抽象层
  6. 李洪强iOS经典面试题131
  7. Elasticsearch 插件安装
  8. MVC ViewModel
  9. 支持IE6的树形节结构TreeTable
  10. 丰富的else语句及简洁的with语句 - 零基础入门学习Python034
  11. JavaScript基础知识----document对象
  12. A Game of Thrones(17) - Bran
  13. C/C++生成随机数
  14. 给萌新的Flexbox简易入门教程
  15. Nginx upstream的5种权重分配方式分享
  16. Golang 入门系列(八) cron定时任务
  17. Eclipse 自动生成 Ant的Build.xml 配置文件
  18. UniDBGrid增加显示记录数的label及隐藏refresh按钮
  19. python socket 老生常谈
  20. 序列化 pickle shelve json configparser

热门文章

  1. python3:实现字符串的全排列(无重复字符)
  2. 【Linux】【Selenium】安装Chrome和ChromeDriver的配置
  3. Error creating bean with name &#39;transactionManager&#39;
  4. 笔记:MYSQL四种事务隔离级。
  5. Linux下编译安装FFmpeg
  6. Android TextView 跑马灯效果 - 2018年6月19日
  7. MySQL中基于mysqldump和二进制日志log-bin进行逻辑备份以及基于时间点的还原
  8. python-imaging-tk : Depends: python-imaging (= 1.1.7-4ubuntu0.12.04.3) but 3.1.2-0ubuntu1.1 is to be installed E: Unable to corre
  9. 大端&amp;小端问题
  10. python 常用模块(一): os模块,序列化模块(json模块 pickle模块 )