P2375 Zoo, p2375 Zoo
Description
Recently, the Director found that there are more and more animals in the zoo. For example, penguins only sell cute food to tourists. The Director of the park decided to set up an algorithm class for animals to learn algorithms in order to improve the bad atmosphere of the zoo and let the animals learn what they want from their own talents.
One day, the Director explained the KMP algorithm to the animals.
Length: "For a string S, its length is L. We can find an array named next in the O (L) time. Has anyone previewed the meaning of the next array ?"
Pandatv: "For the substring consisting of the first I character of string S, it is not only its suffix but also its prefix (except it ), the longest length is recorded as next [I]."
Director: "Very good! Can you give an example ?"
Pandatv: "If S is abcababc, next [5] = 2. Because the first five characters of S are abcab, AB is both its suffix and its prefix, and cannot find a longer string to satisfy this nature. Similarly, next [1] = next [2] = next [3] = 0, next [4] = next [6] = 1, next [7] = 2, next [8] = 3."
The Director praised the students who carefully prepared the course. Later, he explained in detail how to find the next array in O (L) time.
Before class, the Director raised a question: "The KMP algorithm can only find the next array. I want to find a more powerful num array. a substring consisting of the first I character of string S is both its suffix and its prefix, the suffix does not overlap with the prefix. The number of such strings is recorded as num [I]. For example, if S is aaaaa, num [4] = 2. This is because the first four characters of S are aaaa. Both a and aa meet the requirements of both the suffix and prefix, and ensure that the suffix does not overlap with the prefix. Although aaa satisfies the nature of 'both suffix and prefix', it is a pity that this suffix overlaps with this prefix, so it cannot be included. Similarly, num [1] = 0, num [2] = num [3] = 1, num [5] = 2 ."
Finally, the Director gave the reward conditions. The first right student rewarded a box of chocolate. After hearing this sentence, the penguin who slept in a class immediately woke up! But penguins won't do this, so they ask for help from visiting the zoo. Can you help penguins write a program to find the num array?
In particular, to avoid a large amount of output, you do not need to output the num [I] separately. You only need to output the product of all num [I, perform the modulo operation on 1,000,000,007.
Input/Output Format
Input Format:
The row 1st contains only one positive integer n, indicating the number of test data groups. Next n rows, each row describes a group of test data. Each group of test data only contains one string, and the definition of S can be found in the topic description. Data guarantee S only contains lowercase letters. The input file does not contain redundant empty rows, and there is no extra space at the end of the row.
Output Format:
Contains n rows. Each row describes the answers to a set of test data. The answer sequence should be consistent with that of the input data. For each group of test data, only one integer needs to be output, indicating the result of Modulo 1,000,000,007 for the answers to this group of test data. The output file should not contain redundant empty rows.
Input and Output sample
Input example #1:
3aaaaaababcababc
Output sample #1:
36132
Description
Test Point Number Convention
1 N ≤ 5, L ≤ 50
2 N ≤ 5, L ≤ 200
3 N ≤ 5, L ≤ 200
4 N ≤ 5, L ≤ 10,000
5 N ≤ 5, L ≤ 10,000
6 N ≤ 5, L ≤ 100,000
7 N ≤ 5, L ≤ 200,000
8 N ≤ 5, L ≤ 500,000
9 N ≤ 5, L ≤ 1,000,000
10 N ≤ 5, L ≤ 1,000,000
The KMP of this question does not seem to be feasible if it starts from 0.
The specific method is to push the num array while pushing the p array.
Num [I] = num [p [j] + 1
Then, repeat it to find the matching j.
Then num [I] = num [j]?
Well, it's really hard to understand ...........
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define lli long long int 6 using namespace std; 7 const int MAXN=1000001; 8 const int mod=1000000007; 9 int n;10 int p[MAXN];11 char s[MAXN];12 lli num[MAXN];13 lli ans=1; 14 int l;15 void makep()16 {17 18 int j=0;19 p[0]=0;20 num[0]=1;21 for(int i=1;i<l;i++)22 {23 while(j>0&&s[j]!=s[i])24 j=p[j-1];25 //if(s[j]==s[i])26 j++;27 p[i]=j;28 num[i]=num[j]+1;29 }30 }31 void find_ans()32 {33 int i=0,j=0;34 for(int i=1;i<l;i++)35 {36 while(s[i]!=s[j]&&j>0)37 j=p[j-1];38 //if(s[i]==s[j])39 j++;40 while((j<<1)>(i))41 j=p[j-1];42 ans=(ans*(num[j]+1))%mod;43 }44 }45 int main()46 {47 //freopen("zoo.in","r",stdin);48 //freopen("zoo.out","w",stdout);49 int n;50 cin>>n;51 while(n--)52 {53 //int ans=1;54 ans=1;55 memset(num,0,sizeof(num));56 memset(p,0,sizeof(p));57 scanf("%s",s);58 l=strlen(s);59 makep();60 find_ans();61 //for(int i=0;i<strlen(s);i++)62 //ans=ans*(p[i]+1);63 printf("%d\n",ans);64 }65 66 return 0;67 }