HDU4622(后缀自动机)
生活随笔
收集整理的這篇文章主要介紹了
HDU4622(后缀自动机)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:Reincarnation
題意:給定一個字符串,然后再給定Q個詢問,每個詢問是一個區間[l,r],問在這個字符串區間中有多少個不同的子串。
后綴自動機模版題:
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h>using namespace std; const int N=5010;struct State {State *pre,*go[26];int step;void clear(){pre=0;step=0;memset(go,0,sizeof(go));}int calc(){if(pre==0) return 0;return step-pre->step;} }*root,*last;State statePool[N*2],*cur;void init() {cur=statePool;root=last=cur++;root->clear(); }int tot; void Insert(int w) {State *p=last;State *np=cur++;np->clear();np->step=p->step+1;while(p&&!p->go[w])p->go[w]=np,p=p->pre;if(p==0){np->pre=root;tot+=np->calc();}else{State *q=p->go[w];if(p->step+1==q->step){np->pre=q;tot+=np->calc();}else{State *nq=cur++;nq->clear();memcpy(nq->go,q->go,sizeof(q->go));tot-=p->calc()+q->calc();nq->step=p->step+1;nq->pre=q->pre;q->pre=nq;np->pre=nq;tot+=p->calc()+q->calc()+np->calc()+nq->calc();while(p&&p->go[w]==q)p->go[w]=nq, p=p->pre;}}last=np; }int ans[N][N]; char s[N];void work() {scanf("%s",s);int n=strlen(s);for(int i=0;i<n;++i){init();tot=0;for(int j=i;j<n;++j){Insert(s[j]-'a');ans[i][j]=tot;}}int nQ;scanf("%d", &nQ);while (nQ--){int l, r;scanf("%d%d", &l, &r);--l,--r;printf("%d\n", ans[l][r]);} }int main() {int T;cin>>T;while(T--)work();return 0; }
?
總結
以上是生活随笔為你收集整理的HDU4622(后缀自动机)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1588(矩阵连乘求和)
- 下一篇: HDU4577(2013年ACM杭州赛区