Spoj LCS (suffix automaton)

Source: Internet
Author: User

(Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106071#problem/A)
Test instructions: Ask the longest identical substring of a/b
Solution: Directly construct a suffix automaton, make a wave of LCP just fine.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <queue> #include <tr1/unordered_set>//#include <tr1/unordered_map> #include <

bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; #define Lson L, M, Ls[rt] #define Rson m+1, R, Rs[rt] #define INF 1e9 #define DEBUG (a) cout << #a "=" << (a)
<< Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo

R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int maxn=2*250000+30;
    struct suffix_automaton{int SON[MAXN][30],PRE[MAXN],STEP[MAXN];
    int last,si; VoID pushback (int v) {step[++si]=v;
        Pre[si]=-1;
    CLR (son[si],-1);
        } void Extend (int ch) {pushback (step[last]+1);
        int p=last,np=si;
        while (P!=-1&&son[p][ch]==-1) son[p][ch]=np,p=pre[p];
        if (p==-1) pre[np]=0;
            else{int q=son[p][ch];
            if (step[q]==step[p]+1) pre[np]=q;
                else{pushback (step[p]+1);
                int nq=si;
                memcpy (son[nq],son[q],sizeof son[q]);
                PRE[NQ]=PRE[Q];
                PRE[Q]=PRE[NP]=NQ;
            while (P!=-1&AMP;&AMP;SON[P][CH]==Q) son[p][ch]=nq,p=pre[p];
    }} LAST=NP;
        } void Init () {si=last=0;
        CLR (son[0],-1);
        Pre[0]=-1;
    step[0]=0;
        } int LCS (char *s) {int l=0,ans=0,p=0;
            for (int i=0;s[i];i++) {int c=s[i]-' a ';
        while (P!=-1&&son[p][c]==-1)        P=PRE[P];
            if (p==-1) p=0,l=0;
                else{l=min (l,step[p]);
                P=SON[P][C];
            l++;
        } ans=max (ans,l);
    } return ans;

}}su;

Char S[MAXN];
    int main () {//Freopen ("Input.txt", "R", stdin);
    Freopen ("Out2.txt", "w", stdout);
        while (~SCANF ("%s", s)) {su.init (); for (int i=0;s[i];i++) su.
        Extend (s[i]-' a ');
        scanf ("%s", s);
    printf ("%d\n", Su.lcs (s));
} return 0;

 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.