zoukankan      html  css  js  c++  java
  • spoj LCS 后缀自动机

    链接:http://www.spoj.com/problems/LCS/

    题意两串LCS

    确实没什么好说的,第一次编嘛。把网上的教程斗翻出来看一遍就好了。

    另发现,百度内部用户交流使用的图片在百度快照中看得到。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define MAXN 2510000
    #define MAXT MAXN*2
    struct SAM_node
    {
            int pnt,len;
            int nxt[26];
            void Print(int id=-1)
            {
                    printf("-----------------
    ");
                    printf("Sam id:%d
    ",id);
                    printf("Next :
    ");
                    for (int i=0;i<26;i++)
                            printf("[%c:%d];",i+'a',nxt[i]);
                    printf("
    ");
                    printf("Parent:%d
    ",pnt);
                    printf("Length:%d
    ",len);
            }
    }sam[MAXT];
    int last=1;
    int topt=1;
    void Add_item(char ch)
    {
            int p,np;
            p=last;
            np=++topt;
            sam[np].len=sam[p].len+1;
            while (p && !sam[p].nxt[ch-'a'])
                    sam[p].nxt[ch-'a']=np,p=sam[p].pnt;
            if (!p)
            {
                    sam[np].pnt=1;
                    last=np;
            }else
            {
                    int q=sam[p].nxt[ch-'a'];
                    if (sam[q].len==sam[p].len+1)
                            sam[np].pnt=q;
                    else
                    {
                            int nq;
                            nq=++topt;
                            sam[nq]=sam[q];
                            sam[nq].len=sam[p].len+1;
                            sam[nq].pnt=sam[q].pnt;
                            sam[q].pnt=nq;
                            sam[np].pnt=nq;
                            while (p && sam[p].nxt[ch-'a']==q)
                            {
                                    sam[p].nxt[ch-'a']=nq;
                                    p=sam[p].pnt;
                            }
                    }
                    last=np;
            }
    }
    
    
    char str[MAXN];
    char str2[MAXN];
    int main()
    {
            freopen("input.txt","r",stdin);
            scanf("%s",str);
            int n=strlen(str);
            for (int i=0;i<n;i++)
                    Add_item(str[i]);
            for (int i=0;i<=topt;i++)
            {
                    //sam[i].Print(i);
            }
            scanf("%s",str2);
            int now=1,res=0,ans=0;
            int m=strlen(str2);
            for (int i=0;i<m;i++)
            {
                    if (sam[now].nxt[str2[i]-'a'])
                    {
                            now=sam[now].nxt[str2[i]-'a'];
                            res++;
                            ans=max(ans,res);
                    }else
                    {
                            while (now && !sam[now].nxt[str2[i]-'a'])
                                    now=sam[now].pnt;
                            if (!now)
                            {
                                    now=1;
                                    res=0;
                            }
                            else
                            {
                                    res=sam[now].len+1;
                                    now=sam[now].nxt[str2[i]-'a'];
                                    ans=max(ans,res);
                            }
                    }
            }
            printf("%d
    ",ans);
    }
  • 相关阅读:
    .Matrix-第三篇冲刺随笔
    .Matrix-第二篇冲刺随笔
    Alpha冲刺-第九次冲刺笔记
    Alpha冲刺-第八次冲刺笔记
    Alpha冲刺-第七次冲刺笔记
    Alpha冲刺-第六次冲刺笔记
    Alpha冲刺-第五次冲刺笔记
    Alpha冲刺-第四次冲刺笔记
    Alpha冲刺-第三次冲刺笔记
    Alpha冲刺-第二次冲刺笔记
  • 原文地址:https://www.cnblogs.com/mhy12345/p/4430010.html
Copyright © 2011-2022 走看看