zoukankan      html  css  js  c++  java
  • SPOJ 1811 SAM 初探

    思路:

    一个串建SAM

    另一个串在SAM上跑

    //By SiriusRen
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=500050;
    int n;char a[N],b[N];
    struct SAM{
        int ch[N][26],dis[N],fa[N],root,tot,last;
        void init(){root=tot=last=1;}
        int newnode(int v){return dis[++tot]=v,tot;}
        void add(int x){
            int p=last,np=newnode(dis[p]+1);last=np;
            for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
            if(!p)fa[np]=root;
            else{
                int q=ch[p][x];
                if(dis[q]==dis[p]+1)fa[np]=q;
                else{
                    int nq=newnode(dis[p]+1);
                    memcpy(ch[nq],ch[q],sizeof(ch[q]));
                    fa[nq]=fa[q],fa[np]=fa[q]=nq;
                    for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq; 
                }
            }
        }
    }T;
    int main(){
        T.init();
        scanf("%s%s",a+1,b+1);
        int n=strlen(a+1),m=strlen(b+1),ans=0,len=0;
        for(int i=1;i<=n;i++)T.add(a[i]-'a');
        int p=T.root;
        for(int i=1;i<=m;i++){
            int x=b[i]-'a';
            if(T.ch[p][x])len++,p=T.ch[p][x];
            else{
                for(;p&&!T.ch[p][x];p=T.fa[p]);
                if(!p)p=T.root,len=0;
                else len=T.dis[p]+1,p=T.ch[p][x];
            }ans=max(ans,len);
        }printf("%d
    ",ans);
    }
  • 相关阅读:
    python爬取网络上图片【小例子】
    python统计英文单词出现次数【小例子】
    python敏感字处理【小例子】
    platform操作系统信息
    迭代器
    xxxxx
    test
    Jquery
    Jquery二
    DOM文档对象模型
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6770668.html
Copyright © 2011-2022 走看看