bzoj3670 "NOI2014" zoo

Source: Internet
Author: User

3670: [Noi2014] Zoo time limit: ten Sec Memory Limit: MB
Submit: 1744 Solved: 925
[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 to the animals KMP algorithm.

Principal: "For a string S , it's 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 Strings S the former I a substring of characters, which is both its suffix and its prefix string (except for itself), and 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." Because 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, can be obtained 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: "TheKMP algorithm can only find the next array." I'm hoping to find a stronger num now .The substring of array one by one for the first I character of the string s is both its suffix and its prefix, and the suffix does not overlap with the prefix, and the number of such strings is recorded asNum[i]. For exampleSto beAAAAA, youNum[4] = 2. This is becauseSthe former4characters areAAAA, whereaand theAAall satisfies the nature ' both suffix and prefix ', while guaranteeing that this suffix does not overlap with this prefix. andAAAAlthough the satisfying nature is ' both suffix and prefix ', 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 large amounts of output, you do not need to output Num[i] The difference is how much you just need to output the 1,000,000,007 The results can be obtained by the model.

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




KMP Messy

Nxt[i] Represents a mismatch edge, Pos[i] represents the longest prefix suffix that meets the criteria, Num[i] represents the count of Nxt[i].




#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long long#define MAXN 1000005#define mod 1000000007using namespace Std;int T,N,POS[MAXN],NXT[MAXN],NUM[MAXN]; char s[maxn];inline void Solve () {int j=0;num[1]=1; F (I,2,n) {while (J&&s[j+1]!=s[i]) j=nxt[j];if (s[j+1]==s[i]) j++;nxt[i]=j;num[i]=num[j]+1;} F (i,2,n) {if (J>=I/2) J=pos[i-1];while (J&&s[j+1]!=s[i]) j=nxt[j];if (S[j+1]==s[i]) j++;while (J>I/2) j= NXT[J];p os[i]=j;}} int main () {scanf ("%d", &t), while (t--) {scanf ("%s", s+1), N=strlen (s+1); solve (); ll Ans=1; F (i,1,n) ans=ans* (num[pos[i]]+1)%mod;printf ("%lld\n", ans);}}


bzoj3670 "NOI2014" zoo

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.