📜  通过反转和补充二进制字符串K次获得的字符串

📅  最后修改于: 2021-05-25 00:42:59             🧑  作者: Mango

给定大小为N的二进制字符串和整数K ,任务是对该字符串执行K个操作并打印最终的字符串:

  • 如果操作号为奇数,则将字符串反向,
  • 如果操作数为偶数,则对字符串进行补充。

例子:

天真的方法:
遍历所有K个步,如果当前步是奇数,则执行相反的操作,否则对字符串补码。

高效方法:观察给定的操作模式:

  • 如果一个字符串被反转偶数次,则获得原始字符串。
  • 类似地,如果一个字符串被补偶数次,则获得原始字符串。
  • 因此,这些运算仅取决于K奇偶校验
  • 因此,我们将计算要执行的反向操作的数量。如果奇偶校验是奇数,那么我们将其反转。否则字符串将保持不变。
  • 同样,我们将计算要执行的补码运算的数量。如果奇偶校验是奇数,那么我们将对其进行补充。否则字符串将保持不变。

下面是上述方法的实现:

C++
// C++ program to perform K operations upon
// the string and find the modified string
#include 
using namespace std;
  
// Function to perform K operations upon
// the string and find modified string
string ReverseComplement(
    string s, int n, int k)
{
  
    // Number of reverse operations
    int rev = (k + 1) / 2;
  
    // Number of complement operations
    int complment = k - rev;
  
    // If rev is odd parity
    if (rev % 2)
        reverse(s.begin(), s.end());
  
    // If complment is odd parity
    if (complment % 2) {
        for (int i = 0; i < n; i++) {
            // Complementing each position
            if (s[i] == '0')
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
  
    // Return the modified string
    return s;
}
  
// Driver Code
int main()
{
    string str = "10011";
    int k = 5;
    int n = str.size();
  
    // Function call
    cout << ReverseComplement(str, n, k);
  
    return 0;
}


Java
// Java program to perform K operations upon
// the String and find the modified String
class GFG{
   
// Function to perform K operations upon
// the String and find modified String
static String ReverseComplement(
    char []s, int n, int k)
{
   
    // Number of reverse operations
    int rev = (k + 1) / 2;
   
    // Number of complement operations
    int complment = k - rev;
   
    // If rev is odd parity
    if (rev % 2 == 1)
        s = reverse(s);
   
    // If complment is odd parity
    if (complment % 2 == 1) {
        for (int i = 0; i < n; i++) {
            // Complementing each position
            if (s[i] == '0')
                s[i] = '1';
            else
                s[i] = '0';
        }
    }
   
    // Return the modified String
    return String.valueOf(s);
}
  
static char[] reverse(char a[]) {
    int i, n = a.length;
    char t;
    for (i = 0; i < n / 2; i++) {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
} 
  
// Driver Code
public static void main(String[] args)
{
    String str = "10011";
    int k = 5;
    int n = str.length();
   
    // Function call
    System.out.print(ReverseComplement(str.toCharArray(), n, k));
   
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to perform K operations upon
# the string and find the modified string
  
# Function to perform K operations upon
# the string and find modified string
def ReverseComplement(s,n,k):
    # Number of reverse operations
    rev = (k + 1) // 2
  
    # Number of complement operations
    complment = k - rev
  
    # If rev is odd parity
    if (rev % 2):
        s = s[::-1]
  
    # If complment is odd parity
    if (complment % 2):
        for i in range(n):
            # Complementing each position
            if (s[i] == '0'):
                s[i] = '1'
            else:
                s[i] = '0'
  
    # Return the modified string
    return s
  
# Driver Code
if __name__ == '__main__':
    str1 = "10011"
    k = 5
    n = len(str1)
  
    # Function call
    print(ReverseComplement(str1, n, k))
  
# This code is contributed by Surendra_Gangwar


C#
// C# program to perform K operations upon 
// the String and find the modified String 
using System;
class GFG{ 
      
// Function to perform K operations upon 
// the String and find modified String 
static string ReverseComplement(char []s, 
                                int n, int k) 
{ 
      
    // Number of reverse operations 
    int rev = (k + 1) / 2; 
      
    // Number of complement operations 
    int complment = k - rev; 
      
    // If rev is odd parity 
    if (rev % 2 == 1) 
        s = reverse(s); 
      
    // If complment is odd parity 
    if (complment % 2 == 1)
    { 
        for (int i = 0; i < n; i++) 
        { 
            // Complementing each position 
            if (s[i] == '0') 
                s[i] = '1'; 
            else
                s[i] = '0'; 
        } 
    } 
      
    // Return the modified String 
    return (new string(s)); 
} 
  
static char[] reverse(char[] a) 
{ 
    int i, n = a.Length; 
    char t; 
    for (i = 0; i < n / 2; i++) 
    { 
        t = a[i]; 
        a[i] = a[n - i - 1]; 
        a[n - i - 1] = t; 
    } 
    return a; 
} 
  
// Driver Code 
public static void Main() 
{ 
    string str = "10011"; 
    int k = 5; 
    int n = str.Length; 
      
    // Function call 
    Console.Write(ReverseComplement(str.ToCharArray(), n, k)); 
} 
} 
  
// This code is contributed by rutvik_56


输出:
11001

时间复杂度: O(N)