This article is copyright ljh2000 and Blog Park is shared, welcome reprint, but must retain this statement, and give the original link, thank you for your cooperation.
This article ljh2000
Author Blog: http://www.cnblogs.com/ljh2000-jump/
Reprint please specify the source, infringement must investigate, retain the final interpretation right!
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 principal raised a question: "KMP algorithm can only find 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
+HINT
N≤5,l≤1,000,000
Positive solution: KMP
Problem Solving Report:
I feel that my understanding of $kmp$ is not deep enough ...
The first $next$ array in $kmp$ represents the maximum length that makes the prefix and suffix equal.
The topic requires equal number of prefix suffixes, $n $ has millions, can only consider linear procedure. So is it possible to get it done in $kmp$?
Consider if $next[i]=j$, then obviously $i$ contribution is $j$ contribution $+1$. But there is a limitation in the topic, prefixes, suffixes can not overlap, that is to say $j*2<=i$.
In this case, we seem to get an idea: when the $j*2>i$ will continue to jump $next$, until meet $j*2<=i$, then obviously can guarantee $ch[1]$~ $ch [j]$ equals $ch[i-j+1]$~ $ch [i]$, Then you can call $cnt[i]=cnt[j]+1$ again without any problems.
It is made by Ljh2000#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio > #include <cmath> #include <algorithm>using namespace std;typedef long long ll;const int MAXN = 1000011;con St int MOD = 1000000007;int N,len,next[maxn];char CH[MAXN]; LL ans,cnt[maxn];inline int getint () {int w=0,q=0; char C=getchar (); while (c< ' 0 ' | | C> ' 9 ') && c!= '-') C=getchar (); if (c== '-') Q=1,c=getchar (); while (c>= ' 0 ' &&c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q?-w:w;} inline void work () {n=getint (); for (int o=1;o<=n;o++) {scanf ("%s", ch+1); Len=strlen (ch+1); next[1]=0; cnt[1]=1; Ans=1 ; int j=0;for (int i=2;i<=len;i++) {while (J && Ch[i]!=ch[j+1]) j=next[j];if (ch[i]==ch[j+1]) j + +; next[i]=j;cnt [I]=cnt[j]+1; }j=0;for (int i=2;i<=len;i++) {while (J && Ch[i]!=ch[j+1]) j=next[j];if (ch[i]==ch[j+1]) j++;while ((j< <1) >i) j=next[j];ans*=cnt[j]+1; Ans%=mod;} printf ("%lld\n", ans);}} int main () {work (); RetuRN 0;}
BZOJ3670 [Noi2014] Zoo