标签:des style http java color strong
SAM基本操作 拓扑求每个节点的 最左出现left,最右出现right,出现了几次num ......
对于每一个出现两次以上的节点,对其所对应的一串子串的长度范围 [fa->len+1,len] 和其最大间距 right-left比较
即可......
aaaa ababcabb aaaaaa #
2 3 3
#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,布布扣,bubuko.com
标签:des style http java color strong
原文地址:http://blog.csdn.net/ck_boss/article/details/36891749