2017.3.4[hihocoder#1403] suffix Array one • Repeat Melody

Source: Internet
Author: User

Long time no bo.

Suffix Array Board question. The concrete realization does not explain, hihocoder very conscience.

http://hihocoder.com/problemset/problem/1403

1#include <cmath>2#include <queue>3#include <cstdio>4#include <vector>5#include <cstdlib>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineN 20010Ten #defineRG Register One #defineINF 0x3f3f3f3f A #defineINF 99999999999999999LL - using namespacestd; -typedefLong LongLL; the intH,k,n,t,ans,a[n],b[n],q[n],sa[n],ssa[n],cnta[n],cntb[n],heit[n],rank[n],music[n]; -InlineintAbs (RGConst int&a) {returnA>0? a:-A;} -InlineintMax (RGConst int&a,rgConst int&AMP;B) {returnA>b?a:b;} -InlineintMin (RGConst int&a,rgConst int&AMP;B) {returnA>b?b:a;} +Inlineintgi () { -Rgintx=0; RgBOOLflag=0; RgCharC=GetChar (); +      while((c<'0'|| C>'9') &&c!='-') c=GetChar (); A     if(c=='-') C=getchar (), flag=1; at      while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); -     returnflag?-x:x; - } -InlinevoidGetsa () { -      for(intI=1; i<=n;++i) + +Cnta[music[i]]; -      for(intI=1; i<= -; ++i) cnta[i]+=cnta[i-1]; in      for(intI=n;i;--i) sa[cnta[music[i]]--]=i; -rank[sa[1]]=1; to      for(intI=2; i<=n;++i) { +rank[sa[i]]=rank[sa[i-1]]; -         if(music[sa[i]]!=music[sa[i-1]]) the++Rank[sa[i]]; *     } $      for(intnow=1; rank[sa[n]]<n;now<<=1){Panax Notoginsengmemset (Cnta,0,sizeof(Cnta)); -memset (CNTB,0,sizeof(CNTB)); the          for(intI=1; i<=n;++i) { +++cnta[a[i]=rank[i]];//rank is the first keyword that was sorted last A++cntb[b[i]= (i+now<=n)? Rank[i+now]:0];//Rank[i+now] is the second keyword the         } +          for(intI=1; i<=n;++i) cntb[i]+=cntb[i-1]; -          for(intI=n;i;--i) ssa[cntb[b[i]]--]=i;//The SSA is the "false" sa by the second keyword $          for(intI=1; i<=n;++i) cnta[i]+=cnta[i-1]; $          for(inti=n;i;--i) Sa[cnta[a[ssa[i]]]--]=ssa[i];//equivalent to replacing I with ssa[i] to achieve the purpose of double-keyword ordering -rank[sa[1]]=1; -          for(intI=2; i<=n;++i) { therank[sa[i]]=rank[sa[i-1]]; -             if(a[sa[i]]!=a[sa[i-1]]|| b[sa[i]]!=b[sa[i-1]]) ++Rank[sa[i]];Wuyi         } the     } -      for(intI=1, j=0; i<=n;++i) { Wu         if(j)--J; -          while(music[i+j]==music[sa[rank[i]-1]+J]) + +J; Aboutheit[rank[i]]=J; $     } - }    -InlinevoidWork () { -N=gi (); k=gi (); A     if(k==1){ +printf"%d\n", n); the     return; -     } $      for(RGintI=1; i<=n;++i) themusic[i]=gi (); the Getsa (); the     //For (RG int i=1;i<=n;++i) cout< the     //cout<<endl; -      for(RGintI=1; i<=n;++i) { in      while(H<t&&heit[i]T; theq[++t]=i; the     //cout<<q[h+1]<< ' <<q[t]<<endl; About      while(q[h+1]<=q[t]-k+1) ++h; the     if(h<t) Ans=max (ans,heit[q[h+1]]); the     //cout<<ans<<endl; the     } +printf"%d\n", ans); - } the intMain () {Bayi Work (); the     return 0; the}
View Code

2017.3.4[hihocoder#1403] suffix Array one • Repeat Melody

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.