【后缀自动机】[SPOJ LCS2]Longest Common Substring II

思路就是对于建立第一个串的SAM然后存储每一个节点和当前的这个串能够匹配的最大长度,然后存储对于当前的这个节点全局的能够匹配的最小长度,然后每次更新完当前之后要对每个节点根据len排序然后向前对失配边的上一个节点的当前能够匹配的最大长度进行更新,就是{原来的匹配最大长度, 原来的len, 当前节点的最大匹配} 最后在全局匹配的变量里面取最大值就行了

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 200000;
const int INF = 100000000;
struct node{
    node *p[26], *pre;
    int len, now, max;
    node(){
        memset(p, 0, sizeof p);
        pre = NULL;
        len = 0, max = INF, now = 0;
    }
}Edges[MAXN*2+10], *ecnt=Edges+1,*root=Edges,*last=Edges, *st[MAXN*2+10];
void Insert(int w){
    node *np = ecnt++;
    node *p = last;
    np->len = p->len+1;
    while(p&&!p->p[w])
        p->p[w]=np, p=p->pre;
    if(!p){
        np->pre = root;
    }else{
        node *q = p->p[w];
        if(p->len+1 == q->len){
            np->pre = q;
        }else{
            node *nnd = ecnt++;
            memcpy(nnd->p, q->p, sizeof (nnd->p));
            nnd->len = p->len+1; nnd->pre = q->pre; q->pre = nnd; np->pre = nnd;
            while(p&&p->p[w]==q)
                p->p[w]=nnd, p=p->pre;
        }
    }
    last = np;
}
const int MAXT = 100000;
char s1[MAXT+10], s2[MAXT+10];
bool vis[MAXN+10];
bool cmp(node *a, node *b){
    return a->len > b->len;
}
int main(){
    scanf("%s", s1);
    int l1 = strlen(s1), ans = 0;
    for(int i=0;i<l1;i++)
        Insert(s1[i]-'a');
    int ds = 0;
    for(node *p = &Edges[1];p!=ecnt;p++)
        st[ds++] = p;
    sort(st, st+ds, cmp);
    while(scanf("%s", s2)!=EOF){
        int l2 = strlen(s2);
        node *p = root; int len = 0;
        for(int j=0;j<l2;j++){
            int now = s2[j]-'a';
            if(p->p[now]){
                p = p->p[now];
                len++;
            }
            else{
                while(p&&!p->p[now])
                    p = p->pre;
                if(!p)
                    len=0, p = root;
                else
                    len=p->len+1, p=p->p[now];
            }
            p->now = max(p->now, len);
        }
        for(int i=0; i<ds; i++){
            st[i]->max = min(st[i]->max, st[i]->now);
            if(st[i]->pre) st[i]->pre->now = max(st[i]->pre->now, min(st[i]->pre->len, st[i]->now));
            st[i]->now = 0;
        }
    }
    for(int i=0;i<ds-1;i++)
        ans = max(st[i]->max, ans);
    printf("%d\n", ans);

    return 0;
}

posted on 2015-06-06 15:56  JeremyGuo  阅读(110)  评论(0编辑  收藏  举报

导航