[NOI 2014] Zoo: KMP algorithm, recursive

Source: Internet
Author: User

Again the wrong question ... This problem does not change. Fortunately, it's not too far. I decided to add the "test instructions brief" to the later writing.

Test instructions: Give a string s, length L not more than 1 million, remember all is both the substring s[0..i] prefix, but also suffix, and the prefix, the number of non-overlapping strings is num[i], num[0], num[1], ..., num[l-1] The result of the modulus of a given large prime number. Multiple sets of data.

The background is given in the surface: KMP algorithm. So I took "quantity" as the "maximum length".

In the "Noi Guide" training course to listen to the teacher said, but did not understand ... and took it out and thought about it.

If the "prefix, suffix does not overlap" is not considered and is not equal to the entire substring, how to find the number of such strings. When you run the KMP, you push an array of CNT, which indicates how many successful matches you've got from I to the next array, that's what you asked for. Because each jump is equivalent to finding a prefix = suffix; cnt[0] = 0.

If we add this restriction.

Run the KMP first, and find the next and the CNT. Run again, just add j*2 >= i in the condition of the jump, and update the answer after each stop. Note that CNT does not count S[0..J], so this is +1.

Aside from this problem, Next[i] is defined as a true prefix equal to the maximum length of the suffix a little awkward ... After jumping out of the while loop, j=0 can either represent a successful match to s[0] or it may mean that there is no match. Define Next[i] as the position of the next comparison after mismatch at I, and place next[0] =-1 may be better.

#include <cstdio> #include <cstring> #include <cassert> using namespace std;
typedef long Long LL;
const int max_l = 1e6, M = 1e9+7;

Char s[max_l+1];

    int solve () {static int next[max_l], cnt[max_l];//cnt[i] ~ substring S[0..I] How many true prefixes are equal to the suffix int L = strlen (s);
        for (int i = 1, j = 0; i < L; ++i) {while (J && s[i]! = s[j]) j = next[j-1];
            if (s[i] = = S[j]) {cnt[i] = Cnt[j] + 1;
        ++j;
        } else Cnt[i] = 0;
    Next[i] = j;

    } ll ans = 1; for (int i = 1, j = 0; i < L; ++i) {while (J && (s[i]! = S[j] | | j*2 >= i)) j = next[j-
        1];
            if (s[i] = = S[j] && j*2 < i) {ans = (ans * (cnt[j]+2))% M;
        ++j;
}} return ans;
    } int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%s", s);
    printf ("%d\n", Solve ());
} return 0; }

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.