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”).
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;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。