📜  查找由数字字符串组成的数组的GCD

📅  最后修改于: 2021-05-13 23:56:30             🧑  作者: Mango

给定一个由数字字符串组成的数组arr [] ,任务是计算给定数组的最大公因数。

例子:

方法:请按照以下步骤解决问题:

  • 计算给定数组所有字符串的长度的GCD,即GCD
  • 检查是否可以通过将子字符串{arr [0] [0],..,arr [0] [GCD – 1]}串联任意次数来构成给定数组的所有字符串。如果发现是真的,则打印{arr [0] [0],..,arr [0] [GCD – 1]}
  • 否则,打印一个空字符串。

下面是上述方法的实现:

C++
// CPP program for the above approach
#include 
using namespace std;
 
// Recursive function
// to return gcd of A and B
int GCD(int lena, int lenb)
{
 
  if (lena == 0)
    return lenb;
 
  if (lenb == 0)
    return lena;
 
  // Base case
  if (lena == lenb)
    return lena;
 
  // Length of A is greater
  if (lena > lenb)
    return GCD(lena - lenb, lenb);
 
  return GCD(lena, lenb - lena);
}
 
// Calculate GCD
string StringGCD(string a, string b)
{
 
  // Store the GCD of the
  // length of the strings
  int gcd = GCD(a.size(), b.size());
  if (a.substr(0, gcd) == b.substr(0, gcd))
  {
    int x = ((int)b.size()/gcd);
    int y = ((int)a.size()/gcd);
    string r="",s="";
 
    while (x--) s += a;
    while (y--) r += b;
 
    if (s == r)
      return a.substr(0, gcd);
  }
  return "-1";
}
 
// Driver Code
int main()
{
  string a = "geeksgeeks";
  string b = "geeks";
 
  // Function call
  cout<<(StringGCD(a, b));
}
 
// This code is contributed by mohit kumar 29


Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
 
// Recursive function
// to return gcd of A and B
static int GCD(int lena, int lenb)
{
  if (lena == 0)
    return lenb;
  if (lenb == 0)
    return lena;
 
  // Base case
  if (lena == lenb)
    return lena;
 
  // Length of A is greater
  if (lena > lenb)
    return GCD(lena - lenb, lenb);
  return GCD(lena, lenb - lena);
}
 
// Calculate GCD
static String StringGCD(String a, String b)
{
 
  // Store the GCD of the
  // length of the Strings
  int gcd = GCD(a.length(), b.length());
  if (a.substring(0, gcd).equals(b.substring(0, gcd)))
  {
    int x = ((int)b.length()/gcd);
    int y = ((int)a.length()/gcd);
    String r="",s="";
 
    while (x-- >0) s += a;
    while (y-- >0) r += b;
 
    if (s.equals(r))
      return a.substring(0, gcd);
  }
  return "-1";
}
 
// Driver Code
public static void main(String[] args)
{
  String a = "geeksgeeks";
  String b = "geeks";
 
  // Function call
  System.out.print(StringGCD(a, b));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python implemenatation of the above approach
 
# Recursive function
# to return gcd of A and B
def GCD(lena, lenb):   
 
  if (lena == 0):
    return lenb
 
  if (lenb == 0):
    return lena
   
  # Base case
  if (lena == lenb):
    return lena
   
  # Length of A is greater
  if (lena > lenb):
    return GCD(lena-lenb, lenb)
  return GCD(lena, lenb-lena)
 
# Calculate GCD
def StringGCD(a, b):
   
  # Store the GCD of the
  # length of the strings
  gcd = GCD(len(a), len(b))
  if a[:gcd] == b[:gcd]:
 
    if a*(len(b)//gcd) == b*(len(a)//gcd):
      return a[:gcd]
 
  return -1
 
# Driver Code
a = 'geeksgeeks'
b = 'geeks'
 
# Function call
print(StringGCD(a, b))


C#
// C# program for the above approach
using System;
class GFG
{
 
// Recursive function
// to return gcd of A and B
static int GCD(int lena, int lenb)
{
  if (lena == 0)
    return lenb;
  if (lenb == 0)
    return lena;
 
  // Base case
  if (lena == lenb)
    return lena;
 
  // Length of A is greater
  if (lena > lenb)
    return GCD(lena - lenb, lenb);
  return GCD(lena, lenb - lena);
}
 
// Calculate GCD
static String StringGCD(String a, String b)
{
 
  // Store the GCD of the
  // length of the Strings
  int gcd = GCD(a.Length, b.Length);
  if (a.Substring(0, gcd).Equals(b.Substring(0, gcd)))
  {
    int x = ((int)b.Length/gcd);
    int y = ((int)a.Length/gcd);
    String r="", s="";
 
    while (x-- >0) s += a;
    while (y-- >0) r += b;
    if (s.Equals(r))
      return a.Substring(0, gcd);
  }
  return "-1";
}
 
// Driver Code
public static void Main(String[] args)
{
  String a = "geeksgeeks";
  String b = "geeks";
 
  // Function call
  Console.Write(StringGCD(a, b));
}
}
 
// This code is contributed by 29AjayKumar


输出:
geeks

时间复杂度: O(N * M),其中M是字符串的长度
辅助空间: O(1)