Light OJ 1314 Names for babies

Source: Internet
Author: User

http://www.lightoj.com/volume_showproblem.php?problem=1314

Test instructions: Given a string and a p,q, there are several strings for the length of the substring between P and Q.

Idea: The suffix array, the contribution for each position is min (n-sa[i],q), and then to subtract the duplicates and not the part, is Max (height[i],p-1), of course, also to 0 to take a max

1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 Chars[200005];7 intnum[200005],sa[200005],rank[200005],h[200005],p,q,n;8 intws[200005],wa[200005],wb[200005],wv[200005];9 intRead () {Ten     intt=0, f=1;CharCh=GetChar (); One      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A      while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -     returnt*F; - } the voidsolve () { -     intans=0; -      for(intI=1; i<=n;i++) -Ans+=std::max (0, Std::min (Q,n-sa[i])-std::max (P-1, H[i])); +printf"%d\n", ans);  - } + BOOLcmpint*r,intAintBintl) { A     returnr[a]==r[b]&&r[a+l]==r[b+l]; at } - voidDaint*r,int*sa,intNintm) { -     int*x=wa,*y=wb,*t,i,j,p; -      for(i=0; i<n;i++) x[i]=R[i]; -      for(i=0; i<m;i++) ws[i]=0; -      for(i=0; i<n;i++) ws[x[i]]++; in      for(i=1; i<m;i++) ws[i]+=ws[i-1]; -      for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i; to      for(j=1, p=1;p <n;m=p,j*=2){ +          for(p=0, i=n-j;i<n;i++) y[p++]=i; -          for(i=0; i<n;i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J; the          for(i=0; i<n;i++) wv[i]=X[y[i]]; *          for(i=0; i<m;i++) ws[i]=0; $          for(i=0; i<n;i++) ws[wv[i]]++;Panax Notoginseng          for(i=1; i<m;i++) ws[i]+=ws[i-1]; -          for(i=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i]; the          for(t=x,x=y,y=t,i=1, x[sa[0]]=0, p=1; i<n;i++) +X[SA[I]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; A     } the } + voidCalint*r,intN) { -     inti,j,k=0; $      for(i=1; i<=n;i++) rank[sa[i]]=i; $      for(i=0; i<n;h[rank[i++]]=k) -       for(k?k--:0, j=sa[rank[i]-1];r[i+k]==r[j+k];k++); - } the intMain () { -     intT=read (), tcase=0;Wuyi      while(t--){ thetcase++;p rintf ("Case %d:", Tcase); -scanf"%s", s); WuP=read (); q=read (); -n=strlen (s); About          for(intI=0; i<n;i++) num[i]=S[i]; $num[n]=0; -Da (num,sa,n+1, $); - cal (Num,n); - solve (); A     } +}

Light OJ 1314 Names for babies

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.