📌  相关文章
📜  查找按字法顺序排列的字符串中最大的回文子序列

📅  最后修改于: 2021-05-04 19:35:45             🧑  作者: Mango

给定一个字符串S 。任务是找到该字符串的按字典顺序排列的最大子序列,即回文。

例子:

Input : str = "abrakadabra"
Output : rr

Input : str = "geeksforgeeks"
Output : ss

这个想法是观察一个被认为是比字典顺序字符b时,如果它的ASCII值比B的大。

由于字符串必须是回文的,因此该字符串应仅包含最大字符,就像我们在第一个和最后一个字符之间放置其他任何较小的字符,这会使该字符串在字典上变小。

要查找词典上最大的子序列,请首先在给定字符串找到最大的字符,然后将所有出现的字符附加到原始字符串,以形成结果子序列字符串。

下面是上述方法的实现:

C++
// CPP program to find the largest
// palindromic subsequence
  
#include 
using namespace std;
  
// Function to find the largest
// palindromic subsequence
string largestPalinSub(string s)
{
    string res;
  
    char mx = s[0];
  
    // Find the largest character
    for (int i = 1; i < s.length(); i++)
        mx = max(mx, s[i]);
  
    // Append all occurrences of largest character
    // to the resultant string
    for (int i = 0; i < s.length(); i++)
        if (s[i] == mx)
            res += s[i];
  
    return res;
}
  
// Driver Code
int main()
{
    string s = "geeksforgeeks";
  
    cout << largestPalinSub(s);
}


Java
// Java program to find the largest 
// palindromic subsequence 
class GFG
{
  
// Function to find the largest 
// palindromic subsequence 
static String largestPalinSub(String s) 
{ 
    String res = ""; 
    char mx = s.charAt(0); 
  
    // Find the largest character 
    for (int i = 1; i < s.length(); i++) 
        mx = (char)Math.max((int)mx, 
                  (int)s.charAt(i)); 
  
    // Append all occurrences of largest 
    // character to the resultant string 
    for (int i = 0; i < s.length(); i++) 
        if (s.charAt(i) == mx) 
            res += s.charAt(i); 
  
    return res; 
} 
  
// Driver Code
public static void main(String []args)
{
    String s = "geeksforgeeks"; 
    System.out.println(largestPalinSub(s));
}
}
  
// This code is contributed by
// Rituraj Jain


Python3
# Python3 program to find the largest 
# palindromic subsequence 
  
# Function to find the largest 
# palindromic subsequence 
def largestPalinSub(s): 
  
    res = "" 
    mx = s[0] 
  
    # Find the largest character 
    for i in range(1, len(s)): 
        mx = max(mx, s[i]) 
  
    # Append all occurrences of largest 
    # character to the resultant string 
    for i in range(0, len(s)): 
        if s[i] == mx: 
            res += s[i] 
  
    return res 
  
# Driver Code 
if __name__ == "__main__":
  
    s = "geeksforgeeks"
    print(largestPalinSub(s)) 
  
# This code is contributed by
# Rituraj Jain


C#
// C# program to find the largest 
// palindromic subsequence 
using System;
  
class GFG
{
  
    // Function to find the largest 
    // palindromic subsequence 
    static string largestPalinSub(string s) 
    { 
        string res = ""; 
        char mx = s[0]; 
      
        // Find the largest character 
        for (int i = 1; i < s.Length; i++) 
            mx = (char)Math.Max((int)mx, 
                    (int)s[i]); 
      
        // Append all occurrences of largest 
        // character to the resultant string 
        for (int i = 0; i < s.Length; i++) 
            if (s[i] == mx) 
                res += s[i]; 
      
        return res; 
    } 
      
    // Driver Code
    public static void Main()
    {
        string s = "geeksforgeeks"; 
        Console.WriteLine(largestPalinSub(s));
    }
}
  
// This code is contributed by Ryuga


PHP


输出:
ss

时间复杂度: O(N),其中N是字符串的长度。