题解 | #字符的循环#
字符的循环
https://www.nowcoder.com/practice/03f91454227344538cfc1a647251766e
#include <bits/stdc++.h> using namespace std; int main() { // 找到第一个最小短的串,能通过覆盖到达整个,其余的都是长度倍数中能被整除的! string s; cin >> s; int n = s.size(); vector<int> next(n, 0), ret; next[0] = -1; // 退无可退! for (int i = 2; i < n; i++) { for (int j = next[i - 1]; ; j = next[j]) { if (s[j + 1] == s[i]) { next[i] = j + 1; break; } else if (j == -1) { next[i] = 0; break; } } } int cur(n - 1); while (cur!=-1) { int dist = n - next[cur] - 1; if (n % dist == 0) ret.push_back(dist); cur = next[cur]; } cout << ret.size() << endl; for (auto it = ret.begin(); it != ret.end(); it++) cout << *it << " "; return 0; }