查找包含一次或多次给定字符串的最短二进制字符串
给定两个二进制字符串S1和S2 ,任务是生成一个新的二进制字符串(可能的最小长度),它可以表示为S1和S2的一次或多次出现。如果无法生成这样的字符串,则在输出中返回-1 。请注意,生成的字符串不能包含不完整的字符串S1 或 S2。
For example, “1111” can be the resultant string for “11” and “1111” as it is 1 occurrence of “1111” and can also be judged as two occurrences of “11”.
例子:
Input: S1 = “1010”, S2 = “101010”
Output: 101010101010
Explanation: The resultant string is 3 occurrences of s1 and 2 occurrences of s2.
Input: S1 = “000”, S2 = “101”
Output: -1
Explanation: There is no way possible to construct such a string.
方法:如果可以制作这样的字符串,那么它的长度将是字符串S1和S2长度的 LCM。因为只有这样,它才能表示为字符串S1和S2的最小倍数。请按照以下步骤解决问题:
- 定义一个函数repeat(int k, 字符串 S)并执行以下任务:
- 将字符串r初始化为空字符串。
- 迭代范围[0, K]并执行以下步骤:
- 将字符串S附加到变量r。
- 返回字符串r作为答案。
- 将变量x和y初始化为字符串S1和S2 的长度。
- 将变量gcd初始化为 GCD x和y。
- 调用函数repeat(y/gcd, s1)多次形成字符串S1并将其存储到变量A中。
- 调用函数repeat(x/gcd, s2)多次形成字符串S2并将其存储到变量B中。
- 如果A等于B,则打印其中任何一个作为答案,否则打印“NO”。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to form the resultant string
string repeat(int k, string S)
{
string r = "";
while (k--) {
r += S;
}
return r;
}
// Function to find if any such string
// exists or not. If yes, find it
void find(string s1, string s2)
{
int x = s1.size(), y = s2.size();
// GCD of x and y
int gcd = __gcd(x, y);
// Form the resultant strings
string A = repeat(y / gcd, s1);
string B = repeat(x / gcd, s2);
// If both the strings are same,
// then print the answer
if (A == B) {
cout << A;
}
else {
cout << "-1";
}
}
// Driver Code
int main()
{
// Initializing strings
string s1 = "1010", s2 = "101010";
find(s1, s2);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
public static int GCD(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return GCD(a - b, b);
return GCD(a, b - a);
}
public static String repeat(int k, String S)
{
String r = "";
while (k--!=0) {
r += S;
}
return r;
}
// Function to find if any such string
// exists or not. If yes, find it
public static void find(String s1, String s2)
{
int x = s1.length(), y = s2.length();
// GCD of x and y
int gcd = GCD(x, y);
// Form the resultant strings
String A = repeat(y / gcd, s1);
String B = repeat(x / gcd, s2);
// If both the strings are same,
// then print the answer
if (A.equals(B)) {
System.out.println(A);
}
else {
System.out.println("-1");
}
}
// Driver Code
public static void main(String[] args)
{
// Initializing strings
String s1 = "1010", s2 = "101010";
find(s1, s2);
}
}
// This code is contributed by maddler.
Python3
# Python program for the above approach
import math
# Function to form the resultant string
def repeat(k, S):
r = ""
while (k):
r += S
k-=1
return r
# Function to find if any such string
# exists or not. If yes, find it
def find(s1, s2):
x = len(s1)
y = len(s2)
# GCD of x and y
gcd = math.gcd(x, y)
# Form the resultant strings
A = repeat(y // gcd, s1)
B = repeat(x // gcd, s2)
# If both the strings are same,
# then print answer
if (A == B):
print(A)
else:
print("-1")
# Driver Code
# Initializing strings
s1 = "1010"
s2 = "101010"
find(s1, s2)
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG{
public static int GCD(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return GCD(a - b, b);
return GCD(a, b - a);
}
public static string repeat(int k, string S)
{
string r = "";
while (k--!=0) {
r += S;
}
return r;
}
// Function to find if any such string
// exists or not. If yes, find it
public static void find(string s1, string s2)
{
int x = s1.Length, y = s2.Length;
// GCD of x and y
int gcd = GCD(x, y);
// Form the resultant strings
string A = repeat(y / gcd, s1);
string B = repeat(x / gcd, s2);
// If both the strings are same,
// then print the answer
if (A.Equals(B)) {
Console.Write(A);
}
else {
Console.Write("-1");
}
}
// Driver Code
public static void Main()
{
// Initializing strings
string s1 = "1010", s2 = "101010";
find(s1, s2);
}
}
// This code is contributed by code_hunt.
Javascript
101010101010
时间复杂度: O(m + n + log(max(m, n)))
辅助空间: O(n)(用于存储字符串A 和 B)