For problems that require you to provide an algorithm, you must give the followi
ID: 3724117 • Letter: F
Question
For problems that require you to provide an algorithm, you must give the following:
1. a precise description of the algorithm in English and, if helpful, pseudocode,
2. a proof of correctness,
3. an analysis of running time and space.
1. A palindrome is any string that is exactly the same as its reversal (eg. I, DAD, or RACECAR). (a) Provide an efficient algorithm to find the length of the longest subsequence in a given string that is itself a palindrome. For example, the longest palindrome subsequence of AHLATBARGA is AABAA. So your algorithm should output 5. (b) Describe how to modify the algorithm from part (a) to output the longest palindrome subsequenceExplanation / Answer
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
Algorithm to calculate palindrome and then length:
1. First character is always palindrome so print yes.
2. Start from second character, i.e.,
i = 1
a) Compare ‘firstr’ and ‘second’, they don’t match, so print no.
b) Calculate hash values for next iteration, i.e., i = 2
Since i is odd, ‘firstr’ is not changed and ‘second’ becomes hash(“c”)
i = 2
a) Compare ‘firstr’ and ‘second’, they don’t match, so print no.
b) Calculate hash values for next iteration, i.e., i = 3
Since i is even, ‘firstr’ becomes hash(“ba”) and ‘second’ becomes hash(“cb”)
i = 3
a) Compare ‘first’ and ‘second’, they don’t match, so print no.
b) Calculate hash values for next iteration, i.e., i = 4
Since i is odd, ‘firstr’ is not changed and ‘second’ becomes hash(“ba”)
i = 4
a) ‘firstr’ and ‘second’ match, compare the whole strings, they match, so print yes
b) We don’t need to calculate next hash values as this is last index
3. Now take a counter to count number of characters in the string.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.