Title Link: http://uoj.ac/problem/35
This is a template problem.
Read in a length ofn 1 " > 1 To n " > n
Besides, in order to further prove that you do have the ability to sort suffixes, please also output n? 1 integers represent the length of the longest common prefix of the adjacent suffix after sorting.
Input format
A single line of n -length strings containing only lowercase English letters.
Output format
First linen integers, and i integers represent the position of the first character in the original string of the suffix of I.
Second lineN?1An integer, section< Span class= "Mjx_assistive_mathml" >i integer representation i "> < Span class= "Mjx_assistive_mathml" >i and ranked i + 1 ">i< Span id= "mathjax-span-41" class= "Mo" >+1 The length of the longest public prefix of the suffix.
Sample One input
Ababa
Output
5 3 1) 4 2
1 3 0 2
Explanation
After sorting the result is:
- a
- aba
- ababa
- ba
- baba
Restrictions and conventions
1 & #x2264; N & #x2264; 5 > 1≤n ≤ 10^5
time limit : 1 s " > 1s
space Limit : Up toMB
Feelings:
To uoj very porcelain ah, suffix array naked questions, do not understand the self-Baidu
A big Uncle's blog Link: http://blog.csdn.net/shiqi_614/article/details/7982915
Code
#include <cstdio>#include<cstring>using namespacestd;inti,j,k,n,m,x,y,t,a[200010],b[100010],b1[100010],d[100010],rk[100010],p[100010],h[100010],s1[100010];intheight[100010],ans1[100010];Chars[100010];structdata{intx, y;} c[100010],e[100010];voidGet_sa () { for(i=1; i<=n;i++) b[s[i]-'a']=1; for(i=1;i< -; i++) b[i]+=b[i-1]; for(i=1; i<=n;i++) a[i]=b[s[i]-'a']; for(k=1; k<=n;k*=2){ for(i=1; i<=n;i++) c[i].x=a[i],c[i].y=a[i+K]; memset (b,0,sizeofb); for(i=1; i<=n;i++) b[c[i].y]++; for(i=1; i<=n;i++) b[i]+=b[i-1]; for(i=1; i<=n;i++) d[b[c[i].y]]=i,b[c[i].y]--; for(i=1; i<=n;i++) e[i]=C[d[i]]; for(i=1; i<=n;i++) c[i]=E[i]; for(i=1; i<=n;i++) p[i]=D[i]; memset (b,0,sizeofb); for(i=1; i<=n;i++) b[c[i].x]++; for(i=1; i<=n;i++) b[i]+=b[i-1]; for(i=n;i>=1; i--) d[b[c[i].x]]=i,b[c[i].x]--; for(i=1; i<=n;i++) e[i]=c[d[i]],rk[i]=D[i]; for(i=1; i<=n;i++) c[i]=E[i]; s1[1]=1; for(i=2; i<=n;i++)if(c[i].x!=c[i-1].x| | c[i].y!=c[i-1].Y) s1[i]=s1[i-1]+1;Elses1[i]=s1[i-1]; for(i=1; i<=n;i++) a[p[rk[i]]]=S1[i]; } for(i=1; i<=n;i++) rk[a[i]]=i; for(i=1; i<=n;i++) printf ("%d", Rk[i]);p rintf ("\ n");}voidGet_h () {intk=0; for(intI=1; i<=n;i++){ if(a[i]==1) h[i]=k=0; Else{J=rk[a[i]-1]; if(k>0) k--; while(I+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++; H[i]=K; } Height[a[i]]=H[i]; } for(intI=2; i<=n; i++) printf ("%d", Height[i]);}intMain () {gets (s); N=strlen (s); for(i=n;i>=1; i--) s[i]=s[i-1]; Get_sa (); Get_h (); return 0;}
Uoj35 suffix sort