Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
 
Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
 
Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
 
Sample Input
aaaa
ababcabb
aaaaaa
#
 
Sample Output
2
3
3

emmmmmmm,WA了,因为L和R错了;

找L和R需要全部字母插入后拓扑排序求出。

如果不,maxlen(v)+1 < maxlen(x)的时候会拆点,导致x点和拆出的y点L,R出错。

。。。好像是个很弱智的问题,勿喷,我知道错啦。

和HDU4416差不多,写法上一个从0开始,一个从1开始。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=;
int tot,slink[*maxn],trans[*maxn][],maxlen[*maxn];
char str[*maxn];
int R[maxn],L[maxn],last;
long long ans;
void init()
{
ans=tot=;
last=;
memset(trans[],-,sizeof(trans[]));
memset(L,,sizeof(L));
memset(R,,sizeof(R));
slink[]=-; maxlen[]=;
}
void add_char(int pos,char chr)
{
bool ww=false;
int c=chr-'a',nq=;
int p=last,np=++tot;
maxlen[np]=maxlen[p]+;
memset(trans[np],-,sizeof(trans[np]));
while(p!=-&&trans[p][c]==-) trans[p][c]=np,p=slink[p];
if(p==-) slink[np]=;
else
{
int q=trans[p][c];
if(maxlen[q]!=maxlen[p]+)
{
nq=++tot;
memcpy(trans[nq],trans[q],sizeof(trans[q]));
maxlen[nq]=maxlen[p]+;
slink[nq]=slink[q];
slink[np]=slink[q]=nq;
while(p!=-&&trans[p][c]==q) trans[p][c]=nq,p=slink[p];
L[nq]=R[nq]=pos;
ww=true;
}
else slink[np]=q;
}
last=np;
for(;np>;np=slink[np]){
if(!L[np]) L[np]=pos;
R[np]=pos;
}
}
int main() {
while(~scanf("%s",str)){
if(str[]=='#') return ;
init();
int N=strlen(str);
for(int i=; i<N; i++) add_char(i+,str[i]);
for(int i=;i<=tot;i++) {
if(R[i]-L[i]>=maxlen[i]) ans+=maxlen[i]-maxlen[slink[i]];
else ans+=max(,R[i]-L[i]-maxlen[slink[i]]+);
}
printf("%lld\n",ans);
}
}

别人的AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lng long long
using namespace std; const int maxn = + ;
char s[maxn];
int str[maxn], len;
struct suffixautomaton
{
int ch[maxn][], pre[maxn], val[maxn], top[maxn];
int c[maxn];
int l[maxn], r[maxn];
int sz, last; void init() { pre[] = -; last = ; sz = ; memset(ch[], , sizeof(ch[])); } void insert(int x)
{
int p = last, np = sz++; last = np;
memset(ch[np], , sizeof(ch[np]));
val[np] = val[p] + ;
while(p != - && ch[p][x] == )
{
ch[p][x] = np;
p = pre[p];
}
if(p == -) pre[np] = ;
else
{
int q = ch[p][x];
if(val[q] == val[p] + )
pre[np] = q;
else
{
int nq = sz++;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
val[nq] = val[p] + ;
pre[nq] = pre[q];
pre[q] = pre[np] = nq;
while(p != - && ch[p][x] == q) { ch[p][x] = nq; p = pre[p]; }
}
}
} void solve()
{
memset(c, , sizeof(c));
for(int i = ; i < sz; ++i) c[val[i]] += ;
for(int i = ; i <= len; ++i) c[i] += c[i - ];
for(int i = ; i < sz; ++i) top[--c[val[i]]] = i;
for(int i = ; i < sz; ++i) { l[i] = len + ; r[i] = -; }
for(int i = ; ; i = ch[i][str[val[i]]])
{
l[i] = r[i] = val[i];
if(val[i] == len) break;
}
for(int i = sz - ; i > ; --i)
{
int u = top[i];
l[pre[u]] = min(l[pre[u]], l[u]);
r[pre[u]] = max(r[pre[u]], r[u]);
}
lng ans = ;
for(int i = ; i < sz; ++i)
{
if(r[i] - l[i] > val[pre[i]])
{
lng tmp = min(val[i], r[i] - l[i]);
ans += (tmp - val[pre[i]]);
}
}
printf("%I64d\n", ans);
}
}sam; int main()
{
while(~scanf("%s", s) && s[] != '#')
{
len = strlen(s);
sam.init();
for(int i = ; s[i]; ++i)
{
str[i] = s[i] - 'a';
sam.insert(str[i]);
}
sam.solve();
}
return ;
}

地址:http://blog.csdn.net/cool_Fires/article/details/9732475

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

  1. Boring counting HDU - 3518 后缀自动机

    题意: 对于给出的字符串S, 长度不超过1000, 求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次 题解: 将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出 ...

  2. HDU 5343 MZL's Circle Zhou 后缀自动机+DP

    MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. JavaScript:基础扩展(1)——JSON

    JavaScript:扩展知识(1)——JSON 理解: 关于 JSON,最重要的是要理解它是一种数据格式,不是一种编程语言.虽然具有相同的语法形式,但 JSON 并不从属于 JavaScript.而 ...

  2. windows安装mysql教程2017最新

    1.首先在mysql官网下载最新版mysql, 附上链接点击打开链接,根据你的系统型号选择对应的包下载,大约300多兆,版本号为5.7.19 下载完之后,解压缩,是一个标准的mysql文件 2.第二步 ...

  3. JQuery Div层滚动条控制(模拟横向滚动条在最顶端显示)

    想让DIV层滚动条显示在顶端,CSS样式没找到相关属性,于是用2个DIV层来模拟做了一个.经测试IE浏览器上显示并不太美观!不知道是否还有更好的办法可以实现这功能呢?   aaaaaaasssssss ...

  4. 对vector,list的操作函数

    向量只能接受同一类型的数据:list可以接受不同的数据. 1.添加元素 vector:> b=c(1,2,3) > b=c(b,"four") #直接在后面添加添加 & ...

  5. bash脚本之读取数据

    题目: 一个tab间隔的文件,读取时一行为一个循环,依次读取每行的参数. 比如第一行为:a b c ,输出为a+b+c #/bin/bash while read id do a=($id) b=${ ...

  6. [RK3288][Android6.0] TS-ADC驱动流程小结【转】

    本文转载自:https://blog.csdn.net/kris_fei/article/details/55045936 Platform: RK3288OS: Android 6.0Kernel: ...

  7. HDFS数据完整性

    数据完整性 Hadoop用户肯定都希望系统在存储和处理数据时不会丢失或损坏任何数据.尽管磁盘或网络上的每个I/O操作不太可能将错误引入自己正在读/写的数据中,但是如果系统中需要处理的数据量大到Hado ...

  8. HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)

    题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...

  9. Ceph简介

    最近偶尔接触到云计算,开始对云计算感兴趣,希望能够早日加入这个计算机领域的第三次革命中去. 估计这次革命要持续二十年,也就是这辈子一直干云计算都没问题. 先了解一下Ceph吧.本博文主要是根据文献[1 ...

  10. windows 10系统更新失败的解决办法

    最近遇到一个问题,系统总是自动下载一个更新,看样子是个补丁吧,但是每次提醒重新启动以安装更新,但每次又安装失败,回退.就这样,下载,重启,安装失败,下载......一直循环,搞得都不敢关机了,因为这样 ...