You are not logged in. Please login at www.codechef.com to post your questions!

×

Can I use lps array of kmp algorithm for string matching using Z algorithm's string concatenation technique?

KMP algorithm finds Longest common prefix which is also a suffix.

So like Z algorithm, if we concatenate pattern and text as pattern + '#' + text (where # is not in character set) and find lps[] of KMP and check how many lps[] elements are equal to pattern size like z algorithm, will we get the answer?

e.g. text = aacabd and pat = ab

concatenated string = ab#aacabd

lps[] = 0 0 0 1 1 0 1 2 0

since one value is 2, it means the pattern is present in the text.

Can anyone tell me if it is wrong for some test case? I am finding it a bit difficult to use KMP after finding LPS and finding it a bit hard to understand Z values.

asked 24 Oct '16, 19:49

dragonemperor's gravatar image

3★dragonemperor
89321134
accept rate: 10%


Your idea is correct. lps[i] is equal to the longest suffix of i-th prefix, which is equal to prefix. And if one of the values of lps is equal to pattern's length it means that in text there is a substring which is equal to pattern, as substring is suffix of one prefixes.

Sorry for my poor English.

link

answered 25 Oct '16, 00:05

akali's gravatar image

2★akali
162
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×66
×41

question asked: 24 Oct '16, 19:49

question was seen: 741 times

last updated: 25 Oct '16, 00:05