Spoj Sublex lexicographical Substring Search-suffix array

Source: Internet
Author: User

Topic Portal

Portal I

Portal II

Main topic

Given a string, ask it multiple times for its $k$ large-nature different substring, outputting it.

Consider suffix trie. Consider each suffix in turn to add the number of essentially different substrings, it is obvious that it is $n-sa[i]-height[i]$.

After finding out the $height$ array, we can find the prefix of the number of substrings of the essence and the two points of each query.

Here you can go directly offline, $O (n + m) $ sweep just fine.

Code
1 /**2 * Spoj3 * Problem#sublex4 * Accepted5 * time:30ms6 * memory:19456k7  */8#include <bits/stdc++.h>9 using namespacestd;TentypedefBOOLBoolean; One  AtypedefclassPair3 { -      Public: -         intx, y, id; the }pair3; -  -typedefclassQuery { -      Public: +         intK, S, T, id; -  +Booleanoperator< (Query b)Const { A             returnK <B.K; at         } - }query; -  - Const intN = 1e5 +5, M =505; -  - intN, M; in CharStr[n], bs[n]; - intRk[n <<1], Sa[n], hei[n], cnt[n]; to Pair3 Ps[n], buf[n]; + Query qs[m]; -  theInlinevoidinit () { *scanf"%s", str +1); $n = strlen (str +1);Panax Notoginsengscanf"%d", &m); -      for(inti =1; I <= m; i++) thescanf"%d", &AMP;QS[I].K), qs[i].id =i; + } A  theInlinevoidRadix_sort (pair3* x, pair3*y) { +     intm = ((n > the) ? (n): ( the)); -memset (CNT,0,sizeof(int) * (M +1)); $      for(inti =1; I <= N; i++) cnt[x[i].y]++; $      for(inti =1; I <= m; i++) Cnt[i] + = cnt[i-1]; -      for(inti =1; I <= N; i++) y[cnt[x[i].y]--] =X[i]; -memset (CNT,0,sizeof(int) * (M +1)); the      for(inti =1; I <= N; i++) cnt[y[i].x]++; -      for(inti =1; I <= m; i++) Cnt[i] + = cnt[i-1];Wuyi      for(inti = n; I i--) x[cnt[y[i].x]--] =Y[i]; the } -  WuInlinevoidBuild_sa () { -      for(inti =1; I <= N; i++) AboutRk[i] =Str[i]; $      for(intK =1, dif =0; K <= N; K <<=1, dif =0) { -          for(inti =1; I <= N; i++) -ps[i].x = Rk[i], ps[i].y = Rk[i + K], ps[i].id =i; - Radix_sort (PS, buf); Ark[ps[1].id] = + +dif; +          for(inti =2; I <= N; i++) theRk[ps[i].id] = (dif + = (ps[i].x! = ps[i-1].x | | Ps[i].y! = ps[i-1].y)); -         if(DIF = =N) $              Break; the     } the      for(inti =1; I <= N; i++) theSa[rk[i]] =i; the } -  inInlinevoidget_height () { the      for(inti =1, J, k =0; I <= N; i++) { the         if(k) k--; About         if(Rk[i] >1) { the              for(j = Sa[rk[i]-1]; i + K <= n && j + k <= n && str[i + K] = = Str[j + K]; k++); theHei[rk[i]] =K; the}Else +hei[1] =0; -     } the }Bayi  theInlinevoidsolve () { the Build_sa (); - get_height (); - //for (int i = 1; I <= n; i++) the //cerr << hei[i] << ""; the //cerr << Endl; theSort (qs +1, QS + M +1); the     Long LongCur =0; -      for(inti =1, NQ =1, Delta; I <= n && nq <= m; i++) { theDelta = n-sa[i] +1-Hei[i]; the         if(cur + Delta <qs[nq].k) theCur + =Delta;94         Else { theQS[NQ].S = Sa[i], qs[nq].t = Sa[i] + hei[i] + (QS[NQ].K-cur); thenq++, i--; the         }98     } About      for(inti =1; I <= m; i++) -          while(Qs[i].id! =i)101 swap (Qs[qs[i].id], qs[i]);102      for(inti =1; I <= m; i++) {103         intLen = qs[i].t-Qs[i].s;104memcpy (BS, str +Qs[i].s, Len); theBs[len] =0;106 puts (BS); 107     }108 }109  the intMain () {111 init (); the solve ();113     return 0; the}

Spoj Sublex lexicographical Substring Search-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.