Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:56:40+00:00 2026-06-15T20:56:40+00:00

The main function that builds the failure/prefix table in KMP as I have seen

  • 0

The main function that builds the failure/prefix table in KMP as I have seen (in all online resources even in this answer_ is as follows:

int j = 0;  
    for (int i = 1; i < pattern.length(); i++) {  
      while (j > 0 && pattern.charAt(j) != pattern.charAt(i)) {   
        j = failure[j - 1];  
      }  
      if (pattern.charAt(j) == pattern.charAt(i)) {   
        j++;   
      }  
      failure[i] = j;  
    }

I can’t understand this part:
j = failure[j - 1];

Why don’t we do j-- instead to go back the string? How do we know that it is correct to update j using the failed table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:56:42+00:00Added an answer on June 15, 2026 at 8:56 pm

    KMP string matching is correct if the i’th entry of the failure table is the length of the longest suffix-prefix match of the prefix of the pattern of length i.

    Notice that if A[0..k] is a suffix-prefix match of A[0..i], then A[0..k] is either the longest suffix-prefix match of A[0..i] or it’s a suffix-prefix match of said longest suffix-prefix match.

    When you put these two things together, it turns out that we want failure[i] to be the length of the longest suffix-prefix match of pattern[0..i]. KMP preprocessing builds failure inductively, using the following fact:

    If the longest suffix-prefix match of A[0..i] is nonempty, chopping off the last character will give some suffix-prefix match of A[0..i-1].

    Consequently, the longest suffix-prefix match of A[0..i] is either empty or it’s formed by extending the longest suffix-prefix match of A[0..i-1] by one character or by extending a suffix-prefix match of that longest suffix-prefix match by one character. The failure function for the preceding characters gives you a straightforward way to iterate over all suffix-prefix matches of pattern[0..i-1].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery function that replaces thumbnails with the main image. This is
In Winforms you just have the Main function and that's what runs first, but
I have a main function in javascript that is function a() { some code
Suppose that in the main I have a function a : a(c); and c
I have a main thread that invokes a child thread function at different times
I have a function that builds and returns an XML document. I then want
I search for the function that run programm by path, and working of main
Haskell 98 specification says that the entry point of a program, namely, function main
Possible Duplicate: Can main function call itself in C++? I found this problem very
I have main.cpp (including main function) and func1.cpp, and I want to link these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.