Uvalive 3026 Period (KMP algorithm introduction)

Source: Internet
Author: User

KMP's code is short, but it's not easy to understand, let's start by explaining the algorithm process.

Simple string Matching we all know, but the efficiency is not high, why?

The matching process does not take full advantage of the information that has been matched by the template, for example,

I is the subscript for the current character of the text string, and J is the subscript to match the character that the template string is currently matching. (The subscript is zero-based)

When matching to i = 4, j = 4 is mismatched, the naïve match is to move to the right one and then start from J, which is inefficient.

It is not difficult to find that the previous matched string AB is the same as the maximum prefix suffix. Moving the string to the first position of the suffix is exactly

The first match in the naïve matching process can match this prefix to match the position! So j = f[j-1] = 2.

How does this F-array come under the template string?

Finding the maximum prefix suffix process is essentially a match.

I represents the subscript for the current requirement F[i], J indicates the maximum prefix suffix length that the previous string has matched. (j =-1 means position No. No. 0 is not matched)

When i = 6 o'clock, I have already matched J, so as long as I just start from the J subscript position to compare the line. F[i] = j+1 when matched.

If they do not match, they are matched using the previously obtained F value.

When i = 7, j = 3 o'clock, a mismatch occurs, using the previous F-value, you can know that a is the longest prefix and suffix, then only need to match from a, so j = f[j-1],

Repeat the process until the match or J =-1 is reached. Current equals: f[i] = j+1. (j=-1 means no matching is also for the convenience of unified processing)

In the code, because J = f[j-1] is not represented when j = 0, so move the entire F-array to the right. Simply replace J = f[j-1] of the above procedure with j = F[j].

----------------------------------------------Split Line----------------------------------------------------------------

This problem requires a minimum cycle period for prefixes.

First, construct a loop string, and then the process of finding out the mating function f, when the first prefix length equals the suffix length and each half of the time, this prefix must be the smallest loop section. (starting from the first conclusion with the inductive method to prove)

A conclusion can be summed up: at the end of each cycle section i-f[i] = = Shortest cycle section length.

In turn I can be (i-f[i]) divisible can be introduced i-f[i] is the shortest follow-up link. (also with inductive method)

KMP not understand before, only can set. For learning AC automata, KMP is still the foundation.

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6+5;intF[MAXN];CharSTR[MAXN];voidGETF (Char*s) {f[0] = -1;  for(inti =0, j =-1; s[i];) {         while(~j && s[i]! = s[j]) j =F[j]; f[++i] = + +K; }}intMain () {//freopen ("In.txt", "R", stdin);    intN,kas =0;  while(SCANF ("%d\n",&N) {gets (str);        GETF (str); printf ("Test Case #%d\n",++Kas);  for(inti =2; I <= N; i++){            if(F[i] && i% (i-f[i]) = =0) printf ("%d%d\n", i,i/(i-f[i])); } Putchar ('\ n'); }    return 0;}

Uvalive 3026 Period (KMP algorithm introduction)

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.