bzoj4199: [Noi2015] Wine Tasting Conference (and collection && suffix array)

Source: Internet
Author: User

It is said that using the suffix Automaton + DP can also be done

However, it does not

How does the suffix array approach

is to build a suffix array, to find the height value, at this time if the direct search, the complexity is n ^ 2, will definitely time out.

But the height of the large value is not to affect the small, so can be the height size, from large to small combined two intervals, with and check set maintenance can be

The code is as follows

1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 Const intN =300010;6typedefLong Longll;7 CharS[n];8 ll A[n];9 intN;Ten intSa[n], c[n], x[n], y[n]; One  A structE { -     inth, L, R; -InlineBOOL operator< (ConstE o)Const { the         returnH >o.h; -     } - } G[n]; -InlinevoidBUILDSA (intm) { +      for(inti =0; I < m; i++) C[i] =0; -      for(inti =0; I < n; i++) C[x[i] = s[i]]++; +      for(inti =1; I < m; i++) C[i] + = c[i-1]; A      for(inti = n-1; I >=0; i--) Sa[--c[x[i]] =i; at      for(intK =1; K <= N; K <<=1) { -         intp =0; -          for(inti = n-k; I < n; i++) y[p++] =i; -          for(inti =0; I < n; i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; -          for(inti =0; I < m; i++) C[i] =0; -          for(inti =0; I < n; i++) c[x[y[i]]]++; in          for(inti =1; I < m; i++) C[i] + = c[i-1]; -          for(inti = n-1; I >=0; i--) sa[--c[x[y[i] []] =Y[i]; top =1; swap (x, y); +x[sa[0]] =0; -          for(inti =1; I < n; i++) theX[sa[i]] = y[sa[i]] = = Y[sa[i-1]] && Y[sa[i] + K] = = y[sa[i-1] + K]? P-1: p++; *         if(P >= N) Break; $m =p;Panax Notoginseng     } - } the  + intHeight[n], rank[n]; AInlinevoidgetheight () { the      for(inti =0; I < n; i++) Rank[sa[i] =i; +     intK =0; -      for(inti =0; I < n; i++) { $         if(k) k--; $         intj = Sa[rank[i]-1]; -          while(S[j + K] = = s[i + K]) k++; -Height[rank[i]] =K; the     } - }Wuyi  the ll Fa[n], Size[n], ans[n], ans1[n], ans2[n], minn[n], maxn[n]; - voidUnion (intXinty) { WuFA[X] =y; -Size[y] + =Size[x]; AboutAns[y] = max (Ans[y], max (maxn[y] * maxn[x], Minn[y] *minn[x])); $Maxn[y] =Max (Maxn[y], maxn[x]); -Minn[y] =min (minn[y], minn[x]); - } -  A intFindintx) { +     return(Fa[x] = = x)?X:find (fa[x]); the } -  $ intMain () { thescanf"%d", &n); the GetChar (); the      for(inti =0; I < n; i++) thescanf"%c", &s[i]), s[i]-='a'-1; -      for(inti =0; I < n; i++) inscanf"%lld", &a[i]); thes[n++] =0; theBUILDSA ( -); About getheight (); the      for(inti =2; I < n; i++) { theG[i].h =Height[i]; theG[I].L =Sa[i]; +G[I].R = Sa[i-1]; -     } theSort (g +2, G +n);Bayi      for(inti =0; I < n; i++) Fa[i] = i, size[i] =1, maxn[i] = A[i], minn[i] =A[i]; thememset (ans,0x80,sizeof(ans)); thememset (Ans2,0x80,sizeof(ANS2)); -      for(inti =2; I < n; i++) { -         intx =find (G[I].L); the         inty =find (G[I].R); theAns1[g[i].h] + = (LL) size[x] *Size[y]; the Union (x, y); theAns2[g[i].h] =Max (Ans2[g[i].h], ans[y]); -     } the      for(inti = n-3; I >=0; i--) { theAns1[i] + = Ans1[i +1]; the         if(Ans1[i +1]) Ans2[i] = max (Ans2[i], ans2[i +1]);94     } the      for(inti =0; I < n-1; i++) printf ("%lld%lld\n", Ans1[i], ans1[i]? Ans2[i]:0); the     return 0; the}

bzoj4199: [Noi2015] Wine Tasting Conference (and collection && 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.