[luoguP2870] [Usaco07dec] Best bull line, Gold top Cow lines, gold (suffix array)

Source: Internet
Author: User

Portal

If the data is small, greed will be fine.

This string can be flipped back to the back, and then to find the suffix array, the rank array is very simple.

--code

1#include <cstdio>2#include <iostream>3 #defineN 600014 5 intN, len, m = the, sum;6 intBuc[n], x[n], y[n], sa[n], rank[n];7 CharS[n];8 9InlinevoidBuild_sa ()Ten { One     intI, K, p; A      for(i =0; I < m; i++) Buc[i] =0; -      for(i =0; i < Len; i++) Buc[x[i] = s[i]]++; -      for(i =1; I < m; i++) Buc[i] + = buc[i-1]; the      for(i = len-1; I >=0; i--) Sa[--buc[x[i]] =i; -      for(k =1; K <= Len; K <<=1) -     { -p =0; +          for(i = len-1; I >= len-k; i--) y[p++] =i; -          for(i =0; i < Len; i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; +          for(i =0; I < m; i++) Buc[i] =0; A          for(i =0; i < Len; i++) buc[x[y[i]]]++; at          for(i =1; I < m; i++) Buc[i] + = buc[i-1]; -          for(i = len-1; I >=0; i--) sa[--buc[x[y[i] []] =Y[i]; - Std::swap (x, y); -p =1, x[sa[0]] =0; -          for(i =1; i < Len; i++) -X[sa[i]] = y[sa[i-1] [= Y[sa[i]] && y[sa[i-1] + K] = = Y[sa[i] + K]? P-1: p++; in         if(P >= Len) Break; -m =p; to     } + } -  the intMain () * { $     intI, L, R;Panax Notoginsengscanf"%d", &n); -Len = n *2; the      for(i =0; I < n; i++) GetChar (), s[i] = GetChar (), S[len-i-1] =S[i]; + Build_sa (); A      for(i =0; i < Len; i++) Rank[sa[i] =i; theL =0, R = N-1; +      while(L <=R) -     { $         if(Rank[l] <= rank[len-r-1]) Putchar (s[l++]); $         ElsePutchar (s[r--]); -         if(++sum = = the) sum =0, Putchar ('\ n'); -     } the     return 0; -}
View Code

[luoguP2870] [Usaco07dec] Best bull line, Gold top Cow lines, gold (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.