• SPOJ1811 LCS


    传送门[洛谷]

    SAM板子题?(可惜我还是不会

    大概就是能匹配就一直往下匹配

    不能匹配就跳parent 调到能匹配为止 跳到根了就重新开始

    最开始太蠢了非要写递归版 写着写着发现不知道我要写啥了T^T 果断换循环。。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define inf 20021225
    #define ll long long
    #define mxn 250010
    using namespace std;
    
    struct node{int ch[26],fa,len;}t[mxn*4];
    int poi,rt,lt,ans,n;char ch[mxn];
    int id(char ch){return ch-'a';}
    void insert(int c)
    {
        int p=lt,np=lt=++poi; t[np].len=t[p].len+1;
        for(;p&&!t[p].ch[c];p=t[p].fa)	t[p].ch[c]=np;
        if(!p){t[np].fa=rt;return;}
        int q=t[p].ch[c];
        if(t[q].len==t[p].len+1){t[np].fa=q;return;}
        int nq=++poi;t[nq].len=t[p].len+1;
        memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));
        t[nq].fa=t[q].fa; t[q].fa=t[np].fa=nq;
        for(;p&&t[p].ch[c]==q;p=t[p].fa)	t[p].ch[c]=nq;
    }
    void find()
    {
        int pos=rt,cur=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=id(ch[i]);
            ans=max(ans,cur);
            if(t[pos].ch[tmp])	cur++,pos=t[pos].ch[tmp];
            else
            {
                for(;pos&&!t[pos].ch[tmp];pos=t[pos].fa);
                if(pos)	cur=t[pos].len+1,pos=t[pos].ch[tmp];
                else	cur=0,pos=rt;
            }
        }
        ans=max(ans,cur);
    }
    int main()
    {
        scanf("%s",ch+1);n=strlen(ch+1);
        rt=lt=++poi;
        for(int i=1;i<=n;i++)	insert(id(ch[i]));
        scanf("%s",ch+1);n=strlen(ch+1);
        find();
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    Eclipse启动不了
    第三章Hibernate关联映射
    System Generator简介
    Chipscope使用
    总线及数据传输技术【待完善】
    多相滤波器
    notepad++与ISE/Vivado关联
    常用IP核
    〖Linux〗Qt+gsoap开发客户端程序,服务端地址设定的字符串转换处理
    〖Android〗OK6410a的Android HAL层代码编写笔记
  • 原文地址:https://www.cnblogs.com/hanyuweining/p/10321918.html
Copyright © 2020-2023  润新知