题目链接:Problem - 1473B - Codeforces
解题思路:
先算出两个字符串的最小公倍数,再分别除以字符串长度,构造出两个新的字符串,就是最小公倍数除以老字符串长度个老字符串相加,打个比方,a字符串长度为4,b字符串长度为6,最小公倍数就是12,那么新字符串a就是12 / 4个老的字符串a相加,b同理,如果两个新的字符串相等,九叔出一个新的字符串,否则输出-1。
下面是c++代码:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
string boring(string, int);
int lcm(int, int);
int t, n;
string a, b;
cin >> t;
while (t != 0) {
cin >> a >> b;
int alength = a.length(), blength = b.length();
int g = lcm(alength, blength);
if (boring(a, g / alength) == boring(b, g / blength)) {
cout << boring(a, g / alength) << endl;
}
else {
cout << -1 << endl;
}
t--;
}
return 0;
}
string boring(string s, int k) {
string str = "";
while (k != 0) {
str += s;
k--;
}
return str;
}
int lcm(int x, int y)
{
if (x > y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
int num = x, count = 1;
while (1) {
num = x * count;
if (num % x == 0 && num % y == 0) {
return num;
}
count++;
}
}