题目大意
题目链接
给一个字符串a,它可以看成若干个相同的子串串联形成的,求最大串联次数n。
思路分析
打开题解一看大家都在KMP,孱弱瑟瑟发抖,写题时并不知道KMP,第一眼想着能不能那个二分,但是不满足单调性,于是尝试了一下从 遍历长度 ,如果某个长度的串能经过 次幂运算得到最终结果,那么 就是答案。当然幂运算不能傻呆呆的一个个往上加,快速幂比较合适。
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
#define MAX 1000000
string a, b;
//长度为i的子串是否能够经过n次方到达a
bool match(int i) {
string res, base;
base.assign(a, 0, i);
int num = a.size() / i;//num个i能否组成a
while (num > 0) {
if (num & 1) res.append(base);
base.append(base);
num >>= 1;
}
return res == a;
}
int main() {
while (cin >> a) {
if (a[0] == '.')break;
int s = a.size(), sign = 0;
if (s % 2 == 1) { cout << 1 << endl; continue; }
for (int i = 1; i <= s / 2; i++) {
if (s%i != 0)continue; //i长度次方不能达到s
if (match(i)) {
cout << s / i << endl; sign = 1; break;
}
}
if (!sign)cout << 1 << endl;
}
}