/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q7E A subsequence is palindromic if ... [FREE SOLUTION] | 91影视

91影视

A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence

A,C,G,T,G,T,C,A,A,A,A,T,C,G

has many palindromic subsequences, including A,C,G,C,Aand A,A,A,A(on the other hand, the subsequence A,C,Tis not palindromic). Devise an algorithm that takes a sequence X[1...n]and returns the (length of the) longest palindromic subsequence. Its running time should be0(n2).

Short Answer

Expert verified

We say a stringxi....jis a palindrome ifxi=xjorxi+1=xj-1

Since we need to find the Longest Palindromic Subsequence, for this we will be using dynamic programming approach.

Step by step solution

01

Approach

Here we will first define our sub-problem which would serve as recursive relation, and then we would be calling the sub-problem recursively.

In this question, following two possibilities are:

  • If the first and last character of a string are same, include the first and last character in the palindrome and then in same corresponding order, recursively check for other remaining character.

For instance, if a string isxi....j, then checkxi=xj,xi+1=xj-1and so on.

  • If the first character of the string not matches with the last character, then go back to the values we got from by:
  • Either removing first character from xi....j.
  • Or removing the last character from xi....j.
02

Recursive Relation

S(i,j)=1;ifi=j2+S(i+1,j-2);ifi<jandx[i]=x[j]max{Si,j-1,Si+1,j};otherwise

Where Si,jdefine the length of the palindrome.

03

Implementation of Algorithm

fori=2ton+1Si,i-1=0fori=1ton-1Si,i=1form=1ton-1fori=nton-mj=i+mifxi=xjSi,j=2+Si+1,j-1elseSi,j=maxSi,j-1,Si+1,jreturnS1,n

This solution will return us the longest palindrome in the string xi....j.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91影视!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Cutting cloth. You are given a rectangular piece of cloth with dimensions XY, whereX and Yare positive integers, and a list of products that can be made using the cloth. For each producti[1,n] you know that a rectangle of cloth of dimensionsaibi is needed and that the final selling price of the product is ci. Assume the,ai biandci are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an algorithm that determines the best return on theXY piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired.

Given two strings x=x1x2xnand y=y1y2ym, we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices i1<i2<<ikand j1<j2<<jkwith xi1xi2xik=yj1yj2yjk. Show how to do this in time 0(mn).

Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1,a2,...,an; a positive integer t. Question: Does some subset of the ai鈥檚 add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form 鈥渄oes a subset of{a1,a2,...,ai} add up to ?鈥)

You are going on a long trip. You start on the road at mile post 0. Along the way there aren hotels, at mile posts a1<a2<...<an , where eachai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. You鈥檇 ideally like to travel miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (200x)2. You want to plan your trip so as to minimize the total penalty- that is, the sum, over all travel days, of the daily penalties.Give an efficient algorithm that determines the optimal sequence of hotels at which to stop

You are given a string of n characters s[1...n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like 鈥渋twasthebestoftimes...鈥). You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(.): for any string w,

dict(w)={trueifwisavalidwordfalseotherwise

Give a dynamic programming algorithm that determines whether the string s[.]can be reconstituted as a sequence of valid words. The running time should be at mostO(n2) , assuming calls to dict take unit time.

In the event that the string is valid, make your algorithm output the corresponding sequence of words.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.