[Konjac konjac cultivation Plan] [bzoj3670] [NOI2014] Zoo

Source: Internet
Author: User

Description

It is known that a string s, a substring of 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 as Num[i] .

Request MoD 10^9+7.

Input

Line 1 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 s, The definition of S is described in the topic. 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 representing the results of this set of test data for the answer MoD 10^9+7 .

Sample Input

3
Aaaaa
Ab
Abcababc

Sample Output

36
1
32

Solution

Test instructions is to give you a string s, for each prefix of this string s s[1...i] next[j] satisfies the number of J*2<=i Num[i], num[i].

So, first of all, consider the simplified version of the problem, in the absence of j*2<=i restrictions,num[i]=num[next[i]]+1, the case of num[] recorded as cnt[] .

Now the problem becomes, how to use O (n^2) algorithm to satisfy the condition of J.

We can do it again with KMP 's thought, and when we find that J is not satisfied with the condition, we can use next[] to look forward to the Jthat satisfies the condition.

In this case, each time from the j*2<=i-1 to meet the J began to look for, to avoid some repetition of the time to find the complexity, so a .

1#include <Set>2#include <cmath>3#include <ctime>4#include <queue>5#include <stack>6#include <cstdio>7#include <vector>8#include <string>9#include <cstring>Ten#include <cstdlib> One#include <iostream> A#include <algorithm> - #defineN 1000002 - #defineM 1000000007 the using namespacestd; - intt[n],cnt[n],next[n],n,m; - Long Longans; - CharA[n]; +InlinevoidGet_next () { -      for(intI=2, j=0; i<=m;i++){ +          while(j&&a[i]!=a[j+1]) j=Next[j]; Aj+= (a[i]==a[j+1]); atnext[i]=J; -cnt[i]=cnt[j]+1; -     } - } -Inlinevoidinit () { -scanf"%d", &n); cnt[1]=1; in      for(intL=1; l<=n;l++){ -scanf"%s", A +1); toM=strlen (A +1); +Fill (next+1, next+1+m,0); -Get_next (); ans=1; the          for(intI=2, j=0; i<=m;i++){ *              while(j&&a[i]!=a[j+1]) j=Next[j]; $             if(a[i]==a[j+1]) J + +;Panax Notoginseng              while(j*2>i) j=Next[j]; -Ans=ans* (cnt[j]+1)%M; the         } +printf"%lld\n", ans); A     } the      + } - intMain () { $Freopen ("zoo.in","R", stdin); $Freopen ("Zoo.out","W", stdout); - init (); - fclose (stdin); the fclose (stdout); -     return 0;Wuyi}

[Konjac konjac cultivation Plan] [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.