Bzoj 3670 noi2014 zoo KMP Algorithm

Source: Internet
Author: User

Question:

Given a string (L <= 100 W) with a length of L, evaluate an num array. Num [I] indicates the number of string s' In the prefix with the length of I, S is both the prefix and the suffix of the prefix, and | s '| * 2 <= I

Evaluate round (Num [I] + 1) % 1000000007

This is a deformation of the KMP algorithm... First, find the next array. By the way, find the CNT array, which indicates that after the I prefix is fixed = next [Fix] several times, it will get 0 and then re-match it, note that when fix * 2> I, make fix = next [Fix ].

Do not use the num array instead of next.

Do not forget to enable long

This question has passed ....

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 1000100using namespace std;typedef long long ll;ll ans;char s[M];int next[M],cnt[M];void KMP(){    int i,fix=0;    cnt[1]=1;    for(i=2;s[i];i++)    {        while( fix && s[fix+1]!=s[i] )  fix=next[fix];        if(s[fix+1]==s[i]) fix++;        next[i]=fix;        cnt[i]=cnt[fix]+1;    }    fix=0;ans=1;    for(i=2;s[i];i++)    {        while( fix && s[fix+1]!=s[i] )  fix=next[fix];        if(s[fix+1]==s[i]) fix++;        while( (fix<<1) > i ) fix=next[fix];        ans*=cnt[fix]+1;        ans%=1000000007;    }}int main(){    int T;         //freopen("zoo.in","r",stdin);    //freopen("zoo.out","w",stdout);         for(cin>>T;T;T--)    {        scanf("%s",s+1);        KMP();        cout<<ans<<endl;    }     }


Bzoj 3670 noi2014 zoo KMP Algorithm

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.