RT,萌新奔溃ing
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int kMaxN(1000000);
int kNext[kMaxN + 5];
void Kmp(const string&, int);
void GetFail(const string&, int);
int main(void) {
int n(0), case_cnt(1);
string s;
ios::sync_with_stdio(false);
while (cin >> n && n) {
memset(kNext, 0, sizeof(kNext));
cin >> s;
if (case_cnt != 1)
cout << endl;
cout << "Test case #" << case_cnt++ << endl;
Kmp(s, n);
}
return 0;
}
void Kmp(const string& str, int n) {
GetFail(str, n);
for (int i = 2; i < n + 1; ++i) {
int len(i - kNext[i]);
if (kNext[i] && !(i % len))
cout << i << ' ' << i / len << endl;
}
}
void GetFail(const string& str, int str_len) {
for (int i = 1; i < str_len; ++i) {
int j(kNext[i]);
while (j && str[i] != str[j])
j = kNext[j];
kNext[i + 1] = str[i] == str[j] ? j + 1 : 0;
}
}