SAM基本操作 拓扑寻求每个节点  最左边的出现left,最右边的出现right,已经有几个num ......

对于每个出现两次以上的节点。对其所相应的一串子串的长度范围 [fa->len+1,len] 和其最大间距 right-left比較

就可以......

Boring counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1552    Accepted Submission(s): 637

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
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int CHAR=26,maxn=1100; struct SAM_Node
{
SAM_Node *fa,*next[CHAR];
int len,id,pos;
SAM_Node(){}
SAM_Node(int _len)
{
len=_len;
fa=0; memset(next,0,sizeof(next));
}
}; SAM_Node SAM_node[maxn*2],*SAM_root,*SAM_last;
int SAM_size; SAM_Node *newSAM_Node(int len)
{
SAM_node[SAM_size]=SAM_Node(len);
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} SAM_Node *newSAM_Node(SAM_Node *p)
{
SAM_node[SAM_size]=*p;
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} void SAM_init()
{
SAM_size=0;
SAM_root=SAM_last=newSAM_Node(0);
SAM_node[0].pos=0;
} void SAM_add(int x,int len)
{
SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
np->pos=len; SAM_last=np;
for(;p&&!p->next[x];p=p->fa)
p->next[x]=np;
if(!p)
{
np->fa=SAM_root;
return ;
}
SAM_Node *q=p->next[x];
if(q->len==p->len+1)
{
np->fa=q;
return ;
}
SAM_Node *nq=newSAM_Node(q);
nq->len=p->len+1;
q->fa=nq; np->fa=nq;
for(;p&&p->next[x]==q;p=p->fa)
p->next[x]=nq;
} char str[maxn];
int len,c[maxn],L[maxn*2],R[maxn*2],num[maxn*2];
SAM_Node *top[maxn*2]; int main()
{
while(scanf("%s",str)!=EOF)
{
if(str[0]=='#') break;
SAM_init();
len=strlen(str);
for(int i=0;i<len;i++)
SAM_add(str[i]-'a',i+1); memset(c,0,sizeof(c)); memset(top,0,sizeof(top));
memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(num,0,sizeof(num)); ///get tupo sort
for(int i=0;i<SAM_size;i++)
c[SAM_node[i].len]++;
for(int i=1;i<=len;i++)
c[i]+=c[i-1];
for(int i=0;i<SAM_size;i++)
top[--c[SAM_node[i].len]]=&SAM_node[i]; ///get L,R,num
SAM_Node *p=SAM_root;
for(;p->len!=len;p=p->next[str[p->len]-'a'])
{
num[p->id]=1;
L[p->id]=R[p->id]=p->len;
}
for(int i=SAM_size-1;i>=0;i--)
{
p=top[i];
if(L[p->id]==0&&R[p->id]==0)
{
L[p->id]=R[p->id]=p->pos;
}
if(p->fa)
{
SAM_Node *q=p->fa;
num[q->id]+=num[p->id];
if(L[q->id]==0||L[q->id]>L[p->id])
L[q->id]=L[p->id];
if(R[q->id]==0||R[q->id]<R[p->id])
R[q->id]=R[p->id];
}
}
int ans=0;
for(int i=1;i<SAM_size;i++)
{
int ma=SAM_node[i].len;
int mi=SAM_node[i].fa->len+1;
int le=R[SAM_node[i].id]-L[SAM_node[i].id];
if(le>=ma)
ans+=ma-mi+1;
else if(le>mi)
ans+=le-mi+1;
}
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDOJ 3518 Boring counting的更多相关文章

  1. HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)

    Boring counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 后缀数组 --- HDU 3518 Boring counting

    Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...

  3. HDU 3518 Boring counting

    题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...

  4. 【HDOJ】3518 Boring Counting

    后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10 ...

  5. hdu 3518 Boring counting 后缀数组基础题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  6. HDU 3518 Boring counting(后缀数组,字符处理)

    题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...

  7. hdu 3518 Boring counting 后缀数组LCP

    题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output ...

  8. hdu 3518 Boring counting 后缀数组

    题目链接 根据height数组的性质分组计算. #include <iostream> #include <vector> #include <cstdio> #i ...

  9. hdu 3518 Boring counting 后缀数组 height分组

    题目链接 题意 对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\). 思路 枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分. 显然,对于每一 ...

随机推荐

  1. android 视频通话开启呼叫等待后,来第三方的视频通话,接通后通话时间一直显示为0,过几秒之后视频通话自己主动挂断

    开启通话设置视频通话的"来电等待"; 步骤1:測试机和配合机A处于视频通话过程中; 步骤2:配合机B向測试机呼出视频电话; 步骤3:測试机接听配合机B的视频来电; 现象:视频通话过 ...

  2. [Python 学习] 两、在Linux使用平台Python

    在本节,它介绍了Linux如何使用平台Python 1. Python安装. 今天,大多数把自己的版本号Python的,它不能被安装.假设你要安装它,可以使用相应的安装指令. Fedora:先以roo ...

  3. IOC/DI的基本思想

    IOC/DI的基本思想 1.把程序之间的依赖关系去掉 2.把程序对象设置到IOC/DI容器的配置中作为Bean 3.由IOC/D.容器来管理Bean的创建和实例化 4.由IOC/DI容器来把Bean之 ...

  4. NYOJ 1068 ST(段树 为段更新+间隔总和)

    ST 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 "麻雀"lengdan用随机数生成了后台数据.可是笨笨的他被妹纸的问题给难住了. .. 已知 ...

  5. Unity2D实现贴图凹凸感并接受实时光照效果

    先看终于效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  6. Spring MVC框架搭建

    Spring MVC篇一.搭建Spring MVC框架 本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij ...

  7. 思考的工作方式——计划经济or市场经济

    背景:单位成立了技术领先的基础部门.专注于产品规划的技术解决方案部门.产品的发展规划方向.批准的项目和各部门的其他工作方案.工作内容是在这一领域没有问题.毕竟,从过去企业发展的一个部门模型现在是一个功 ...

  8. Web Design 再生:UX Design

    高质量的Web 模板,成熟的Design Pattern,人工智能的引用,移动技术的冲击是否标志着Web Design 结束的时代已经到来? Web Design 最终也未避免与“死亡”这个词的关联, ...

  9. Shell编程入门(再版)(在)

    简单的演示样本Shell规划 演示样例1. #!/bin/bash #This is to show what a shell script looks like echo "Our fir ...

  10. PLSQL:[1]plsql中文乱码,显示问号

    PLSQL运行sql语句,不识别中文.输出的中文标题显示成问号?? ?? 工具/原料 PLSQL Developer 9 方法/步骤 1 登陆plsql,运行sql语句.输出的中文标题显示成问号??? ...