📜  两个不同字符的最长回文序列

📅  最后修改于: 2021-05-17 05:10:07             🧑  作者: Mango

给定一个由小写字母组成的字符串S ,任务是找到仅由两个不同字符组成的最长回文子序列的长度。
例子:

方法:
为了解决该问题,我们需要执行以下步骤:

  • 将每个字符的出现次数存储在一个前缀数组中,并将每个字符的位置存储在另一个数组中。
  • 现在对于每对等于字符的计算它们之间的字符的出现的最大数。

下面是上述方法的实现:

C++
// C++ implementation to find Longest
// Palindromic Subsequence consisting
// of two distinct characters only
 
#include 
using namespace std;
 
// Function that prints the length of
// maximum required subsequence
void longestPalindrome(string s)
{
    // Calculate length of string
    int n = s.length();
 
    vector > pref(
        26,
        vector(n, 0));
    vector > pos(26);
 
    pref[s[0] - 'a'][0]++;
    pos[s[0] - 'a'].push_back(0);
 
    // Store number of occurrences of each
    // character and position of each
    // chatacter in string
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < 26; j++)
            pref[j][i] += pref[j][i - 1];
 
        int index = s[i] - 'a';
        pref[index][i]++;
        pos[index].push_back(i);
    }
 
    int ans = 0;
 
    // Iterate all characters
    for (int i = 0; i < 26; i++) {
 
        // Calculate number of
        // occurences of the
        // current character
        int size = pos[i].size();
        ans = max(ans, size);
 
        // Iterate half of the
        // number of positions
        // of current character
        for (int j = 0; j < size / 2; j++) {
            int l = pos[i][j];
            int r = pos[i][size - j - 1] - 1;
 
            // Determine maximum length
            // of a character between
            // l and r position
            for (int k = 0; k < 26; k++) {
 
                int sum = pref[k][r] - pref[k][l];
 
                // Compute the maximum from all
                ans = max(ans, 2 * (j + 1) + sum);
            }
        }
    }
 
    // Printing maximum length
    cout << ans << "\n";
}
 
// Driver Code
int main()
{
    string S = "bbccdcbb";
 
    longestPalindrome(S);
 
    return 0;
}


Java
// Java implementation to find Longest
// Palindromic Subsequence consisting
// of two distinct characters only
import java.util.*;
class GFG{
 
// Function that prints the length of
// maximum required subsequence
static void longestPalindrome(String s)
{
  // Calculate length of String
  int n = s.length();
 
  int [][]pref = new int[26][n];
 
  Vector []pos = new Vector[26];
  for (int i = 0; i < pos.length; i++)
    pos[i] = new Vector();
 
  pref[s.charAt(0) - 'a'][0]++;
  pos[s.charAt(0) - 'a'].add(0);
 
  // Store number of occurrences of each
  // character and position of each
  // chatacter in String
  for (int i = 1; i < n; i++)
  {
    for (int j = 0; j < 26; j++)
      pref[j][i] += pref[j][i - 1];
 
    int index = s.charAt(i) - 'a';
    pref[index][i]++;
    pos[index].add(i);
  }
 
  int ans = 0;
 
  // Iterate all characters
  for (int i = 0; i < 26; i++)
  {
    // Calculate number of
    // occurences of the
    // current character
    int size = pos[i].size();
    ans = Math.max(ans, size);
 
    // Iterate half of the
    // number of positions
    // of current character
    for (int j = 0; j < size / 2; j++)
    {
      int l = pos[i].elementAt(j);
      int r = pos[i].elementAt(size - j - 1) - 1;
 
      // Determine maximum length
      // of a character between
      // l and r position
      for (int k = 0; k < 26; k++)
      {
        int sum = pref[k][r] - pref[k][l];
 
        // Compute the maximum from all
        ans = Math.max(ans, 2 *
                      (j + 1) + sum);
      }
    }
  }
 
  // Printing maximum length
  System.out.print(ans + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
  String S = "bbccdcbb";
  longestPalindrome(S);
}
}
 
// This code is contributed by Princi Singh


C#
// C# implementation to find longest
// Palindromic Subsequence consisting
// of two distinct characters only
using System;
using System.Collections.Generic;
class GFG{
 
// Function that prints
// the length of maximum
// required subsequence
static void longestPalindrome(String s)
{
  // Calculate length of String
  int n = s.Length;
 
  int [,]pref = new int[26, n];
  List []pos = new List[26];
   
  for (int i = 0; i < pos.Length; i++)
    pos[i] = new List();
 
  pref[s[0] - 'a', 0]++;
  pos[s[0] - 'a'].Add(0);
 
  // Store number of occurrences of each
  // character and position of each
  // chatacter in String
  for (int i = 1; i < n; i++)
  {
    for (int j = 0; j < 26; j++)
      pref[j, i] += pref[j, i - 1];
 
    int index = s[i] - 'a';
    pref[index, i]++;
    pos[index].Add(i);
  }
 
  int ans = 0;
 
  // Iterate all characters
  for (int i = 0; i < 26; i++)
  {
    // Calculate number of
    // occurences of the
    // current character
    int size = pos[i].Count;
    ans = Math.Max(ans, size);
 
    // Iterate half of the
    // number of positions
    // of current character
    for (int j = 0; j < size / 2; j++)
    {
      int l = pos[i][j];
      int r = pos[i][size -
                     j - 1] - 1;
 
      // Determine maximum length
      // of a character between
      // l and r position
      for (int k = 0; k < 26; k++)
      {
        int sum = pref[k, r] -
                  pref[k, l];
 
        // Compute the maximum from all
        ans = Math.Max(ans, 2 *
                      (j + 1) + sum);
      }
    }
  }
 
  // Printing maximum length
  Console.Write(ans + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
  String S = "bbccdcbb";
  longestPalindrome(S);
}
}
 
// This code is contributed by Amit Katiyar


输出:
7