首页 > 代码库 > HDOJ 3518 Boring counting
HDOJ 3518 Boring counting
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
2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
#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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。