For InputString = "abcdcba"
function(string str) n=Length of String
arr[n+1][n+1]
for i=0 to n
arr[i][0] = 0
arr[0][1] = 0
for i=1 to n
for j=1 to n
if (str[i-1] ==str[j-1] and i!=j) arr[i][j] = 1 + arr[i-1][i-1]
else
arr[i][j] = max(arr[i][j-1], arr[i-1][j])
return arr[n][n]
1) b
2) cd
3) bcd
4) bdc
Answers
Answered by
3
Answer:
4) bdc It was the form of the computer as it is
Explanation:
please mark me as brain list
Answered by
0
The given code is an implementation of Longest Palindromic Subsequence (LPS) algorithm using dynamic programming.
- It finds the length of the longest palindromic subsequence in a given input string.
- LPS is a palindromic string which can be read same from left to right and from right to left.
- For example, for the input string "abcdcba", the possible LPS strings are:
- "b"
- "cdc"
- "bcdcb"
- "dcbcd"
- So, the code returns the length of the longest palindromic subsequence which is 5.
- The code uses a 2D array arr of size n+1 x n+1 to store the length of the longest palindromic subsequence.
- First, it initializes the first row and first column of the array with 0
- Then, it uses a nested loop to fill the rest of the array:
- If the characters at positions i-1 and j-1 in the input string str are equal and i is not equal to j, it updates the value of arr[i][j] as follows:
- This is because, in this case, the characters can be added to the palindromic subsequence.
- Otherwise, it updates the value of arr[i][j] as the maximum of arr[i][j-1] and arr[i-1][j]:
- This is because, in this case, the characters cannot be added to the palindromic subsequence.
- Finally, it returns the value stored in arr[n][n] which is the length of the longest palindromic subsequence.
#SPJ3
Similar questions