📌  相关文章
📜  给定操作生成的序列中第N个二进制字符串中的第M位

📅  最后修改于: 2021-04-27 05:28:18             🧑  作者: Mango

给定两个整数NM ,通过以下步骤生成N个二进制字符串的序列:

  • S 0 =“ 0”
  • S 1 =“ 1”
  • 通过公式S i =反向(S i – 2 )+反向(S i – 1 )生成剩余的字符串

任务是在第N字符串找到第M设置位。

例子:

简单方法:最简单的方法是将生成S 2〜S N – 1和遍历字符串S N – 1找到M位。

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

高效方法:请按照以下步骤优化上述方法:

  • 计算并存储数组中的前N个斐波那契数,例如fib []
  • 现在,在第N字符串搜索第M比特。
  • 如果N> 1:考虑S N是反向的字符串S N串接– 2和的字符串S N反向– 1,字符串S N的长度– 2等于FIB [N – 2]和的长度字符串S N – 1等于fib [N – 1]
    • 如果M≤fib [n-2]:表示M位于S N – 2中,因此,递归搜索字符串S N – 2的(fib [N – 2] + 1 – M)位。
    • 如果M> FIB [N – 2]:它标志着使得M在于S N – 1,因此,递归搜索(FIB [N – 1] + 1 – (M – FIB [N – 2]))S N – 1
  • 如果N≤1:则返回N。

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
#define maxN 10
 
// Function to calculate N
// Fibonacci numbers
void calculateFib(int fib[], int n)
{
    fib[0] = fib[1] = 1;
    for (int x = 2; x < n; x++) {
        fib[x] = fib[x - 1] + fib[x - 2];
    }
}
 
// Function to find the mth bit
// in the string Sn
int find_mth_bit(int n, int m, int fib[])
{
    // Base case
    if (n <= 1) {
        return n;
    }
 
    // Length of left half
    int len_left = fib[n - 2];
 
    // Length of the right half
    int len_right = fib[n - 1];
 
    if (m <= len_left) {
 
        // Recursive check in the left half
        return find_mth_bit(n - 2,
                            len_left + 1 - m, fib);
    }
    else {
        // Recursive check in the right half
        return find_mth_bit(
            n - 1, len_right + 1
                       - (m - len_left),
            fib);
    }
}
 
void find_mth_bitUtil(int n, int m)
{
 
    int fib[maxN];
    calculateFib(fib, maxN);
    int ans = find_mth_bit(n, m, fib);
    cout << ans << ' ';
}
 
// Driver Code
int main()
{
 
    int n = 5, m = 3;
    find_mth_bitUtil(n, m);
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
static final int maxN = 10;
 
// Function to calculate N
// Fibonacci numbers
static void calculateFib(int fib[],
                         int n)
{
  fib[0] = fib[1] = 1;
   
  for (int x = 2; x < n; x++)
  {
    fib[x] = fib[x - 1] +
             fib[x - 2];
  }
}
 
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
                        int m,
                        int fib[])
{
  // Base case
  if (n <= 1)
  {
    return n;
  }
 
  // Length of left half
  int len_left = fib[n - 2];
 
  // Length of the right half
  int len_right = fib[n - 1];
 
  if (m <= len_left)
  {
    // Recursive check in
    // the left half
    return find_mth_bit(n - 2,
                        len_left +
                        1 - m, fib);
  }
  else
  {
    // Recursive check in
    // the right half
    return find_mth_bit(n - 1,
                        len_right +
                        1 - (m -
                        len_left), fib);
  }
}
 
static void find_mth_bitUtil(int n, int m)
{
  int []fib = new int[maxN];
  calculateFib(fib, maxN);
  int ans = find_mth_bit(n, m, fib);
  System.out.print(ans + " ");
}
 
// Driver Code
public static void main(String[] args)
{
  int n = 5, m = 3;
  find_mth_bitUtil(n, m);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for above approach
maxN = 10
 
# Function to calculate N
# Fibonacci numbers
def calculateFib(fib, n):
     
    fib[0] = fib[1] = 1
    for x in range(2, n):
        fib[x] = (fib[x - 1] +
                  fib[x - 2])
 
# Function to find the mth bit
# in the string Sn
def find_mth_bit(n, m, fib):
     
    # Base case
    if (n <= 1):
        return n
 
    # Length of left half
    len_left = fib[n - 2]
 
    # Length of the right half
    len_right = fib[n - 1]
 
    if (m <= len_left):
         
        # Recursive check in the left half
        return find_mth_bit(n - 2,
                 len_left + 1 - m, fib)
    else:
         
        # Recursive check in the right half
        return find_mth_bit(n - 1,
                    len_right + 1 -
                    (m - len_left), fib)
 
def find_mth_bitUtil(n, m):
 
    fib = [0 for i in range(maxN)]
    calculateFib(fib, maxN)
     
    ans = find_mth_bit(n, m, fib)
     
    print(ans)
 
# Driver Code
if __name__ == '__main__':
 
    n = 5
    m = 3
     
    find_mth_bitUtil(n, m)
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
class GFG{
 
static int maxN = 10;
 
// Function to calculate N
// Fibonacci numbers
static void calculateFib(int []fib ,
                         int n)
{
  fib[0] = fib[1] = 1;
   
  for (int x = 2; x < n; x++)
  {
    fib[x] = fib[x - 1] +
             fib[x - 2];
  }
}
 
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
                        int m,
                        int []fib)
{
  // Base case
  if (n <= 1)
  {
    return n;
  }
 
  // Length of left half
  int len_left = fib[n - 2];
 
  // Length of the right half
  int len_right = fib[n - 1];
 
  if (m <= len_left)
  {
    // Recursive check in
    // the left half
    return find_mth_bit(n - 2,
                        len_left +
                        1 - m, fib);
  }
  else
  {
    // Recursive check in
    // the right half
    return find_mth_bit(n - 1,
                        len_right +
                        1 - (m -
                        len_left), fib);
  }
}
 
static void find_mth_bitUtil(int n,
                             int m)
{
  int []fib = new int[maxN];
  calculateFib(fib, maxN);
  int ans = find_mth_bit(n, m, fib);
  Console.Write(ans + " ");
}
 
// Driver Code
public static void Main()
{
  int n = 5, m = 3;
  find_mth_bitUtil(n, m);
}
}
 
// This code is contributed by Chitranayal


Javascript


输出:
1

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