BZOJ3670: [Noi2014] Zoo

Source: Internet
Author: User

Description

Recently, the director found that the zoo lazy more and more animals. Penguins, for example, will only sell to tourists for food. In order to cure the bad atmosphere of the zoo, let the animals with their own knowledge to the visitors to eat, the director decided to set up an algorithm class, let the animals learn the algorithm.

One day, the principal explained the KMP algorithm to the animals .

Director: "For a string S, its length is L." We can find an array named next in the time of O (L) . Does anyone preview the meaning of the next array? "

Panda: "For a substring of the first I character of the string s, it is both its suffix and its prefix string (except itself), the longest length is recorded as next[i]." "

Principal: "Very good!" So can you give me an example? ”

Panda: "Case S is abcababc, then next[5]=2." Since the first 5 characters of S are abcab,AB is both its suffix and its prefix, and cannot find a longer string to satisfy this property. Similarly, it can be concluded that next[1] = next[2] = next[3] = 0,next[4] = next[6] = 1,next[7] = 2,next[8] = 3. "

The director praised the careful preview of the panda classmate. He then explained in detail how to find the next array in the time of O (L) .

before class, the director raised a question: "next array. I now wish to find a more powerful num array one by one for the substring of the first I character of the string s, both its suffix and its prefix, and the suffix does not overlap with the prefix. The number of such strings is recorded as num[i]. For example s is aaaaa , then num[4] = 2. This is because s's former 4 characters aaaa , where a and aa satisfies the nature ' Both suffix and prefix ', while ensuring that this suffix does not overlap with this prefix. and aaa while satisfying the nature ' Both suffix and prefix ', but unfortunately this suffix overlaps with this prefix, so it cannot be counted. Similarly, num[1] = 0,num[2] = num[3] = 1,num[5] = 2. "

Finally, the Director gave the reward conditions, the first to do the right students to reward chocolate box. After listening to this sentence, the penguin who slept in a class immediately woke up! But penguins do not do this problem, so you ask for help to visit the zoo. Can you help penguins write a program to find the num array?

In particular, in order to avoid a large amount of output, you do not need to output num[i] respectively, you only need to output the results of 1,000,000,007 modulo .

Input

Line 1th contains only a positive integer n, which represents the number of groups of test data. Then n rows, each row describes a set of test data. Each set of test data contains only one string the definition of s,s is described in the topic description. The data guarantees that s contain only lowercase letters. The input file does not contain extra empty lines, and there are no extra spaces at the end of the line.

Output

Contains n rows, each of which describes the answer to a set of test data, and the order of the answers should be consistent with the order in which the data is entered. For each set of test data, you only need to output an integer that represents the result of the answer of this set of test data to 1,000,000,007 modulo. The output file should not contain extra empty lines.

Sample Input3
Aaaaa
Ab
ABCABABCSample Output36
1
+HINT

N≤5,l≤1,000,000

The next[i in the KMP algorithm] is connected to I with an edge to get the KMP tree. So Num[i] is actually I along the KMP tree up the first length <=I/2 the node on the KMP tree depth. Then we can maintain the node in the KMP.
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=1000010;Const intMod=1000000007;CharS[MAXN];intF[MAXN],DEP[MAXN];intMain () {Dwn (T,read (),1) {scanf ("%s", s);intn=strlen (s); f[1]=f[0]=0;d ep[1]=1;intj=0, j2=0; Long Longans=1; Rep (I,1, N-1) {             while(J&&s[i]!=s[j]) j=F[j]; if(S[i]==s[j]) j++;f[i+1]=j;dep[i+1]=dep[j]+1;  while(J2&&s[i]!=s[j2]) j2=F[J2]; if(S[i]==s[j2]) j2++;  while(j2>i+1>>1) j2=F[J2]; (Ans*=dep[j2]+1)%=MoD; } printf ("%lld\n", ans); }    return 0;}
View Code

BZOJ3670: [Noi2014] Zoo

Related Keywords:

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.