Question
5
Dynamic Programming. A palindrome is a nonempty s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I believe you need to proceed this way:

Let y1y2 ... yn be your string (where yi are letters).

Create the generalized suffix tree of Sf = y1y2 ... yn$ and Sr = ynyn - 1 ... y1# (reverse the letters and choose different ending characters for Sf ($) and Sr (#))... where Sf stands for "String, Forward" and Sr stands for "String, Reverse".

For every suffix i in Sf, find the lowest common ancestor with the suffix n - i + 1 in Sr.

What runs from the root till this lowest common ancestor is a palindrome, because now the lowest common ancestor represents the longest common prefix of these two suffixes. Recall that:

(1) A prefix of a suffix is a substring.

(2) A palindrome is a string identical to its reverse.

(3) So the longest contained palindrome within a string is exactly the longest common substring of this string and its reverse.

(4) Thus, the longest contained palindrome within a string is exactly the longest common prefix of all pairs of suffixes between a string and its reverse. This is what we're doing here.

EXAMPLE

Let's take the word banana.

Sf = banana$

Sr = ananab#

Below is the generalised suffix tree of Sf and Sr, where the number at the end of each path is the index of the corresponding suffix. There's a small mistake, the a common to all the 3 branches of Blue_4's parent should be on its entering edge, beside n:

B# 4 AB# NA AI ANANAS 4 B# 0 NA AB# B# 0

The lowest interior node in the tree is the longest common substring of this string and its reverse. Looking at all the interior nodes in the tree you will therefore find the longest palindrome.

The longest palindrome is found between between Green_0 and Blue_1 (i.e., banana and anana) and is anana

Add a comment
Know the answer?
Add Answer to:
5 Dynamic Programming. A palindrome is a nonempty string over some alphabet that reads the same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and...

    Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, noon, and aibohphobia (fear of palindromes). You may assume that in the problems below, an input string is given as an array of characters. For example, input string noon is given as an array s[L.4], where s[1] = n, s[2] = o, s[3] = o, and s[4] = n. (a) (15 pts)...

  • C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same...

    C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man,a plan, a canal, Panama Desserts, I stressed Kayak Write a book function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program, which continues to...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • please explain the answer A sequence of characters is called a palindrome if it reads the...

    please explain the answer A sequence of characters is called a palindrome if it reads the same way forward or backward. For example, 59AA95 is a six-character palindrome, and 59A95 is a five-character palindrome. Some other instances of palindromes: U NU, LON NOL, MALAYALAM, NOW ON, PUT UP, TOO HOT TO HOOT, NEVER ODD OR EVEN, ABLE WAS I ERE I SAW ELBA, and POOR DAN IS IN A DROOP. Find the number of nine-character palindromes that can be formed...

  • Write a function palcheck (char word II) which takes a C-style string word (or a C++...

    Write a function palcheck (char word II) which takes a C-style string word (or a C++ string word if you wish) and returns or false depending on whether or not the string is a palindrome. (A palindrome is a string which reads the same backwards and forwards. Some classic example of palindromes are radar, racecar, toot, deed, bib, civic, redder and madam.) Use your function from part (a) in a complete C++ program which, for each small letter 'a' through...

  • Python Question: A palindrome is a string that reads identical forwards and backwards. Examples include abba,...

    Python Question: A palindrome is a string that reads identical forwards and backwards. Examples include abba, abcba, a1b1b1a, houstonnotsuoh etc. Your task is to write a function ispalindrome(string) that reads an input string and returns True if both of the following are true: The string is a palindrome The string has at least one letter (uppercase or lowercase) For example: ispalindrome('racecar') should return True PS: ispalindrome('123321') should return False ispalindrome('racecar') should return True

  • palindrome is a string that reads the same both forward and backward. C++ For example, the...

    palindrome is a string that reads the same both forward and backward. C++ For example, the string "madam" is a palindrome. Write a program that uses a recursive function to check whether a string is a palindrome. Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.

  • TASK Your task is to build a palindrome from an input string. A palindrome is a...

    TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT