Bzoj_3670_[noi2014]_ Zoo (KMP)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=3670

For a string, find the num array, where num[i] represents the number of pairs in the same prefix that are not coincident in the string that consists of the first I character.

Analysis

When doing KMP self-match, by the way record a CNT array, where cnt[i] indicates that the I-character needs a few i=fail[i] to reach 0, that is, if the mismatch to jump to the I position, including I position, before the total number can be used as a mismatch jump object ( That is, how much satisfies the same prefix as a suffix). When a sub-match is made for the second time, the mismatch pointer jumps to a position of less than or equal to I/2 J, at which point Cnt[j] is the same prefix suffix group with the same number of satisfied num[i]. (Hand draw more clearly?)

P.s. If the I position jumps to K at the second match, then any number k after K ' is satisfied with K ' >i/2, then at I+1, K ' +1> (i+1)/2, so if you jump to K at position I, continue from i+1 position in k+1 position.

#include <cstdio>Const intmod=1e9+7;Const intmaxn=1e6+5;intans;CharC[MAXN];intFAIL[MAXN],CNT[MAXN];voidKMP () {intj=0; fail[1]=0, cnt[1]=1;  for(intI=2; c[i];i++){         while(j&&c[j+1]!=c[i]) j=Fail[j]; if(c[i]==c[j+1]) J + +; Fail[i]=J; Cnt[i]=cnt[j]+1; } J=0; ans=1;  for(intI=2; c[i];i++){         while(j&&c[j+1]!=c[i]) j=Fail[j]; if(c[i]==c[j+1]) J + +;  while((j<<1) >i) j=Fail[j]; Ans=((Long Long) ans* (Long Long) (cnt[j]+1))%MoD; }}intMain () {intN; scanf ("%d",&N);  while(n--) {scanf ("%s", c+1);        KMP (); printf ("%d\n", ans); }    return 0;}
View Code

Bzoj_3670_[noi2014]_ Zoo (KMP)

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.