4698. [Sdoi2008]sandy card "suffix array"

Source: Internet
Author: User

Descriptionsandy and Sue are passionate about collecting cards in plain faces. However, Sue collects cards because of the beautiful characters on the cards, while Sandy is trying to save the cards for a super-glamorous character model. Each card is marked by a number of cards, the sequence length of the first card is MI, to redeem the character model, first must be set enough n cards, for the n card, if they have a same substring length of k, you can redeem a character model of grade K. The same definition is: two substrings of the same length and all elements of a string plus one number will become another string. Sandy's number of cards was much smaller than the required n, so Sue decided to send her card to Sandy on Sandy's birthday, and with Sue's help, Sandy finally had enough n cards, but Sandy didn't know which level of character model he could redeem, and now, Please help Sandy and Sue to see what level of character models they are best able to get. Input first behavior is a number n, which means that you can redeem the minimum number of cards required for the character model, that is, Sandy now has a number of cards i+1 line to line i+n the first number of each row of the card sequence length mi, then j+1 to j+1+mi number, separated by a space, Indicates the number of J in the sequence N<=1000,m<=1000,2<=mi<=101output a number k, which indicates the highest level that can be obtained. Sample Input2
2 1 2
3 4 5 9Sample Output2 emmmm a very uncomfortable suffix array
altogether three steps but only thought the first two steps (the most mentally retarded step did not want to come out ...)
First Connect all the differences of the neighbors to make an SA, set different cards for different colors
and then two minutes to see if there is an SA interval that has a common prefix greater than that length and covers all colors to satisfy the
It is illegal to ask for a public prefix if the prefix of the suffix crosses two colors (where it is pits)

#include <iostream> #include <cstring> #include <cstdio> #define MAXN (1000000+10) using namespace std; int N,m=2050,t,h,maxn;int R[maxn],a[maxn],col[maxn];int Sa[maxn],rank[maxn],height[maxn];int wt[MAXN],wa[MAXN],wb[ Maxn];int stack[maxn],top;bool used[maxn];bool cmp (int *y,int a,int b,int k) {int arank1=y[a];int brank1=y[b];int arank2= A+k>=n?-1:y[a+k];int Brank2=b+k>=n?-1:y[b+k];return arank1==brank1 && arank2==brank2;}    void Build_sa () {int *x=wa,*y=wb;    for (int i=0;i<m;++i) wt[i]=0;    for (int i=0;i<n;++i) wt[x[i]=r[i]]++;    for (int i=1;i<m;++i) wt[i]+=wt[i-1];         for (int i=n-1;i>=0;--i) sa[--wt[x[i]]]=i;        for (int j=1;j<=n;j<<=1) {int p=0;        for (int i=n-j;i<n;++i) y[p++]=i;            for (int i=0;i<n;++i) if (sa[i]>=j) y[p++]=sa[i]-j;        for (int i=0;i<m;++i) wt[i]=0;        for (int i=0;i<n;++i) wt[x[y[i]]]++;        for (int i=1;i<m;++i) wt[i]+=wt[i-1]; for (int i=n-1;i>=0;--i) Sa[--wt[x[y[i]]]]=y[i];        M=1;swap (x, y);        x[sa[0]]=0;        for (int i=1;i<n;++i) x[sa[i]]=cmp (y,sa[i],sa[i-1],j)? m-1:m++;    if (m>=n) break; }}void Build_height () {for (int i=0;i<n;++i) Rank[sa[i]]=i;int k=0; height[0]=0;for (int i=0;i<n;++i) {if (! Rank[i]) Continue;int j=sa[rank[i]-1];if (k) k--;while (R[i+k]==r[j+k]) k++; Height[rank[i]]=k;}} BOOL Check (int len) {int sum=0;for (int i=0;i<n;++i) {if (Height[i]<len) {sum=0;while (top) used[stack[top--]]= false;} if (!used[col[sa[i]] && col[sa[i]]==col[sa[i]+len-1]) {Used[col[sa[i]]]=true;sum++;stack[++top]=col[sa[i] ];} if (sum==t) return true;} return false;} int main () {scanf ("%d", &t), for (int. i=1;i<=t;++i) {scanf ("%d", &h), Maxn=max (maxn,h); for (int j=1;j<=h;++ j) {scanf ("%d", &a[j]); if (j==1) continue;r[n++]=a[j]-a[j-1]+510; Col[n-1]=i;}} Build_sa (); Build_height (); int L=0,r=105;while (l<r) {int mid= (l+r+1) >>1;if (check (mid)) l=mid;elser=mid-1;} printf ("%d",l+1);} 

4698. [Sdoi2008]sandy card "suffix array"

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.