链接: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("-----------------\n");
                printf("Sam id:%d\n",id);
                printf("Next :\n");
                for (int i=0;i<26;i++)
                        printf("[%c:%d];",i+'a',nxt[i]);
                printf("\n");
                printf("Parent:%d\n",pnt);
                printf("Length:%d\n",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\n",ans);
}

 

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-02-19
  • 2022-01-03
  • 2022-02-16
  • 2021-10-11
猜你喜欢
  • 2021-12-12
  • 2021-08-09
  • 2021-07-09
  • 2021-09-01
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
相关资源
相似解决方案