bzoj3670: [Noi2014] Zoo

Source: Internet
Author: User

Understanding the wrong sample and test instructions the Qaq ... Always do not want to write KMP finally still have to write Ah ...

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (i,s,t) for (int i=s;i<=t;i++) #define CLR (x,c) memset (x,c,sizeof (x)) #define LL long Longconst int nmax=1e6+ 5;const int Mod=1e9+7;char s[nmax];int next[nmax],num[nmax];void kmp () {    int fail=0,len=strlen (s+1); num[1]=1;    Rep (I,2,len) {        while (Fail&&s[fail+1]!=s[i]) fail=next[fail];        if (s[fail+1]==s[i]) fail++;        next[i]=fail;num[i]=num[fail]+1;    }    Fail=0;ll Ans=1;    Rep (I,2,len) {        while (Fail&&s[fail+1]!=s[i]) fail=next[fail];        if (s[fail+1]==s[i]) fail++;        if ((fail<<1) >i) Fail=next[fail];        Ans= (ans* (num[fail]+1))%mod;    }    printf ("%lld\n", ans);} int main () {    int t;scanf ("%d", &t);    while (t--) {        scanf ("%s", s+1);        KMP ();    }    

  

3670: [Noi2014] Zoo time limit:10 Sec Memory limit:512 MB
submit:2030 solved:1076
[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]

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.