📌  相关文章
📜  来自给定数字的唯一子序列的计数,它们是 2 的幂

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

来自给定数字的唯一子序列的计数,它们是 2 的幂

给定一个大小为N并包含[0-9]范围内的数字的字符串S ,任务是打印字符串的所有唯一子序列的计数,这些子序列是2的幂。

例子:

方法:可以通过生成字符串S的所有子序列,然后检查该数字是否为2的幂来解决该问题。请按照以下步骤解决问题:

  • 初始化一个集合allUniqueSubSeq来存储子序列,它是2的幂。
  • 定义一个递归函数,比如uniqueSubSeq(S, ans, index) ,然后执行以下步骤:
    • 基本情况:如果索引等于N,那么如果(int)ans是 2 的幂,则将ans插入集合allUniqueSubSeq中。
    • 否则,有两种情况:
      • 如果在字符串ans中附加了S[index] ,则调用带有参数Sans + S[index]index+1的函数uniqueSubSeq
      • 如果S[index]未附加在字符串ans中,则调用带有参数Sansindex + 1的函数uniqueSubSeq
  • 最后,执行上述步骤后,调用函数uniqueSubSeq(S, “”, 0)然后 打印allUniqueSubSeq.size()作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Set to store subsequences that are
// power of 2
unordered_set allUniqueSubSeq;
 
// Function to check whether the number
// is power of 2 or not
bool checkpower(int n)
{
    if ((n & (n - 1)) == 0)
    {
        return true;
    }
    return false;
}
 
// Auxiliary recursive Function to find
// all the unique subsequences of a string
// that are the power of 2
void uniqueSubSeq(string S, int N, string ans,
                            int index)
{
     
    // If index is equal to N
    if (index == N)
    {
        if (ans.length() != 0)
 
            // If the number is
            // power of 2
            if (checkpower(stoi(ans)))
            {
                 
                // Insert the String
                // in the set
                allUniqueSubSeq.insert(ans);
            }
        return;
    }
 
    // Recursion call, if the S[index]
    // is inserted in ans
    uniqueSubSeq(S, N, ans + S[index], index + 1);
 
    // Recursion call, if S[index] is
    // not inserted in ans
    uniqueSubSeq(S, N, ans, index + 1);
}
 
// Function to find count of all the unique
// subsequences of a string that are the
// power of 2
int Countsubsequneces(string S, int N)
{
     
    // Function Call
    uniqueSubSeq(S, N, "", 0);
 
    // Return the length of set
    // allUniqueSubSeq
    return allUniqueSubSeq.size();
}
 
// Driver Code
int main()
{
     
    // Given Input
    string S = "1216389";
    int N = S.length();
     
    // Function call
    cout << Countsubsequneces(S, N);
     
    return 0;
}
 
// This code is contributed by maddler


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
public class main {
 
    // Set to store subsequences that are
    // power of 2
    static HashSet allUniqueSubSeq
        = new HashSet<>();
 
    // Function to check whether the number
    // is power of 2 or not
    static boolean checkpower(int n)
    {
        if ((n & (n - 1)) == 0) {
            return true;
        }
        return false;
    }
 
    // Auxiliary recursive Function to find
    // all the unique subsequences of a string
    // that are the power of 2
    static void uniqueSubSeq(String S, int N, String ans,
                             int index)
    {
 
        // If index is equal to N
        if (index == N) {
 
            if (ans.length() != 0)
 
                // If the number is
                // power of 2
                if (checkpower(
                        Integer.parseInt(ans.trim()))) {
 
                    // Insert the String
                    // in the set
                    allUniqueSubSeq.add(ans);
                }
            return;
        }
 
        // Recursion call, if the S[index]
        // is inserted in ans
        uniqueSubSeq(S, N, ans + S.charAt(index),
                     index + 1);
 
        // Recursion call, if S[index] is
        // not inserted in ans
        uniqueSubSeq(S, N, ans, index + 1);
    }
 
    // Function to find count of all the unique
    // subsequences of a string that are the
    // power of 2
    static int Countsubsequneces(String S, int N)
    {
        // Function Call
        uniqueSubSeq(S, N, "", 0);
 
        // Return the length of set
        // allUniqueSubSeq
        return allUniqueSubSeq.size();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given Input
        String S = "1216389";
        int N = S.length();
 
        // Function call
        System.out.println(Countsubsequneces(S, N));
    }
}


Python3
# Python program for the above approach
 
# Set to store subsequences that are
# power of 2
allUniqueSubSeq = set()
 
# Function to check whether the number
# is power of 2 or not
def checkpower(n):
    if(n & (n-1) == 0):
        return True
    return False
 
# Auxiliary recursive Function to find
# all the unique subsequences of a string
# that are the power of 2
def uniqueSubSeq(S, N, ans, index):
   
    # If index is equal to N
    if (index == N):
        if (len(ans) != 0):
           
            # If the number is
            # power of 2
            if(checkpower(int(ans))):
                allUniqueSubSeq.add(ans)
        return
       
    # Recursion call, if the S[index]
    # is inserted in ans
    uniqueSubSeq(S, N, ans+S[index], index+1)
     
    # Recursion call, if the S[index]
    # is not inserted in ans
    uniqueSubSeq(S, N, ans, index+1)
 
# Function to find count of all the unique
# subsequences of a string that are
# the power of 2
def countSubsequences(S, N):
   
    # Function call
    uniqueSubSeq(S, N, "", 0)
     
    # Return the length of set
    # allUniqueSubSeq
    return len(allUniqueSubSeq)
 
# Driver code
if __name__ == '__main__':
   
    # Given Input
    S = "1216389"
    N = len(S)
 
    # Function call
    print(countSubsequences(S, N))
     
# This code is contributed by MuskanKalra1


C#
using System;
using System.Collections.Generic;
public class GFG {
 
    // Set to store subsequences that are
    // power of 2
    static HashSet allUniqueSubSeq
        = new HashSet();
 
    // Function to check whether the number
    // is power of 2 or not
    static bool checkpower(int n)
    {
        if ((n & (n - 1)) == 0) {
            return true;
        }
        return false;
    }
 
    // Auxiliary recursive Function to find
    // all the unique subsequences of a string
    // that are the power of 2
    static void uniqueSubSeq(String S, int N, String ans,
                             int index)
    {
 
        // If index is equal to N
        if (index == N) {
 
            if (ans.Length != 0)
 
                // If the number is
                // power of 2
                if (checkpower(
                        int.Parse(ans))) {
 
                    // Insert the String
                    // in the set
                    allUniqueSubSeq.Add(ans);
                }
            return;
        }
 
        // Recursion call, if the S[index]
        // is inserted in ans
        uniqueSubSeq(S, N, ans + S[index],
                     index + 1);
 
        // Recursion call, if S[index] is
        // not inserted in ans
        uniqueSubSeq(S, N, ans, index + 1);
    }
 
    // Function to find count of all the unique
    // subsequences of a string that are the
    // power of 2
    static int Countsubsequeneces(String S, int N)
    {
        // Function Call
        uniqueSubSeq(S, N, "", 0);
 
        // Return the length of set
        // allUniqueSubSeq
        return allUniqueSubSeq.Count;
    }
 
    // Driver Code
 
    static public void Main()
    {
        String S = "1216389";
        int N = S.Length;
 
        // Function call
        Console.WriteLine(Countsubsequeneces(S, N));
    }
}
 
// This code is contributed by maddler.


Javascript


输出
5

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