[POJ1743] Musical Theme (suffix array + dichotomy/suffix automaton)

Source: Internet
Author: User
Tags extend cmath

Transmission Door

reviewed the suffix array.
First, because of the transpose we need to make a difference to the string.

Consider the practice of a binary + suffix array.
Two-point answer K, the continuous height[i]>=x segment grouping, if the maximum value of the SA within a group and the difference between the minimum value >=k, then K is feasible.

The suffix automaton is also possible.
Because the right set is the set of the rvalue that the substring appears in the parent string, and you can find out where the string will appear, then find the maximum and minimum values for each point, if their difference is not less than max at that point, Then it means that the substring represented by this position repeats and is not coincident, updating the answer with Max (because Max is the longest of the substrings contained in this node, if the first occurrence and the last occurrence of the substring contained in the node is more than Max, the middle is So there are at least two or more non-coincident substrings).

I think the suffix automaton is better.

Suffix array:

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std;
int a[21000];
int rsort[21000],rank[21000],sa[21000],y[21000],wr[21000];
    BOOL CMP (int k1,int K2,int LN) {return wr[k1]==wr[k2]&&wr[k1+ln]==wr[k2+ln];} void Get_sa (int n,int m) {
    memset (rsort,0,sizeof (Rsort));
    for (int i=1;i<=n;i++) rank[i]=a[i];
    for (int i=1;i<=n;i++) rsort[rank[i]]++;
    for (int i=1;i<=m;i++) rsort[i]+=rsort[i-1];
    for (int i=n;i>=1;i--) sa[rsort[rank[i]]--]=i;
    int p=0,k=0,ln=1;
        while (p<n) {k=0;
        for (int i=n-ln+1;i<=n;i++) y[++k]=i;

        for (int i=1;i<=n;i++) if (SA[I]&GT;LN) Y[++k]=sa[i]-ln;
        memset (rsort,0,sizeof (Rsort));
        for (int i=1;i<=n;i++) rsort[rank[y[i]]]++;
        for (int i=1;i<=m;i++) rsort[i]+=rsort[i-1];

        for (int i=n;i>=1;i--) sa[rsort[rank[y[i]]]--]=y[i];
        for (int i=1;i<=n;i++) wr[i]=rank[i]; Rank[sa[1]]=1;p=1;
        for (int i=2;i<=n;i++) {if (CMP (SA[I],SA[I-1],LN) ==0) p++;
        Rank[sa[i]]=p;
    } ln*=2;m=p;
}} int height[21000];
    void get_he (int n) {int k=0;
        for (int i=1;i<=n;i++)//position {int j=sa[rank[i]-1];
        if (k) k--;
        while (A[i+k]==a[j+k]) k++;
    Height[rank[i]]=k;
        }} bool Check (int k,int n) {for (int i=2;i<=n;i++) {if (height[i]<k) continue;
            for (int j=i-1;j>=1;j--) {if (ABS (Sa[i]-sa[j]) >=k) return true;
        if (height[j]<k) break;
}} return false;
    } int main () {int n;
        while (scanf ("%d", &n)!=eof && N) {for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
        int mx=-1e8;
            for (int i=1;i<n;i++) {a[i]=a[i+1]-a[i]+88;
        Mx=max (Mx,a[i]);
        } get_sa (N,MX);
        Get_he (n);
        int ans=1,l=1,r=n;
 while (L&LT;=R)       {int mid= (L+R)/2;
                if (check (mid,n) ==1) {ans=mid;
            l=mid+1;
        } else r=mid-1;
        } if (ans+1>=5) printf ("%d\n", ans+1);
    else printf ("0\n");
} return 0; }

Suffix automata:
tried the array version of the SAM, feel no structural version of the write cool AH.

#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <
cmath> const int n=40003;
using namespace Std;
int n,m,cnt,last,root,a[n],rs[n],sa[n];
int pre[n],mxx[n],ch[n][180],mx[n],mn[n];
    void Extend (int k) {int x=a[k]; int p=last; int np=++cnt; 
    LAST=NP; 
    mxx[np]=mxx[p]+1;
    while (P &&!ch[p][x]) ch[p][x]=np,p=pre[p];
    if (!p) pre[np]=root;
        else {int q=ch[p][x];
        if (mxx[q]==mxx[p]+1) pre[np]=q; 
            else {int nq=++cnt; 
            mxx[nq]=mxx[p]+1;
            memcpy (ch[nq],ch[q],sizeof (CH[NQ));
            PRE[NQ]=PRE[Q];
            PRE[Q]=PRE[NP]=NQ;
        while (P && ch[p][x]==q) ch[p][x]=nq,p=pre[p];
    }}} int solve () {memset (rs,0,sizeof (Rs));
    for (int i=1;i<=cnt;i++) rs[mxx[i]]++;
    for (int i=1;i<=n;i++) rs[i]+=rs[i-1];
    for (int i=1;i<=cnt;i++) sa[rs[mxx[i]]--]=i;
    int p=1; memset (mx,0,sizeof (MX));
    memset (Mn,127,sizeof (MN)); 
        for (int i=1;i<=n;i++) {p=ch[p][a[i]];
    Mx[p]=mn[p]=i;
        } for (int i=cnt;i;i--) {int t=sa[i];
        Mx[pre[t]]=max (Mx[pre[t]],mx[t]);
    Mn[pre[t]]=min (Mn[pre[t]],mn[t]);
    } int ans=0;

    for (int i=1;i<=cnt;i++) if (Mx[i]-mn[i]>=mxx[i]) Ans=max (Ans,mxx[i]);
    ans++;
    if (ans<5) ans=0;
return ans;
    } int main () {//Freopen ("A1.in", "R", stdin);//Freopen ("A1.out", "w", stdout);
        while (1) {scanf ("%d", &n); 
        if (!n) break;
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]); 
        for (int i=1;i<n;i++) a[i]=a[i+1]-a[i]+88;
        n--;
        memset (mxx,0,sizeof (MXX));
        memset (pre,0,sizeof (pre)); 
        memset (ch,0,sizeof (ch)); cnt=0;
        last=root=++cnt; 
        for (int i=1;i<=n;i++) extend (i);
    printf ("%d\n", Solve ()); }
}

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.