📌  相关文章
📜  具有最小周期和给定二进制字符串作为子序列的定期二进制字符串。

📅  最后修改于: 2021-06-26 10:34:29             🧑  作者: Mango

定期二进制字符串:如果二进制字符串可以写成长度较小或相同长度的二进制字符串的重复,则称为二进制字符串。例如,101010是周期为10的周期性二进制字符串,因为我们可以通过在其自身上重复附加10来获得该字符串。通常,具有周期P的字符串S表示S i等于S i + P。
问题:给定二进制字符串S ,任务是在以下附加条件下找到具有最小可能周期的周期字符串
1)给定的字符串S应该是结果的子序列
2)结果的长度不得超过输入字符串长度的两倍。
例子:

方法:
主要思想取决于两种可能性:

  1. 如果字符串由全1或全0组成,则答案是给定的字符串S本身,其周期为1
  2. 如果字符串包含异种元素的,找到具有周期2的字符串和具有长度为给定的字符串S的长度的两倍

下面是上述方法的实现:

C++
// C++ implementation to find the
// periodic string with minimum period
#include 
using namespace std;
  
// Function to find the periodic string
// with minimum period
void findPeriodicString(string S)
{
    int l = 2 * S.length();
  
    int count = 0;
    for (int i = 0; i < S.length(); i++) {
        if (S[i] == '1')
            count++;
    }
  
    // Print the string S if it
    // consists of similar elements
    if (count == S.length() || count == 0)
        cout << S << "\n";
  
    // Find the required periodic
    // string with period 2
    else {
        char arr[l];
        for (int i = 0; i < l; i += 2) {
            arr[i] = '1';
            arr[i + 1] = '0';
        }
  
        for (int i = 0; i < l; i++)
            cout << arr[i];
        cout << "\n";
    }
}
  
// Driver Code
int main()
{
    string S = "1111001";
    findPeriodicString(S);
    return 0;
}


Java
// Java implementation to find the
// periodic string with minimum period
class GFG{
      
// Function to find the periodic string
// with minimum period
static void findPeriodicString(String S)
{
    int l = 2 * S.length();
    int count = 0;
      
    for(int i = 0; i < S.length(); i++)
    {
        if (S.charAt(i) == '1')
            count++;
    }
  
    // Print the string S if it
    // consists of similar elements
    if (count == S.length() || count == 0)
        System.out.println(S);
  
    // Find the required periodic
    // string with period 2
    else
    {
        char arr[] = new char[l];
        for(int i = 0; i < l; i += 2) 
        {
            arr[i] = '1';
            arr[i + 1] = '0';
        }
          
        for(int i = 0; i < l; i++)
            System.out.print(arr[i]);
        System.out.println();
    }
}
  
// Driver Code
public static void main (String[] args)
{
    String S = "1111001";
      
    findPeriodicString(S);
}
}
  
// This code is contributed by chitranayal


Python3
# Python3 implementation to find the
# periodic with minimum period
  
# Function to find the periodic string
# with minimum period
def findPeriodicString(S):
    l = 2 * len(S)
  
    count = 0
    for i in range(len(S)):
        if (S[i] == '1'):
            count += 1
  
    # Print the S if it
    # consists of similar elements
    if (count == len(S) or count == 0):
        print(S)
  
    # Find the required periodic
    # with period 2
    else:
        arr = ['0']*l
        for i in range(0, l, 2):
            arr[i] = '1'
            arr[i + 1] = '0'
  
        for i in range(l):
            print(arr[i],end="")
  
# Driver Code
if __name__ == '__main__':
    S = "1111001"
    findPeriodicString(S)
      
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// periodic string with minimum period 
using System;
  
class GFG{
      
// Function to find the periodic string
// with minimum period
static void findPeriodicString(string S)
{
    int l = 2 * S.Length;
    int count = 0;
      
    for(int i = 0; i < S.Length; i++)
    {
        if (S[i] == '1')
            count++;
    }
  
    // Print the string S if it
    // consists of similar elements
    if (count == S.Length || count == 0)
        Console.WriteLine(S);
  
    // Find the required periodic
    // string with period 2
    else
    {
        char[] arr = new char[l];
        for(int i = 0; i < l; i += 2) 
        {
            arr[i] = '1';
            arr[i + 1] = '0';
        }
          
        for(int i = 0; i < l; i++)
            Console.Write(arr[i]);
              
        Console.WriteLine();
    }
}
  
// Driver code 
public static void Main () 
{ 
    string S = "1111001";
      
    findPeriodicString(S);
} 
} 
  
// This code is contributed by sanjoy_62


输出:
10101010101010

时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。