[BZOJ2795] [Poi2012] A horrible Poem

Source: Internet
Author: User

2795: [Poi2012]a horrible Poem time limit:50 Sec Memory limit:128 MB
submit:261 solved:150
[Submit] [Status] [Discuss] Description


A string s consisting of lowercase English letters is given, and Q is asked to answer the shortest follow-up link of a substring of s.
If string B is a looping section of string A, then a can be repeated several times by B.

Input

The first line is a positive integer n (n<=500,000), which represents the length of S.
The second row of n lowercase English letters, representing the string s.
The third line is a positive integer q (q<=2,000,000), which indicates the number of queries.
The following q lines are two positive integers a, B (1<=a<=b<=n), representing the query string s[a: b] The shortest cycle section length.

Output

Output Q-line positive integers, and the positive integers of line I correspond to the answer of the I query.

Sample Input8
Aaabcabc
3
1 3
3 8
4 8

Sample Output1
3
5HINT Source

Acknowledgement Jiangzoi&oimaster

[Submit] [Status] [Discuss]


Hash, and then there is an obvious violent practice, a single inquiry is $\sqrt n$.

Can be optimized, record the number of occurrences of each letter, the number of links must be this approximate.

Time complexity is not very good $o (n+q+q\sqrt N) $

1#include <cstdio>2#include <algorithm>3 #defineN 5000504 #definell unsigned long long5 using namespacestd;6InlineintRead ()7 {8     intx=0, f=1;CharCh=GetChar ();9      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}Ten      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} One     returnx*F; A } -ll pow[n]={1},hash[n]; - intn,num[n][ -],q,st[n],top; the CharS[n]; -inline ll Gethash (intLintR) -{returnhash[r]-hash[l-1]*pow[r-l+1];} -InlineBOOLCheckintLintRintx) +{returnGethash (l+x,r) ==gethash (l,r-x);} -InlineintgcdintAintb) {returnB?GCD (b,a%b): A;} +InlineintQueryintLintR) A { at swap (l,r); -     intx=r-l+1, y=r-l+1; -      for(intI=1; i<= -; i++) -X=GCD (x,num[r][i]-num[l-1][i]); -     if(x==1)returny; -top=0; in      for(intI=1; i*i<=x;i++) -     if(! (%i)) to     { +st[++top]=y/i; -         if(Check (l,r,y/x*i)) the         returny/x*i; *     } $      while(!check (L,r,st[top])) top--;Panax Notoginseng     returnSt[top]; - } the intMain () + { Ascanf"%d\n",&n); theGets (s+1); +      for(intI=1; i<=n;i++) -hash[i]=hash[i-1]*233+S[i], $pow[i]=pow[i-1]*233; $      for(intI=1; i<=n;i++) -      for(intj=1; j<= -; j + +) -num[i][j]=num[i-1][j]+ (s[i]-'a'+1==j); theq=read (); -      while(q--) printf ("%d\n", query (read (), read () ));Wuyi}
View Code

[BZOJ2795] [Poi2012] A horrible Poem

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.