📜  用二进制表示形成回文的子序列计数

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

用二进制表示形成回文的子序列计数

给定一个数组A[] 。任务是计算子序列的数量,以便数字的二进制表示的串联表示回文。

例子:

方法:可以通过在将给定数组转换为每个元素的二进制表示后检查每个子序列的回文来解决问题。

请按照以下步骤解决问题:

  • 首先,将数组的所有整数替换为它们的二进制表示,并将其存储到字符串的向量中。
  • 现在,找到字符串向量的所有子序列:
    • 检查子序列是否是回文。
    • 如果为真,则增加计数。
  • 最后,打印count

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to convert array element into
// their binary representation
vector convert(vector arr,
                       int n)
{
 
    vector v;
    int i, x;
    for (i = 0; i < n; i++) {
        x = arr[i];
        string s;
 
        // Convert into binary one by one
        while (x) {
 
            int r = x % 2;
            s += to_string(r);
            x /= 2;
        }
 
        // Reverse the string and store
        // into vector v
        reverse(s.begin(), s.end());
 
        v.push_back(s);
    }
    return v;
}
 
// Function to check palindrome
bool palindrome(string a, int i, int j)
{
    while (i < j) {
 
        // If the integer at i is
        // not equal to j
        // then return false
        if (a[i] != a[j])
            return false;
 
        i++;
        j--;
    }
 
    // All a[i] is equal to a[j]
    // then return true
    return true;
}
 
// Function to count palindromic subsequences
void countSubsequences(vector arr,
                       int i, string s,
                       int& count)
{
    // Check the condition of palindrome
    // if it is the leaf of recursion tree
    if (i == arr.size()) {
        int l = s.length();
 
        // Check for palindrome and
        // increment count
        if (l > 0 && palindrome(s, 0, l - 1)) {
 
            count++;
        }
    }
    else {
 
        // Subsequence without including
        // the element at current index
        countSubsequences(arr, i + 1, s, count);
 
        // Concatenate string
        s += (arr[i]);
 
        // Subsequence including the element
        // at current index
        countSubsequences(arr, i + 1,
                          s, count);
    }
    return;
}
 
// Function to find all the subsequences
int solve(vector& arr)
{
    int count = 0, i = 0;
    vector v = convert(arr, arr.size());
 
    // Recursive function call
    countSubsequences(v, i, "", count);
    return count;
}
 
// Driver code
int main()
{
    vector arr = { 4, 9, 3, 15, 23 };
 
    // First replace all the array element
    // to their binary form
    int count = solve(arr);
 
    cout << count;
    return 0;
}


C#
// C# implementation of above approach
using System;
using System.Collections;
 
class GFG {
 
  static int count = 0;
 
  static string reverse(string str)
  {
    char[] chars = str.ToCharArray();
    for (int i = 0, j = str.Length - 1; i < j;
         i++, j--) {
      char c = chars[i];
      chars[i] = chars[j];
      chars[j] = c;
    }
    return new string(chars);
  }
 
  // Function to convert array element into
  // their binary representation
  static ArrayList convert(int[] arr, int n)
  {
 
    ArrayList v = new ArrayList();
    int i = 0, x = 0;
    for (i = 0; i < n; i++) {
      x = arr[i];
      string s = "";
 
      // Convert into binary one by one
      while (x > 0) {
 
        int r = x % 2;
        s += r.ToString();
        x /= 2;
      }
 
      // Reverse the string and store
      // into vector v
      s = reverse(s);
 
      v.Add(s);
    }
    return v;
  }
 
  // Function to check palindrome
  static bool palindrome(string a, int i, int j)
  {
    while (i < j) {
 
      // If the integer at i is
      // not equal to j
      // then return false
      if (a[i] != a[j]) {
        return false;
      }
 
      i++;
      j--;
    }
 
    // All a[i] is equal to a[j]
    // then return true
    return true;
  }
 
  // Function to count palindromic subsequences
  static void countSubsequences(ArrayList arr, int i,
                                string s)
  {
     
    // Check the condition of palindrome
    // if it is the leaf of recursion tree
    if (i == arr.Count) {
      int l = s.Length;
 
      // Check for palindrome and
      // increment count
      if (l > 0 && palindrome(s, 0, l - 1) == true) {
 
        count++;
      }
    }
    else {
 
      // Subsequence without including
      // the element at current index
      countSubsequences(arr, i + 1, s);
 
      // Concatenate string
      s += (arr[i]);
 
      // Subsequence including the element
      // at current index
      countSubsequences(arr, i + 1, s);
    }
    return;
  }
 
  // Function to find all the subsequences
  static int solve(int[] arr)
  {
    int i = 0;
    ArrayList v = convert(arr, arr.Length);
 
    // Recursive function call
    countSubsequences(v, i, "");
    return count;
  }
 
  // Driver code
  public static void Main()
  {
    int[] arr = { 4, 9, 3, 15, 23 };
 
    // First replace all the array element
    // to their binary form
    int c = solve(arr);
 
    Console.Write(c);
  }
}
 
// This code is contributed by Samim Hossain Mondal.



输出
6

时间复杂度: O(2 N * N)
辅助空间O(N)