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

  • SEARCH
  • Home
  • 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 7438303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:59+00:00 2026-05-29T10:31:59+00:00

I’m currently learning algorithms from a book I have bought from Amazon but it

  • 0

I’m currently learning algorithms from a book I have bought from Amazon but it is the worst book in the world, it shows examples but crucially doesn’t show how to work the answers out.

so the first question I have is,

Prefix-Match(T[1..n], P[1..m]) {
i := 1 // point to current position in T[]
while(i <= n) {
// find a match for first character of P
while( i <= n && T[i] != P[1]) i++
if (i > n) return; // quit
len := 1
// match as much as possible
while(len < m && i+len <= n && T[i+len] == P[1+len]) len++
output i, len
i++
}

what would the output of this program be if T = [a,b,a,b,c,a,b] and P = [a,b,a]???

and secondly how do I work out time complexity of the algorithm, in terms of m and n?

  • 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-05-29T10:31:59+00:00Added an answer on May 29, 2026 at 10:31 am

    Well first of all a question: what book you are talking about? If this is exactly a code sample from it, then it has to be terrible…

    And now for the answer:

    • this function runs through every element of T (this is the first while loop),
    • then it finds the first occurrence of the the first element of P in the range from “i” to the end of the table T. In this case i = 1, because T[1] == P[1] (if T does not contain the first element of P then the function simply returns),
    • then it looks at how many elements in T starting from i match subsequent elements in P (meaning that it will take element T[i+1] and check if it matches P[2] and same with T[i+2] and P[3]),
    • it then prints the i it found and how many matches it found.

    Basically this function finds all the subsets of T that match P. And the exact output should look like this:

    1, 3
    3, 2
    6, 2
    

    Tldr: the first number is the index at which the element P[1] is in the table T and the second number is the number of elements following it equal to its counterparts in table P.

    But I do not know what was the author’s intention – from what I understand the found subset doesn’t have to completely match the P set, i.e. if we took: T = [a,c,a] and P = [a,b,a] we would still get the output 1,3. There is no element that breaks the loop if the subsequent element of the found subset in T is different from its counterpart in P.

    About time complexity there’s plenty of articles on the web. If you want a book then CLRS Introduction to Algorithms or Algorithm Design Manual are my favorits.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.