【poj2774】Long Long Message 后缀自动机

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother.

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:

  1. All characters in messages are lowercase Latin letters, without punctuations and spaces.
  2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long.
  3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer.
    E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc.
  4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different.

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.

Background:
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.

Why ask you to write a program? There are four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :(

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

Source

POJ Monthly–2006.03.26,Zeyuan Zhu,”Dedicate to my great beloved mother.”

学会SAM了!

把一个串建SAM,另一个在上面跑,跑的时候更新答案…
怎么更新见代码…
感谢善良的小绿学长

O(n)的SAM110ms,O(log^2n)的SA1600+ms,吓尿了…

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; 

const int SZ = 1000010; 

struct sam_node{
    sam_node *par,*ch[30];
    int val;
}T[SZ], *root, *last;

int Tcnt = 0;

sam_node* newnode(int x)
{
    sam_node *k = T + (Tcnt ++);
    memset(k -> ch,0,sizeof(k -> ch));
    k -> val = x;
    k -> par = 0;
    return k;
}

void insert(int x)
{
    sam_node *p = last;
    sam_node *np = newnode(p -> val + 1);
    while(p && !p -> ch[x])
        p -> ch[x] = np,p = p -> par;
    if(!p)
        np -> par = root;
    else
    {
        sam_node *q = p -> ch[x];
        if(q -> val == p -> val + 1)
            np -> par = q;
        else
        {
            sam_node *nq = newnode(p -> val + 1);
            memcpy(nq -> ch,q -> ch,sizeof(q -> ch));
            nq -> par = q -> par;
            q -> par = nq;
            np -> par = nq;
            while(p && p -> ch[x] == q)
                p -> ch[x] = nq,p = p -> par;
        }
    }
    last = np;
}

int ask(char s[])
{
    int ans = 0;
    int tmp = 0;
    int l = strlen(s);
    sam_node *p = root;
    for(int i = 0;i < l;i ++)
    {
        int c = s[i] - 'a' + 1;
        if(p -> ch[c])
        {
            tmp ++;
            p = p -> ch[c];
        }
        else
        {
            while(p != root && !p -> ch[c])
                p = p -> par;
            tmp = p -> val;
            if(p -> ch[c]) 
                p = p -> ch[c], tmp++;
        }
        ans = max(ans,tmp);
    }
    return ans;
}

void init()
{
    root = newnode(0);
    last = root;
}

char s[SZ];

int main()
{
    init();
    scanf("%s",s);
    int l = strlen(s);
    for(int i = 0;i < l;i ++)
        insert(s[i] - 'a' + 1);
    scanf("%s",s);
    printf("%d\n",ask(s));
    return 0;
}


你可能感兴趣的:(【poj2774】Long Long Message 后缀自动机)