📜  Bit Stuffing和Bit Destuffing的实现

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

Bit Stuffing和Bit Destuffing的实现

位填充是在帧序列遇到5个连续的 1时插入一个额外位为0的过程。给定一个大小为N的数组arr[] ,由01 组成,任务是在位填充后返回一个数组。

例子:

方法:这个想法是检查给定的数组是否由 5 个连续的 1组成。请按照以下步骤解决问题:

  • 初始化存储填充数组的数组brr[] 。此外,创建一个变量计数来维护连续 1 的计数。
  • 使用[0, N)范围内的变量i在 while 循环中遍历并执行以下任务:
    • 如果arr[i]1 ,则检查接下来的4位是否也设置了位。如果是,则在将所有 5 个设置位插入数组brr[]后插入一个 0 位。
    • 否则,将arr[i]的值插入到数组brr[]中。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function for bit stuffing
void bitStuffing(int N, int arr[])
{
     
    // Stores the stuffed array
    int brr[30];
 
    // Variables to traverse arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N)
    {
         
        // If the current bit is a set bit
        if (arr[i] == 1)
        {
             
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // next 5 bits
            for(k = i + 1; arr[k] == 1 && k < N && count < 5;
                k++)
            {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set bits
                // are found insert a 0 bit
                if (count == 5)
                {
                    j++;
                    brr[j] = 0;
                }
                i = k;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr[]
        else
        {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for(i = 0; i < j; i++)
        cout << brr[i];
}
 
// Driver code
int main()
{
    int N = 6;
    int arr[] = { 1, 1, 1, 1, 1, 1 };
 
    bitStuffing(N, arr);;
    return 0;
}
 
// This code is contributed by target_2


C
// C program for the above approach
#include 
#include 
 
// Function for bit stuffing
void bitStuffing(int N, int arr[])
{
    // Stores the stuffed array
    int brr[30];
 
    // Variables to traverse arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // next 5 bits
            for (k = i + 1;
                 arr[k] == 1
                 && k < N
                 && count < 5;
                 k++) {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set bits
                // are found insert a 0 bit
                if (count == 5) {
                    j++;
                    brr[j] = 0;
                }
                i = k;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr[]
        else {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for (i = 0; i < j; i++)
        printf("%d", brr[i]);
}
 
// Driver Code
int main()
{
    int N = 6;
    int arr[] = { 1, 1, 1, 1, 1, 1 };
 
    bitStuffing(N, arr);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function for bit stuffing
static void bitStuffing(int N, int arr[])
{
   
    // Stores the stuffed array
    int []brr = new int[30];
 
    // Variables to traverse arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // next 5 bits
            for (k = i + 1; k < N &&
                 arr[k] == 1
                  
                 && count < 5;
                 k++) {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set bits
                // are found insert a 0 bit
                if (count == 5) {
                    j++;
                    brr[j] = 0;
                }
                i = k;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr[]
        else {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for (i = 0; i < j; i++)
        System.out.printf("%d", brr[i]);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 6;
    int arr[] = { 1, 1, 1, 1, 1, 1 };
 
    bitStuffing(N, arr);
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function for bit stuffing
def bitStuffing(N, arr):
     
    # Stores the stuffed array
    brr = [0 for _ in range(30)]
 
    # Variables to traverse arrays
    k = 0
    i = 0
    j = 0
 
    # Stores the count of consecutive ones
    count = 1
 
    # Loop to traverse in the range [0, N)
    while (i < N):
 
        # If the current bit is a set bit
        if (arr[i] == 1):
 
            # Insert into array brr[]
            brr[j] = arr[i]
 
            # Loop to check for
            # next 5 bits
            k = i + 1
            while True:
                if not (k < N and arr[k] == 1 and
                        count < 5):
                    break
 
                j += 1
                brr[j] = arr[k]
                count += 1
 
                # If 5 consecutive set bits
                # are found insert a 0 bit
                if (count == 5):
                    j += 1
                    brr[j] = 0
 
                i = k
                k += 1
 
        # Otherwise insert arr[i] into
        # the array brr[]
        else:
            brr[j] = arr[i]
 
        i += 1
        j += 1
 
    # Print Answer
    for i in range(0, j):
        print(brr[i], end = "")
 
# Driver Code
if __name__ == "__main__":
 
    N = 6
    arr = [ 1, 1, 1, 1, 1, 1 ]
 
    bitStuffing(N, arr)
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function for bit stuffing
static void bitStuffing(int N, int[] arr)
{
     
    // Stores the stuffed array
    int[] brr = new int[30];
 
    // Variables to traverse arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N)
    {
         
        // If the current bit is a set bit
        if (arr[i] == 1)
        {
             
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // next 5 bits
            k = i + 1;
            while (k < N && arr[k] == 1 && count < 5)
            {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set bits
                // are found insert a 0 bit
                if (count == 5)
                {
                    j++;
                    brr[j] = 0;
                }
                i = k;
                k++;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr[]
        else
        {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for(i = 0; i < j; i++)
        Console.Write(brr[i]);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
    int[] arr = { 1, 1, 1, 1, 1, 1 };
 
    bitStuffing(N, arr);
}
}
 
// This code is contributed by ukasp


Javascript


C
// C program for the above approach
#include 
#include 
 
// Function for bit de-stuffing
void bitDestuffing(int N, int arr[])
{
    // Stores the de-stuffed array
    int brr[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1;
                 arr[k] == 1
                 && k < N
                 && count < 5;
                 k++) {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set
                // bits are found skip the
                // next bit in arr[]
                if (count == 5) {
                    k++;
                }
                i = k;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr
        else {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for (i = 0; i < j; i++)
        printf("%d", brr[i]);
}
 
// Driver Code
int main()
{
    int N = 7;
    int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
 
    bitDestuffing(N, arr);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function for bit de-stuffing
static void bitDestuffing(int N, int arr[])
{
   
    // Stores the de-stuffed array
    int []brr = new int[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1; k


Python3
# Python program for the above approach
 
# Function for bit de-stuffing
def bitDestuffing(N, arr):
   
    # Stores the de-stuffed array
    brr = [0 for i in range(30)];
 
    # Variables to traverse the arrays
    k = 0;
    i = 0;
    j = 0;
 
    # Stores the count of consecutive ones
    count = 1;
 
    # Loop to traverse in the range [0, N)
    while (i < N):
 
        # If the current bit is a set bit
        if (arr[i] == 1):
 
            # Insert into array brr
            brr[j] = arr[i];
 
            # Loop to check for
            # the next 5 bits
            for k in range(i + 1, k < N and arr[k] == 1 and count < 5,1):
                j += 1;
                brr[j] = arr[k];
                count += 1;
 
                # If 5 consecutive set
                # bits are found skip the
                # next bit in arr
                if (count == 5):
                    k += 1;
 
                i = k;
 
 
 
        # Otherwise insert arr[i] into
        # the array brr
        else:
            brr[j] = arr[i];
 
        i += 1;
        j += 1;
 
    # PrAnswer
    for i in range(0, j):
        print(brr[i],end="");
 
# Driver Code
if __name__ == '__main__':
    N = 7;
    arr = [1, 1, 1, 1, 1, 0, 1];
 
    bitDestuffing(N, arr);
 
# This code contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function for bit de-stuffing
static void bitDestuffing(int N, int[] arr)
{
   
    // Stores the de-stuffed array
    int []brr = new int[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1; k


Javascript


输出
1111101

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

Bit DestuffingBit Unstuffing是撤消在位填充过程中对数组所做的更改的过程,即在遇到5个连续的 1后删除额外的0位。

例子:

方法:这个问题可以类似于比特填充问题来解决。上面讨论的方法中唯一需要的更改是每当遇到5个连续的 1时,跳过数组arr[]中的下一位,而不是在数组brr[]中插入一个0位。

下面是上述方法的实现:

C

// C program for the above approach
#include 
#include 
 
// Function for bit de-stuffing
void bitDestuffing(int N, int arr[])
{
    // Stores the de-stuffed array
    int brr[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1;
                 arr[k] == 1
                 && k < N
                 && count < 5;
                 k++) {
                j++;
                brr[j] = arr[k];
                count++;
 
                // If 5 consecutive set
                // bits are found skip the
                // next bit in arr[]
                if (count == 5) {
                    k++;
                }
                i = k;
            }
        }
 
        // Otherwise insert arr[i] into
        // the array brr
        else {
            brr[j] = arr[i];
        }
        i++;
        j++;
    }
 
    // Print Answer
    for (i = 0; i < j; i++)
        printf("%d", brr[i]);
}
 
// Driver Code
int main()
{
    int N = 7;
    int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
 
    bitDestuffing(N, arr);
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function for bit de-stuffing
static void bitDestuffing(int N, int arr[])
{
   
    // Stores the de-stuffed array
    int []brr = new int[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1; k

Python3

# Python program for the above approach
 
# Function for bit de-stuffing
def bitDestuffing(N, arr):
   
    # Stores the de-stuffed array
    brr = [0 for i in range(30)];
 
    # Variables to traverse the arrays
    k = 0;
    i = 0;
    j = 0;
 
    # Stores the count of consecutive ones
    count = 1;
 
    # Loop to traverse in the range [0, N)
    while (i < N):
 
        # If the current bit is a set bit
        if (arr[i] == 1):
 
            # Insert into array brr
            brr[j] = arr[i];
 
            # Loop to check for
            # the next 5 bits
            for k in range(i + 1, k < N and arr[k] == 1 and count < 5,1):
                j += 1;
                brr[j] = arr[k];
                count += 1;
 
                # If 5 consecutive set
                # bits are found skip the
                # next bit in arr
                if (count == 5):
                    k += 1;
 
                i = k;
 
 
 
        # Otherwise insert arr[i] into
        # the array brr
        else:
            brr[j] = arr[i];
 
        i += 1;
        j += 1;
 
    # PrAnswer
    for i in range(0, j):
        print(brr[i],end="");
 
# Driver Code
if __name__ == '__main__':
    N = 7;
    arr = [1, 1, 1, 1, 1, 0, 1];
 
    bitDestuffing(N, arr);
 
# This code contributed by shikhasingrajput

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function for bit de-stuffing
static void bitDestuffing(int N, int[] arr)
{
   
    // Stores the de-stuffed array
    int []brr = new int[30];
 
    // Variables to traverse the arrays
    int i, j, k;
    i = 0;
    j = 0;
 
    // Stores the count of consecutive ones
    int count = 1;
 
    // Loop to traverse in the range [0, N)
    while (i < N) {
 
        // If the current bit is a set bit
        if (arr[i] == 1) {
 
            // Insert into array brr[]
            brr[j] = arr[i];
 
            // Loop to check for
            // the next 5 bits
            for (k = i + 1; k

Javascript


输出
1111101

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