POJ 1509 suffix Array

Source: Internet
Author: User

Click to open link

Test instructions: Give the smallest suffix to a string, but this string is a ring

Idea: is a ring or processing, then the string copy is added to the back of the line, and then to find the suffix array and height array, just at the beginning I wrote is to find the suffix array sa, and then from the ranking 1th, such as found I, ranked I from the sa[i], then if this value is less than the length of the input string, Direct output on the line, submit WA, look at the discussion, there is such an array, aaaa then should output 1, and that program output 4, and then think of the height array, just start to find the position of 4, and then find the next rank of the height array, if the value of LCP is greater than the length of the string, then the result is updated And so on until the value of the LCP is no longer greater than the length of the original string, and why is greater than the length of the original string can continue, for example, Abab, into a abababab,sa array of 4 8 3 7 2 6 1 5,LCP array is 0 2 4 6 0 1 3 5, then we find the first POS is 3, then it The next is the POS 1 position, if the value of lcp[3] is greater than the original string length of 4, it means that the string starting from 1, can replace the string starting from 3, because the final length is certainly 4, in the specific point, look at the time to take a picture, the string from 3 to represent 3 4 1 2, It's 3 4 5 6 on the string that's copied over, and 1 1 2 3 from 4, so if LCP[3] is greater than 4, it means 3 4 5 6 and 1 2 3 4 are the same strings, understand? ~ ~ ~

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < Algorithm>using namespace Std;typedef long long ll;const int inf=0x3f3f3f3f;const int Maxn=20010;char str[maxn];int RA Nk1[maxn],tmp[maxn],sa[maxn],lcp[maxn];int n,k;bool Compare_sa (int i,int j) {if (Rank1[i]!=rank1[j]) return rank1[i]&    LT;RANK1[J];        else{int ri=i+k<=n? rank1[i+k]:-1; int rj=j+k<=n?        Rank1[j+k]:-1;    Return ri<rj;        }}void Construct_sa () {for (int i=0;i<=n;i++) {sa[i]=i; Rank1[i]=i<n?    Str[i]:-1;        } for (k=1;k<=n;k*=2) {sort (SA,SA+1+N,COMPARE_SA);        tmp[sa[0]]=0;        for (int i=1;i<=n;i++) {tmp[sa[i]]=tmp[sa[i-1]]+ (Compare_sa (Sa[i-1],sa[i])? 1:0);    } for (int i=0;i<=n;i++) rank1[i]=tmp[i];    }}void CONSTRUCT_LCP () {for (int i=0;i<=n;i++) rank1[sa[i]]=i;    int h=0;    lcp[0]=0;        for (int i=0;i<n;i++) {int j=sa[rank1[i]-1]; if (h>0) h--;        for (; i+h<n&&j+h<n;h++) if (str[i+h]!=str[j+h]) break;    Lcp[rank1[i]-1]=h;    }}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%s", str);        int Nn=strlen (str);        for (int i=0;i<nn;i++) str[nn+i]=str[i];        N=2*nn;construct_sa ();        CONSTRUCT_LCP ();        int pos=n+1;                for (int i=1;i<=n;i++) {if (SA[I]&LT;NN) {pos=sa[i++];                while (I&LT;=N&AMP;&AMP;LCP[I-1]&GT;=NN) {pos=min (pos,sa[i]); i++;            } break;    }} printf ("%d\n", pos+1); } return 0;}

POJ 1509 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.