思路1:
很明显,两个串的最小循环节要是一样的才行。然后取两个串中循环节的重复次数的lcm,将这个循环节重复lcm次,便是答案
AC代码:
import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.file.attribute.AclEntryFlag;
import java.security.AlgorithmConstraints;
import java.sql.Struct;
import java.text.CollationElementIterator;
import java.text.DateFormatSymbols;
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5;
static math_myself math_me = new math_myself();
static List<Integer> list = new ArrayList<>();
static Map<String, Integer> map = new HashMap<>();
// 检查字符串s循环的长度是否为k
// abababab,最小循环节是ab,长度是2
static boolean check_mcs(char s[],int k)
{
if(s.length % k != 0) return false; // 防止aba情况
for(int i = 0 ; i < s.length ; i ++) if(s[i] != s[i % k]) return false;
return true;
}
static boolean check_issame_mcs(char s1[], int x, char s2[], int y)
{
// 长度不同,最小循环节肯定不同
if(x != y) return false;
for(int i = 0 ; i < x ; i ++) if(s1[i] != s2[i]) return false;
return true;
}
public static void main(String[] args ) throws IOException
{
int T = rd.nextInt();
while(T -- > 0)
{
char s1[] = rd.next().toCharArray();
char s2[] = rd.next().toCharArray();
// 寻找s1的最小循环节
int minmcs_len_s1 = 0;
for(int i = 1 ; i <= s1.length ; i ++)
{
if(check_mcs(s1,i))
{
minmcs_len_s1 = i;
break;
}
}
int minmcs_len_s2 = 0;
for(int i = 1 ; i <= s2.length ; i ++)
{
if (check_mcs(s2,i))
{
minmcs_len_s2 = i;
break;
}
}
if(!check_issame_mcs(s1,minmcs_len_s1,s2,minmcs_len_s2))
{
pw.println(-1);
}
else
{
int k1 = s1.length / minmcs_len_s1;
int k2 = s2.length / minmcs_len_s2;
int lcm = math_me.lcm(k1,k2);
for(int i = 1 ; i <= lcm ; i ++)
{
for(int j = 0 ; j < minmcs_len_s1 ; j ++)
{
pw.print(s1[j]);
}
}
}
pw.println();
}
pw.flush();
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next());}
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class PII
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
}
class math_myself
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
boolean check_isPrime(int n)
{
if(n < 2) return false;
for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;
return true;
}
}
思路2: