问题:

Amateur astronomers Tom and Bob try to find radio broadcasts of extraterrestrial civilizations in the air. Recently they received some strange signal and represented it as a word consisting of small letters of the English alphabet. Now they wish to decode the signal. But they do not know what to start with.
      They think that the extraterrestrial message consists of words, but they cannot identify them. Tom and Bob call a subword of the message a potential word if it has at least two non-overlapping occurrences in the message.

For example, if the message is “abacabacaba”, “abac” is a potential word, but “acaba” is not because two of its occurrences overlap.
      Given a message m help Tom and Bob to find the number of potential words in it.

Input

      Input file contains one string that consists of small letters of the English alphabet. The length of the message doesn’t exceed 10 000.

Output

      Output one integer number — the number of potential words in a message.

Sample Input

abacabacaba

Sample Output

15

题意:

求字符串里的不相交重复字串的个数和。

思路;

后缀自动机:记录每个状态的最先出现和最后一次出现的位置,就可以判断是否出现了多次,以及是否相交。(116ms)

后缀数组:。。。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<memory>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
char chr[maxn];
int Head[maxn],Lates[maxn],Now;
struct SAM
{
int sz,Last,ch[maxn][],slink[maxn],maxlen[maxn],ans;
int c[maxn],pos[maxn],num[maxn];
void init()
{
Last=sz=;Head[]=Lates[]=;ans=;
memset(ch[],,sizeof(ch[]));
memset(Lates,,sizeof(Lates));
memset(Head,,sizeof(Head));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;num[np]=;
Head[np]=Now;Lates[np]=Now; memset(ch[np],,sizeof(ch[np]));
maxlen[np]=maxlen[p]+;
while(p&&!ch[p][x]) ch[p][x]=np,p=slink[p];
if(!p) slink[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) slink[np]=q;
else {
int nq=++sz; num[nq]=;
memcpy(ch[nq],ch[q],sizeof(ch[q])); Head[nq]=Head[q];Lates[nq]=Lates[q];
slink[nq]=slink[q],slink[np]=slink[q]=nq;
maxlen[nq]=maxlen[p]+;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=slink[p];
}
}
while(np>) Lates[np]=Now,np=slink[np];
}
void solve()
{
for(int i=;i<=sz;i++) {
int dis=Lates[i]-Head[i];
int mi=min(dis,maxlen[i]);
if(mi>=maxlen[slink[i]]) ans+=mi-maxlen[slink[i]];
}
printf("%d\n",ans);
}
};
SAM sam;
int main()
{
sam.init(); int l;
scanf("%s",chr); l=strlen(chr);
for(Now=;Now<l;Now++) sam.add(chr[Now]-'a'); sam.solve();
return ;
}

也可以刷新完了再跟新最后一次出现的位置(拓扑排序)(4ms)(优化一下居然时间排名第一了。。。)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<memory>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
char chr[maxn];
int Head[maxn],Lates[maxn],Now;
struct SAM
{
int sz,Last,ch[maxn][],slink[maxn],maxlen[maxn],ans;
int c[maxn],pos[maxn],num[maxn];
void init()
{
Last=sz=;Head[]=Lates[]=;ans=;
memset(ch[],,sizeof(ch[]));
memset(Lates,,sizeof(Lates));
memset(Head,,sizeof(Head));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;num[np]=;
Head[np]=Now;Lates[np]=Now; memset(ch[np],,sizeof(ch[np]));
maxlen[np]=maxlen[p]+;
while(p&&!ch[p][x]) ch[p][x]=np,p=slink[p];
if(!p) slink[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) slink[np]=q;
else {
int nq=++sz; num[nq]=;
memcpy(ch[nq],ch[q],sizeof(ch[q])); Head[nq]=Head[q];Lates[nq]=Lates[q];
slink[nq]=slink[q],slink[np]=slink[q]=nq;
maxlen[nq]=maxlen[p]+;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=slink[p];
}
}
//while(np>1) Lates[np]=Now,np=slink[np];
}
void sort()
{
for(int i=;i<=sz;i++) c[i]=;
for(int i=;i<=sz;i++) c[maxlen[i]]++;
for(int i=;i<=sz;i++) c[i]+=c[i-];
for(int i=;i<=sz;i++) pos[c[maxlen[i]]--]=i;
for(int i=sz;i>=;i--) Lates[slink[pos[i]]]=max(Lates[pos[i]],Lates[slink[pos[i]]]);
}
void solve()
{ ans=;
for(int i=;i<=sz;i++) {
int dis=Lates[i]-Head[i];
int mi=min(dis,maxlen[i]);
if(mi>=maxlen[slink[i]]) ans+=mi-maxlen[slink[i]];
}
printf("%d\n",ans);
}
};
SAM sam;
int main()
{
sam.init();int l;
scanf("%s",chr);l=strlen(chr);
for(Now=;Now<l;Now++) sam.add(chr[Now]-'a');
sam.sort();sam.solve();
return ;
}

ACdream1430SETI(后缀自动机)的更多相关文章

  1. BZOJ 后缀自动机四·重复旋律7

    后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...

  2. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

  3. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  4. 【BZOJ-3998】弦论 后缀自动机

    3998: [TJOI2015]弦论 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2018  Solved: 662[Submit][Status] ...

  5. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. hihoCoder 后缀自动机三·重复旋律6

    后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi ...

  7. hihoCoder #1445 : 后缀自动机二·重复旋律5

    #1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...

  8. 数据结构:后缀自动机 WJMZBMR讲稿的整理和注释

    链接放在这里,有点难理解,至少我个人是的. 后缀自动机是一种有限状态自动机,其功能是识别字符串是否是母串的后缀.它能解决的问题当然不仅仅是判断是不是后缀这种事,跟字符串的连续子串有关的问题都可以往这个 ...

  9. 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)

    http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...

随机推荐

  1. Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...

  2. ios上ZXing库的配置流程

    本文转载至 http://blog.csdn.net/louercab/article/details/26448587 步骤 首先,用Xcode创建我们的demo, 取名TestZXing(根据自己 ...

  3. double,long double及各变量数组内存开销

    IEEE754浮点数的表示方法.C语言里对float类型数据的表示范围为-3.4*10^38-+3.4*10^38.double为-1.7*10^-308~1.7*10^308,long double ...

  4. Linux 服务器配置JDK

    1. 查看java版本 [root@plttestap5 ~]# java -versionjava version "1.8.0_121"Java(TM) SE Runtime ...

  5. Ajax之基础总结

    一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...

  6. 最简单的 GitExtensions 教程(持续更新中)

    一.安装 GitExtensions 下载 GitExtensions 完全版,一直点 Next,安装全部组件. 二.将项目文件夹/文件提交到 Git 服务器(以 GitHub 为例) 新建一个文件夹 ...

  7. Codeforces441C_Valera and Tubes(暴力)

    Valera and Tubes time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. 关于align-items和align-content的区别和使用场景

    最近在研究flex布局,容器中有两个属性,是用来定义crossAxis测轴排列方式的.一开始接触align-items还可以理解感觉不难,后来看到align-content就感觉有点混淆了,特开一篇博 ...

  9. iOS __weak 和 __block 的使用探讨

    在基本的开发中遇到 需要弱引用时候 我一般 用 weak  预防 死锁的时候 我会用 block 的确没出过大错  但是这样处理 的确有点囫囵  现在我想好好理解一下这两个修饰符 "bloc ...

  10. 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array

    描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...