POJ3581 Sequence (suffix array)

Source: Internet
Author: User

Test instructions: Give a string, the first word of the string transmitting the back of the big, to divide it into three paragraphs, and then reverse each paragraph, to obtain the dictionary order of the smallest string is what.

First, the first paragraph is ok: reverse the original string, because the first character is the largest, it is unique, there is no inverted string suffix between the inclusion of the relationship, so take the smallest suffix this is the first paragraph of the string;

Then the next two paragraphs, if you determine the split position can be found that the two string composition is a split position from the counter-clockwise loop back to the string--

The next requirement is the smallest representation of the inverse of the remainder, which can be done with a suffix array: The answer is at the minimum and the length is greater than or equal to the length of the original string.

Note that the divided segment is not empty. So also in the first paragraph, the next two paragraphs of the Division to add some judgment.

The problem is so difficult a, I don't know why. WA to WA to go. Change, change, finally re to go to re. After the other people to disperse the number of AC.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 #defineMAXN 2222227 intWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];8 intcmpint(RNintAintBintl) {9     returnR[A]==R[B] && r[a+l]==r[b+l];Ten } One intSA[MAXN]; A voidSA (int*r,intNintm) { -     int*x=wa,*y=WB; -  the      for(intI=0; i<m; ++i) ws[i]=0; -      for(intI=0; i<n; ++i) ++ws[x[i]=R[i]]; -      for(intI=1; i<m; ++i) ws[i]+=ws[i-1]; -      for(inti=n-1; i>=0; -I.) sa[--ws[x[i]]]=i; +  -     intp=1; +      for(intj=1; p<n; j<<=1, m=2) { Ap=0; at          for(intI=n-j; i<n; ++i) y[p++]=i; -          for(intI=0; i<n; ++i)if(SA[I]&GT;=J) y[p++]=sa[i]-J; -          for(intI=0; i<n; ++i) wv[i]=X[y[i]]; -          for(intI=0; i<m; ++i) ws[i]=0; -          for(intI=0; i<n; ++i) + +Ws[wv[i]]; -          for(intI=1; i<m; ++i) ws[i]+=ws[i-1]; in          for(inti=n-1; i>=0; -I.) sa[--ws[wv[i]]]=Y[i]; -Swap (x, y); x[sa[0]]=0; p=1; to          for(intI=1; i<n; ++i) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p1:p + +; +     } - } the  * intA[MAXN],R[MAXN]; $ intb[maxn],bn;Panax Notoginseng intMain () { -     intN; thescanf"%d",&n); +      for(intI=0; i<n; ++i) { Ascanf"%d", A +i); theb[i]=A[i]; +     } -Sort (b,b+n); $Bn=unique (B,b+n)-b; $      for(intI=0; i<n; ++i) { -A[i]=lower_bound (B,b+bn,a[i])-b+1; -     } the      for(intI=0; i<n; ++i) { -r[i]=a[n-i-1];Wuyi     } ther[n]=0; -SA (r,n+1, bn+1); Wu     intm=0; -      for(intI=1; i<=n; ++i) { About         if(sa[i]>=2){ $              for(intJ=sa[i]; j<n; ++j) { -printf"%d\n", b[r[j]-1]); -             } -n=Sa[i]; A              for(intj=0; j<sa[i]; ++j) { +r[j+sa[i]]=R[j]; them+=2; -             } $r[m]=0; the              Break; the         } the     } theSA (r,m+1, bn+1); -      for(intI=1; i<=m; ++i) { in         if(M-sa[i]>=n && sa[i]!=m/2&& sa[i]!=0){ the              for(intj=0; j<n; ++J) printf ("%d\n", b[r[sa[i]+j]-1]); the              Break; About         } the     } the     return 0; the}

POJ3581 Sequence (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.