3670: [Noi2014] Zoo time limit:10 Sec Memory limit:512 MB
submit:2032 solved:1077
[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 ' 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
Source [Submit] [Status] [Discuss]
HOME back
———————————— I'm a split line ——————————————————
Two times KMP, the second time judgment is greater than the total length of half.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7#include <cstdlib>8#include <iomanip>9#include <cassert>Ten#include <climits> One#include <vector> A#include <list> - #defineMAXN 1000001 - #defineF (i,j,k) for (int i=j;i<=k;i++) the #defineM (A, B) memset (A,b,sizeof (a)) - #defineFF (i,j,k) for (int i=j;i>=k;i--) - #defineINF 0x7fffffff - #defineMAXM 2016 + #defineMoD 1000000007 - //#define LOCAL + using namespacestd; A intRead () { at intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } - CharST[MAXN]; in intLEN,NEXT[MAXN],NUM[MAXN],CNT[MAXN]; - Long Longans; toInlinevoidWork_next () { + inti,t; - for(i=1; i<=len;++i) { thet=next[i-1]; * while(t!=-1&&st[i]!=st[t+1]) $t=Next[t];Panax Notoginsengnext[i]=t+1; -cnt[i]=cnt[t+1]+1; the } + } AInlinevoidWork_num () { the inti,t; + for(i=1; i<=len;++i) { -t=num[i-1]; $ if(t*2+2>i) t=Next[t]; $ while(t!=-1&&st[i]!=st[t+1]) -t=Next[t]; -num[i]=t+1; the } - } Wuyi intMain () { the intt,i; -Cin>>T; Wu while(t--){ -cin>>st+1; AboutLen=strlen (st+1); $next[0]=num[0]=-1; - Work_next (); - work_num (); -ans=1; A for(i=1; i<=len;++i) { +Ans*= (cnt[num[i]]+1); theans%=MoD; - } $cout<<ans<<Endl; the } the return 0; the}
Bzoj 3670
Bzoj 3670 [NOI2014] Zoo