Bzoj3620 seems to have seen in a dream KMP __KMP

Source: Internet
Author: User
Description"Madoka, don't believe QB. With Homura's disappointment, Madoka signed a contract with QB. It was a nightmare for Modoka, and it was also what happened in the last cycle. In order to make this one Madoka no longer contract with QB, Homura decided to resolve QB on the first day of school. However, QB has a lot of surrogates too (but in the eighth words the plot shows that it could have been reborn indefinitely.) But a firm Homura will not give up-she has decided to destroy everything that may be QB. Now, she has sensed a near state, and turned it into a string of length n for you to learn OI. Now you know from her words that all the strings that resemble a+b+a are QB or its surrogates, and Len (A) >=k,len (B) >=1 (the position is different with the other characters of the same substring of different substrings, the same position but split different substring count the same substring), and then you have to tell Ho as soon as possible Mura the answer--QB and the number of its surrogates. InputThe first line is a string, the second line is a number k OutputOnly one line of ans, indicating the number of QB and its aliases Sample Input"Sample Input 1"
Aaaaa
1
"Sample Input 2"
Abcabcabc
2
Sample Output"Sample Output 1"
6

"Sample Output 2"
8
HINT

For 100% of the data: n<=15000, k<=100, and the character set is all lowercase letters

A: Enumerates the left endpoints and then expands the KMP.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 20000
using namespace Std;
Char S[n],ss[n];
int Ans,k,j,fail[n],len;
int main () {
  scanf ("%s", s+1); scanf ("%d", &k); Len=strlen (s+1)
  ; for (int i=1;i<=len;i++)
    if (len-i+1>k*2) {for
     (int p=i;p<=len;p++) ss[p-i+1]=s[p];
     j=0;
     for (int p=2;p<=len-i+1;p++) {while
       (J&&ss[j+1]!=ss[p]) j=fail[j];
       if (Ss[j+1]==ss[p]) j + +;
       fail[p]=j;
     } 
     j=0;
     for (int p=1;p<=len-i+1;p++) {while
       (J&&ss[j+1]!=ss[p]) j=fail[j];
       if (Ss[j+1]==ss[p]) j + +;
       int jj=j;
       while (jj&& (jj<<1>=p)) JJ=FAIL[JJ];
       ans+=jj>=k; 
     }
    cout<<i<< ' <<ans<<endl;
   }
  cout<<ans<<endl;



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.