6
17
2014
1

Suffix Automaton

namespace sam{
    const int N=100000,L=26;
    int pr[N*2+10],ln[N*2+10],tr[N*2+10][L+10],nw;
    int make(){++nw;fill(tr[nw],tr[nw]+L,0);return nw;}
    void init(){nw=0;make();}
    int add(int c,int p){
        if(tr[p][c]){
            int q=tr[p][c];
            if(ln[q]==ln[p]+1)return q;
            else{
                int np=make();ln[np]=ln[p]+1;pr[np]=pr[q];
                copy(tr[q],tr[q]+L,tr[np]);pr[q]=np;
                for(;p&&tr[p][c]==q;p=pr[p])tr[p][c]=np;
                return np;
            }
        }else{
            int np=make();ln[np]=ln[p]+1;
            for(;p&&!tr[p][c];p=pr[p])tr[p][c]=np;
            if(!p)pr[np]=1;
            else{
                int q=tr[p][c];
                if(ln[q]==ln[p]+1)pr[np]=q;
                else{
                    int nq=make();ln[nq]=ln[p]+1;pr[nq]=pr[q];
                    copy(tr[q],tr[q]+L,tr[nq]);pr[q]=pr[np]=nq;
                    for(;p&&tr[p][c]==q;p=pr[p])tr[p][c]=nq;
                }
            }
            return np;
        }
    }
}
Category: 未分类 | Tags: | Read Count: 435
Avatar_small
jnanabhumiap.in 说:
2024年1月21日 01:04

JNANABHUMI AP provides all latest educational updates and many more. The main concept or our aim behind this website has been the will to provide resources full information on each topic which can be accessed through Internet. To ensure that every readers get’s what important and worthy about the topic they search and link to hear from us. jnanabhumiap.in Jnanabhumi AP is a startup by passionate webmasters and bloggers who have passion to provide engaging content which is accurate, interesting and worthy to read. We are mope like a web community where you can find different information’s, resources, topics on day to day incidents or news. We provide you the finest of web content on each and every topics possible with help of editorial and content team.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com