Using a binary + hash to find the suffix array

Source: Internet
Author: User

Personal feeling suffix array of the board is too difficult to back, listen to the small train speak two points + hash can be implemented to find a suffix array, seemingly very good understanding, the code is as follows.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;TentypedefLong LongLL; Onetypedef unsignedLong LongULL; A Const intmaxn=200000; - ConstULL base=127; - CharS[MAXN]; the intSA[MAXN],RANK[MAXN]; -ULL A[MAXN],Base[Maxn],hash[maxn],len; - structh{ -     intPos; + }SUF[MAXN]; -Inline ULL Get_hash (intLintR) { +     returnhash[r]-hash[l-1]*Base[r-l+1]; A } atInlineintFindintLintRintF1,intF2) { -     if(L +1>=R) { -         if(Get_hash (f1,f1+r-1) ==get_hash (f2,f2+r-1))returnR; -         Else returnl; -     } -     intMid= (l+r) >>1; in     if(Get_hash (f1,f1+mid-1) ==get_hash (f2,f2+mid-1))returnfind (MID,R,F1,F2); -     Else returnFind (l,mid-1, F1,F2); to } +InlineBOOLcmpConstH &x,ConstH &y) { -     intlen1=len-x.pos+1, len2=len-y.pos+1; the     intMaxx=find (0, min (len1,len2) +1, X.pos,y.pos); *     if(Maxx==min (LEN1,LEN2) +1)returnlen1<Len2; $     Else returna[x.pos+maxx]<a[y.pos+Maxx];Panax Notoginseng } - intHEIGHT[MAXN]; theInlinevoidcalheight () { +     intk=0; A      for(intI=1; i<=len;i++){ the         if(k!=0) k--; +          for(intj=sa[rank[i]-1];; k++){ -             if(A[i+k]!=a[j+k]) Break; $         } $height[rank[i]]=K; -     } -     return ; the } - intMain () {Wuyiscanf"%s", s+1); Len=strlen (s+1); the      for(intI=1; i<=len;i++) A[i]=ull (s[i]-'a'+1); -     Base[0]=1; for(intI=1; i<=len;i++)Base[i]=Base[I1]*BASE; Wu      for(intI=1; i<=len;i++) hash[i]=hash[i-1]*base+A[i]; -      for(intI=1; i<=len;i++){ Aboutsuf[i].pos=i; $     } -Sort (suf+1, suf+len+1, CMP); -      for(intI=1; i<=len;i++){ -Sa[i]=suf[i].pos; rank[suf[i].pos]=i; A     } + calheight (); the      for(intI=1; i<=len;i++){ -cout<" "; $     } the     return 0; the}

Using a binary + hash to find the 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.