2795: [Poi2012]a horrible Poem

Source: Internet
Author: User

2795: [Poi2012]a horrible Poem time limit:50 Sec Memory limit:128 MB
submit:484 solved:235
[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

First we can know, a length of Len's substring, its circulation section must be Len of the approximate, so just find the approximate Len, and then the hash to determine the line. But this complexity is q√n and will be tle, so consider optimization.
Consider if there is a letter K, then the number of cycles of this substring must be the approximate amount of k, we take all the K to a GCD, and then find approximate.

#include <cstdio>#include<algorithm>#include<iostream>#defineP 31#defineR Register#defineull unsigned long Longusing namespacestd;intRead () {Rintx=0;BOOLf=1; RCharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=0; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnf?x:-x;}Const intn=5e5+Ten; ull Hash[n],Base[N];intn,m,vgcd,ans,c[n][ -];;CharS[n];voidGet_hash () {Base[0]=1;  for(intI=1; i<=n;i++) {Hash[i]=hash[i-1]*p+s[i]-'a'; Base[i]=Base[I1]*Q; }}voidCheckintXintYintt) {ull HAS1=hash[y-t]-hash[x-1]*Base[y-x+1-T]; ull HAS2=hash[y]-hash[x+t-1]*Base[y-x+1-T]; if(HAS1==HAS2) ans=min (ans,t);}intMain () {n=read (); Gets (s+1); m=read ();    Get_hash ();  for(intI=0; i<= -; i++){         for(intj=1; j<=n;j++) {C[j][i]=c[j-1][i]+ (s[j]-'a'==i); }    }     for(intI=1, x,y;i<=m;i++) {x=read (); Y=read (); ans=n;vgcd=y-x+1;  for(intj=0; j<= -; j + +) VGCD=__GCD (vgcd,c[y][j]-c[x-1][j]);  for(intj=1; j*j<=vgcd;j++){            if(VGCD%J)Continue; Check (x, Y,-x+1)/j); Check (x, Y,-x+1)/(vgcd/j)); } printf ("%d\n", ans); }    return 0;}

2795: [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.