📜  最长回文子序列的C# 程序| DP-12

📅  最后修改于: 2022-05-13 01:56:12.172000             🧑  作者: Mango

最长回文子序列的C# 程序| DP-12

给定一个序列,找出其中最长的回文子序列的长度。

最长回文子序列

再举一个例子,如果给定的序列是“BBABCBCAB”,那么输出应该是 7,因为“BABCBAB”是其中最长的回文子序列。 “BBBBB”和“BBCBB”也是给定序列的回文子序列,但不是最长的。

1) 最优子结构:
令 X[0..n-1] 为长度为 n 的输入序列,L(0, n-1) 为 X[0..n-1] 的最长回文子序列的长度。
如果 X 的最后一个字符和第一个字符相同,则 L(0, n-1) = L(1, n-2) + 2。
否则 L(0, n-1) = MAX (L(1, n-1), L(0, n-2))。

以下是处理所有情况的通用递归解决方案。

C#
// C# program of above approach
using System;
 
public class GFG {
 
    // A utility function to get max of two integers
    static int max(int x, int y)
    {
        return (x > y) ? x : y;
    }
    // Returns the length of the longest palindromic subsequence in seq
 
    static int lps(char[] seq, int i, int j)
    {
        // Base Case 1: If there is only 1 character
        if (i == j) {
            return 1;
        }
 
        // Base Case 2: If there are only 2 characters and both are same
        if (seq[i] == seq[j] && i + 1 == j) {
            return 2;
        }
 
        // If the first and last characters match
        if (seq[i] == seq[j]) {
            return lps(seq, i + 1, j - 1) + 2;
        }
 
        // If the first and last characters do not match
        return max(lps(seq, i, j - 1), lps(seq, i + 1, j));
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        String seq = "GEEKSFORGEEKS";
        int n = seq.Length;
        Console.Write("The length of the LPS is " + lps(seq.ToCharArray(), 0, n - 1));
    }
}
 
// This code is contributed by Rajput-Ji


C#
// A Dynamic Programming based C# Program
// for the Egg Dropping Puzzle
using System;
 
class GFG {
 
    // A utility function to get max of
    // two integers
    static int max(int x, int y)
    {
        return (x > y) ? x : y;
    }
 
    // Returns the length of the longest
    // palindromic subsequence in seq
    static int lps(string seq)
    {
        int n = seq.Length;
        int i, j, cl;
 
        // Create a table to store results
        // of subproblems
        int[, ] L = new int[n, n];
 
        // Strings of length 1 are
        // palindrome of length 1
        for (i = 0; i < n; i++)
            L[i, i] = 1;
 
        // Build the table. Note that the
        // lower diagonal values of table
        // are useless and not filled in
        // the process. The values are
        // filled in a manner similar to
        // Matrix Chain Multiplication DP
        // solution (See
        // https:// www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
        // cl is length of substring
        for (cl = 2; cl <= n; cl++) {
            for (i = 0; i < n - cl + 1; i++) {
                j = i + cl - 1;
 
                if (seq[i] == seq[j] && cl == 2)
                    L[i, j] = 2;
                else if (seq[i] == seq[j])
                    L[i, j] = L[i + 1, j - 1] + 2;
                else
                    L[i, j] = max(L[i, j - 1], L[i + 1, j]);
            }
        }
 
        return L[0, n - 1];
    }
 
    /* Driver program to test above
    functions */
    public static void Main()
    {
        string seq = "GEEKS FOR GEEKS";
        int n = seq.Length;
        Console.Write("The length of the "
                      + "lps is " + lps(seq));
    }
}
 
// This code is contributed by nitin mittal.


输出:
The length of the LPS is 5

动态规划解决方案

C#

// A Dynamic Programming based C# Program
// for the Egg Dropping Puzzle
using System;
 
class GFG {
 
    // A utility function to get max of
    // two integers
    static int max(int x, int y)
    {
        return (x > y) ? x : y;
    }
 
    // Returns the length of the longest
    // palindromic subsequence in seq
    static int lps(string seq)
    {
        int n = seq.Length;
        int i, j, cl;
 
        // Create a table to store results
        // of subproblems
        int[, ] L = new int[n, n];
 
        // Strings of length 1 are
        // palindrome of length 1
        for (i = 0; i < n; i++)
            L[i, i] = 1;
 
        // Build the table. Note that the
        // lower diagonal values of table
        // are useless and not filled in
        // the process. The values are
        // filled in a manner similar to
        // Matrix Chain Multiplication DP
        // solution (See
        // https:// www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
        // cl is length of substring
        for (cl = 2; cl <= n; cl++) {
            for (i = 0; i < n - cl + 1; i++) {
                j = i + cl - 1;
 
                if (seq[i] == seq[j] && cl == 2)
                    L[i, j] = 2;
                else if (seq[i] == seq[j])
                    L[i, j] = L[i + 1, j - 1] + 2;
                else
                    L[i, j] = max(L[i, j - 1], L[i + 1, j]);
            }
        }
 
        return L[0, n - 1];
    }
 
    /* Driver program to test above
    functions */
    public static void Main()
    {
        string seq = "GEEKS FOR GEEKS";
        int n = seq.Length;
        Console.Write("The length of the "
                      + "lps is " + lps(seq));
    }
}
 
// This code is contributed by nitin mittal.
输出
The length of the lps is 7

请参阅有关最长回文子序列的完整文章 | DP-12 了解更多详情!