POJ 3882 (stammering aliens) suffix array or hash

Source: Internet
Author: User
Tags cmath

Suffix array: Construct a suffix array, and note that you want to add a character that does not appear at the end of the string. Then it can be 2 points or direct scan, the direct scan needs to use the monotone queue to maintain

VIEW CODE

#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include <queue > #include <stack> #include <string> #include <cstring> #include <map> #include <vector > #include <set> #include <ctime> #include <stdlib.h>using namespace Std;const int mmax= 40010;const int mod=1000000007;typedef Long Long ll;//typedef unsigned long long ull;char s[mmax];int sa[mmax],t[mmax],t2[mmax],c[    Mmax],n;void build_sa (int m) {int i,*x=t,*y=t2;    for (i=0;i<m;i++) c[i]=0;    for (i=0;i<n;i++) c[x[i]=s[i]]++;    for (i=1;i<m;i++) c[i]+=c[i-1];    for (i=n-1;i>=0;i--) sa[--c[x[i]]]=i;        for (int k=1;k<=n;k<<=1) {int p=0;        for (i=n-k;i<n;i++) y[p++]=i;        for (i=0;i<n;i++) if (sa[i]>=k) y[p++]=sa[i]-k;        for (i=0;i<m;i++) c[i]=0;        for (i=0;i<n;i++) c[x[y[i]]]++;        for (i=1;i<m;i++) c[i]+=c[i-1];        for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];      Swap (x, y);  p=1;x[sa[0]]=0;        for (i=1;i<n;i++) x[sa[i]] = Y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if (p>=n) break;    M=p;    }}int rank[mmax],heigth[mmax];void Getheigth () {for (int i=0;i<n;i++) rank[sa[i]]=i;    int k=0;        for (int i=0;i<n;i++) {if (k) k--;            if (Rank[i]) {int j=sa[rank[i]-1];            while (S[i+k]==s[j+k]) k++;        Heigth[rank[i]]=k;    }}}int len[mmax];int mark[mmax];int head,end,head1,end1;int main () {int m;        while (scanf ("%d", &m) &&m) {head=end=0;        head1=end1=0;        scanf ("%s", s);            if (m==1) {printf ("%d%d\n", strlen (s), 0);        Continue        } n=strlen (s);        S[n++]= ' $ ';        s[n]=0;        BUILD_SA (131);        Getheigth ();                for (int i=1;i<m;i++) {while (End>head && heigth[len[end-1]]>= heigth[i])            end--; LeN[end++]=i;            while (End1>head1 && Sa[mark[end1-1]] <sa[i]) end1--;        Mark[end1++]=i;        } int Ans=heigth[len[head]];        int E=sa[mark[head1]];            for (int i=m;i<n;i++) {if (I-m+1==len[head]) head++;            while (End>head && heigth[len[end-1]]>= heigth[i]) end--;            Len[end++]=i;            if (I-m==mark[head1]) head1++;            while (End1>head1 && Sa[mark[end1-1]] <sa[i]) end1--;            Mark[end1++]=i;                if (Heigth[len[head]]>ans) {Ans=heigth[len[head]];            E=SA[MARK[HEAD1]];        } else if (Heigth[len[head]]==ans) E=max (E,sa[mark[head1]]);            } if (ans==0) {puts ("none");        Continue    } printf ("%d%d\n", ans,e); } return 0;}

String hash: Direct 2 points well written.

VIEW CODE

#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include <queue > #include <stack> #include <string> #include <cstring> #include <map> #include <vector > #include <set> #include <ctime> #include <stdlib.h>using namespace std;const int mmax= 1000010; const int Mod=1000000007;const int inf=0x3fffffff;using namespace std;typedef long long ll;typedef unsigned long long ULL ; ULL h[mmax];map<ull,int>q;int M;char Str[mmax];    ULL pow[mmax];void Build_ha () {int n=strlen (str);    h[n]=0; for (int i=n-1;i>=0;i--) h[i]=h[i+1]*131+str[i];}    ULL ha[mmax];bool ok (int k) {int n=strlen (str);        for (int i=0;i+k-1<n;i++) {ULL tmp=h[i]-h[i+k]*pow[k];    ha[i]=tmp;    } sort (ha,ha+n-k+1);    int cnt=0;        for (int i=0;i<n-k+1;i++) {if (I && ha[i]==ha[i-1]) cnt++;            else {if (cnt>=m) return 1;        Cnt=1;}} if (cnt>=m) return 1; return 0;}    int main () {pow[0]=1;    for (int i=1;i<mmax;i++) pow[i]=pow[i-1]*131;        while (cin>>m&&m) {scanf ("%s", str);        int N=strlen (str);        int l=1,r=n+1;        Build_ha ();            while (l<r) {int mid= (L+R) >>1;            if (OK (mid)) l=mid+1;        else R=mid;        } int ans=r-1;            if (!ans) {puts ("none");        Continue        } int e;        Q.clear ();            for (int i=0;i+ans-1<n;i++) {ULL Tmp=h[i]-h[i+ans]*pow[ans];            q[tmp]++;        if (q[tmp]>=m) e=i;    } printf ("%d%d\n", ans,e); } return 0;}


POJ 3882 (stammering aliens) suffix array or hash

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.