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

📅  最后修改于: 2021-09-03 14:38:12             🧑  作者: 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


Javascript


输出:
10101010101010

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live