[BZOJ3670] [Uoj#5] [NOI2014] Zoo

Source: Internet
Author: User
Tags cmath

[BZOJ3670] [Uoj#5] [NOI2014] Zoo

Question 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 principal raised a question: "The KMP algorithm can only find the next array." I'm hoping to find a stronger num array one by one a 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, and the number of such strings is recorded asNum[i]。 For exampleSForAAAAA, youNum[4] = 2。 This is becauseSThe former4A word identifierAAAA, whereaAndAAAll 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. Similarlynum[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 lot 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.

Input Example 1

3 AAAAAABABCABABC

Output Example 1

 $ 1  +

Input Example 2

Portal (click to download)

Output Example 2

Transmission Door

Data size and conventions

n≤5,L≤1000000

Exercises

The solution to this problem suffix array is not difficult to think of, because the search for the num[i] only consider the prefix, so the suffix and the whole string matching, do a range increase on the line. As for the requirement of not overlapping, a little discussion.

80 points:

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL        ine Char Getchar () {if (Head = = tail) {int L = fread (buffer, 1, buffersize, stdin);    Tail = (Head = buffer) + L; } return *head++;}    int read () {int x = 0, f = 1; char c = Getchar ();    while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();}    while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 1000010#define maxlog 21#define MOD 1000000007#define LL long longint N, M, RANK[MAXN], HEIGHT[MAXN], WS[MAXN ], Sa[maxn];char s[maxn];bool CMP (int* A, int p1, int p2, int len) {return A[P1] = = A[p2] && A[p1+len] = = A[p2+le n]; }void Ssort () {int *x = rank, *y = height;m = 0; memset (Ws, 0, sizeof (Ws)), for (int i = 1; I <= n; i++) s[i]-= (' A '-1), ws[x[i] = s[i]]++, M = max (M, (int) s[i]); for (int i = 1 ; I <= m; i++) ws[i] + = ws[i-1];for (int i = n; i; i--) sa[ws[x[i]]--] = i;for (int pos = 0, j = 1; pos < n; j <<= 1, m = pos ) {pos = 0;for (int i = n-j + 1; I <= n; i++) Y[++pos] = i;for (int i = 1; I <= n; i++) if (Sa[i] > J) Y[++pos] = Sa[i]-j;for (int i = 1; I <= m; i++) ws[i] = 0;for (int i = 1; I <= n; i++) ws[x[i]]++;for (int i = 1; I <= m; i++ ) Ws[i] + = ws[i-1];for (int i = n; i; i--) sa[ws[x[y[i]]]--] = Y[i];swap (x, y); pos = 1; X[SA[1]] = 1;for (int i = 2; I <= n; i++) x[sa[i]] = cmp (y, sa[i], sa[i-1], j)? POS: ++pos;} return;} void Calch () {for (int i = 1; I <= n; i++) rank[sa[i]] = i;for (int i = 1, j, k = 0; I <= N; height[rank[i++]] = k) for (k. k--: 0, j = sa[rank[i]-1]; S[i+k] = = S[j+k]; k++); return;} int MINV[MAXLOG][MAXN], log[maxn];void init () {log[1] = 0;for (int i = 2; I <= n; i++) log[i] = log[i>>1] + 1;for (int i = 1; I <= n; i++) Minv[0][i] = height[i];for (int j = 1; (1 << j) <= N; J + +) for (int i = 1; i + (1 << J)-1 <= N; i++) Minv[j][i] = min (Minv[j-1][i], minv[j-1][i+ (1<<j-1)]); return ;} int query (int ql, int qr) {Ql++;int len = qr-ql + 1, t = Log[len];return min (minv[t][ql], minv[t][qr-(1<<t) +1]);} int Addv[maxn];int Main () {int T = read (), while (t--) {n = 0; memset (s, 0, sizeof (s)), char TC = Getchar (); while (!isalpha (TC ) TC = Getchar (), while (Isalpha (TC)) s[++n] = TC, TC = Getchar (); Ssort (); Calch (); Init (); memset (ADDV, 0, sizeof (ADDV)), for (int i = 2; I <= n; i++) {int tmp = query (min (rank[1], rank[i]), Max (rank[1], rank[i]) ) tmp = MIN (tmp, i-1);//printf ("%d", tmp); addv[i]++; addv[i+tmp]--;} Putchar (' \ n '); LL ans = 1; int t = 0;for (int i = 1; I <= n; i++) T + = Addv[i], (ans *= (1ll + t))%= mod;printf ("%lld\n", ans);} return 0;}

The positive solution of this problem is KMP, while maintaining two pointers J, J2, respectively, representing the position of the last mismatch and the position at which the position is less than or equal (I >> 1) mismatch, and the maintenance Num[i] array indicates how many mismatch edges the I forward mismatch will pass through.

100 Points:

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include < algorithm> #include <stack> #include <vector> #include <queue> #include <cstdlib>using namespace Std;int read () {int x = 0, F = 1, char c = GetChar (), while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 1000010#define MOD 1000000007#define LL long longint N, F[MAXN]; LL Num[maxn];char S[maxn];int Main () {int T = read (), while (t--) {scanf ("%s", s+1); n = strlen (s+1); f[1] = f[2] = 1; num[1] = 0;int J = 1, J2 = 1; LL ans = 1;for (int i = 2; I <= n; i++) {while (J > 1 && s[i]! = s[j]) j = f[j];f[i+1] = j + = (s[i] = = S[j]); n Um[i] = Num[f[i]] + 1;while (J2 > 1 && s[i]! = s[j2]) J2 = f[j2];j2 + = (s[i] = = S[j2]); while (J2 > 1 &&amp ; (j2-1 << 1) > i) J2 = F[j2];(ans *= (1ll + num[j2]))%= MOD;} printf ("%lld\n", ans);} return 0;}

[BZOJ3670] [Uoj#5] [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.