Bzoj 3670 [Noi2014] Zoo KMP

Source: Internet
Author: User

Title: Give a string of N (n<=1e6), the multiplicative and modulo 1,000,000,007 of the NUM array, where NUM (i) ={the number of strings s ' in the prefix of length I, where S ' is both the prefix of the prefix and the suffix of the prefix, And the suffix does not overlap with the prefix}

The topic background has been paved, the problem can be solved with KMP.

In calculating the NXT array, we can calculate the CNT array (skipping a few times to 0), indicating how many prefixes can match the suffix contained in the current prefix, for example, Niconiconi in this string, NXT (=6,NXT) (6) =2,nxt (2) = 0, So CNT (10) =3.3 prefixes representing a length of ten (Niconiconi)/6 (Niconi)/2 (NI) can be found with a suffix in the prefix length of 10 to match.

The CNT array is not a num array, because there will be strings that do not satisfy the criteria, so again the match will not satisfy the condition of the removal, that is, to meet the fix (already matched prefix length) < I (the length of the current prefix), at this time the statistical answer can be.

 #include <cstdio> #include <cstring> #define N 1000005 #define MOD 1000000007 using
namespace Std;
typedef long Long LL;
Char S[n];
int nxt[n],cnt[n];
    LL KMP () {memset (nxt,0,sizeof nxt); LL Ans=1;
    Cnt[1]=1;
    int Len=strlen (s+1), fix=0;
        for (int i=2;i<=len;i++) {while (s[i]!=s[fix+1] && fix) Fix=nxt[fix];
        if (s[i]==s[fix+1]) fix++;
        Nxt[i]=fix;
    cnt[i]=cnt[fix]+1;
    } fix=0;
        for (int i=2;i<=len;i++) {while (s[i]!=s[fix+1] && fix) Fix=nxt[fix];
        if (s[i]==s[fix+1]) fix++;
        while (Fix*2>i) Fix=nxt[fix];
        ans*=cnt[fix]+1;
    Ans%=mod;
} return ans;
    } int main () {int T;
    scanf ("%d", &t);
    while (t--) scanf ("%s", s+1), printf ("%lld\n", KMP ());
return 0; }
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.