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 8959527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:27:45+00:00 2026-06-15T15:27:45+00:00

I am reading about KMP for string matching. It needs a preprocessing of the

  • 0

I am reading about KMP for string matching.
It needs a preprocessing of the pattern by building a prefix table.
For example for the string ababaca the prefix table is: P = [0, 0, 1, 2, 3, 0, 1]
But I am not clear on what does the numbers show. I read that it helps to find matches of the pattern when it shifts but I can not connect this info with the numbers in the 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-15T15:27:46+00:00Added an answer on June 15, 2026 at 3:27 pm

    Every number belongs to corresponding prefix (“a”, “ab”, “aba”, …) and for each prefix it represents length of longest suffix of this string that matches prefix. We do not count whole string as suffix or prefix here, it is called self-suffix and self-prefix (at least in Russian, not sure about English terms).

    So we have string “ababaca”. Let’s look at it. KMP computes Prefix Function for every non-empty prefix. Let’s define s[i] as the string, p[i] as the Prefix function. prefix and suffix may overlap.

    +---+----------+-------+------------------------+
    | i |  s[0:i]  | p[i]  | Matching Prefix/Suffix |
    +---+----------+-------+------------------------+
    | 0 | a        |     0 |                        |
    | 1 | ab       |     0 |                        |
    | 2 | aba      |     1 | a                      |
    | 3 | abab     |     2 | ab                     |
    | 4 | ababa    |     3 | aba                    |
    | 5 | ababac   |     0 |                        |
    | 6 | ababaca  |     1 | a                      |
    |   |          |       |                        |
    +---+----------+-------+------------------------+
    

    Simple C++ code that computes Prefix function of string S:

    vector<int> prefixFunction(string s) {
        vector<int> p(s.size());
        int j = 0;
        for (int i = 1; i < (int)s.size(); i++) {
            while (j > 0 && s[j] != s[i])
                j = p[j-1];
    
            if (s[j] == s[i])
                j++;
            p[i] = j;
        }   
        return p;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
In reading about SCM (software configuration mgt, not supply chain mgt), I have seen
Reading about database efficiency I've stated that table indexes imposes extra costs to insert/update/delete
When reading about SQL I've come across the term 'slot'. For example, in a
Reading about and using the Amazon Web Services, I'm not really able to grasp
while reading about declare construct from php manual i tried the following example using
After reading about how file based PHP sessions are not the greatest for performance,
Been reading about map/reduce here and on other sites and I'm still not sure
While reading about tuning SQL queries, I read somewhere that 'Always use table alias

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.