Files
python/TangDou/LuoGuBook/21_3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

52 lines
1.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
string s;
int n;
/**
测试用例:
abbaabbaabba
参考答案4
*/
//找出n的所有约数
const int N = 1010;
int a[N]; //哪些约数
int cnt; //共多少个
void find_divisors(int n) {
for (int i = 1; i <= n / i; i++)
if (n % i == 0) {
a[++cnt] = i;
//防止出现5*5这样的录入两次
if (i != n / i)a[++cnt] = n / i;
}
}
int main() {
cin >> s;
n = s.size();
//找约数
find_divisors(n);
//排序
sort(a + 1, a + 1 + cnt);
//遍历约数尝试
for (int i = 1; i <= cnt; i++) {
string base = s.substr(0, a[i]);
bool flag = true;
for (int j = a[i]; j < s.size(); j += a[i]) {
if (s.substr(j, a[i]) != base) {
flag = false;
break;
}
}
if (flag) {
cout << a[i] << endl;
exit(0);
}
}
return 0;
}