Ex 7
Ex 7
Ex 7
Assignment 7
1. Given two sequences X = ⟨x1,...,xm⟩ and Y = ⟨y1,...,yn⟩, the longest common sub-sequence problem
(LCS) seeks to find a maximum length common sub-sequence of X and Y .
For example, if X = ⟨A,B,C,B,D,A,B⟩ and Y = ⟨B,D,C,A,B,A⟩, then the sequences ⟨B,C,B,A⟩ and
⟨B,D,A,B⟩ are the longest common sub-sequences, since X and Y have no common sub-sequence
of length 5 or greater.
2. In Domino Solitaire, you have a grid with two rows and many columns.Each square in the grid
contains an integer. You are given a supply of rectangular 2 × 1 tiles, each of which exactly covers
two adjacent squares of the grid. You have to place tiles to cover all the squares in the grid such
that each tile covers two squares and no pair of tiles overlap. The score for a tile is the difference
between the bigger and the smaller number that are covered by the tile. The aim of the game is to
maximize the sum of the scores of all the tiles. Here is an example of a grid, along with two different
tilings and their scores.
1|Page
Ex no:7 UCS2403: DESIGN & ANALYSIS OF ALGORITHMS Name: Micheal Berdinanth M
Aim:
Q.No 1 : Algorithm
2|Page
3|Page
Python Code
if m == 0 or n == 0:
return 0
if dp[m][n] != -1:
return dp[m][n]
if str1[m-1] == str2[n-1]:
dp[m][n] = 1 + dp_lcs(str1, str2, m-1, n-1, dp)
else:
dp[m][n] = max(dp_lcs(str1, str2, m, n-1, dp), dp_lcs(str1, str2, m-1,
n, dp))
return dp[m][n]
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0:
L[i][j] = 0
elif X[i-1] == Y[j-1]:
L[i][j] = L[i-1][j-1]+1
else:
4|Page
L[i][j] = max(L[i-1][j], L[i][j-1])
return L[m][n]
str1 = "ABCBDAB"
str2 = "BDCABA"
m = len(str1)
n = len(str2)
dp = [[-1 for _ in range(n + 1)] for _ in range(m + 1)]
OUTPUT
5|Page
Q.No 2 : Algorithm
Python Code
#domino solitaire problem
no_of_cols=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
prev_state=0
final=abs(arr1[0]-arr2[0])
for i in range(1,no_of_cols):
vertical=final+abs(arr1[i]-arr2[i])
horizontal=prev_state+abs(arr1[i]-arr1[i-1])+abs(arr2[i]-arr2[i-1])
prev_state=final
final=max(vertical,horizontal)
print("Score:",final)
6|Page
Output
Learning Outcomes
7|Page