3670: [Noi2014] Zoo time limit:10 Sec Memory limit:512 MB
submit:2080 solved:1107
[Submit] [Status] [Discuss] 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 of ' both suffix and prefix ', while guaranteeing that this suffix does not overlap with this prefix. While aaa satisfies 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
+" The Puzzle"first KMP to find the P array, by the way the NUM array to record the current number of matching methods, and then do a similar to the kmp of things, the specific look at the code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <ctime>6#include <cmath>7#include <algorithm>8 using namespacestd;9 #defineMoD 1000000007TentypedefLong Longll; Onell t,n,ans,p[1000100],num[1000100]; A Charch[1000100]; - voidKMP () - { thenum[1]=1; - for(intI=2, j=0; i<=n;i++) - { - while(j&&ch[i]!=ch[j+1]) j=P[j]; + if(ch[i]==ch[j+1]) J + +; -p[i]=J; +num[i]=num[j]+1; A } at for(intI=2, j=0; i<=n;i++) - { - while(j&&ch[i]!=ch[j+1]) j=P[j]; - if(ch[i]==ch[j+1]) J + +; - while(j*2>i) J=p[j];//if they overlap, look forward. -Ans=ans* (num[j]+1)%MoD; in } - } to intMain () + { -Freopen ("zoo.in","R", stdin); theFreopen ("Zoo.out","W", stdout); *scanf"%lld",&T); $ while(t--)Panax Notoginseng { -scanf"%s", ch+1); theans=1; N=strlen (ch+1); KMP (); +printf"%lld\n", ans%MoD); A } the return 0; +}
"bzoj3670" [Noi2014] Zoo