forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOJ1961.cpp
More file actions
43 lines (38 loc) · 811 Bytes
/
POJ1961.cpp
File metadata and controls
43 lines (38 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
POJ 1961 Period
KMP
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
int len;
string str;
void run() {
vector<int> pi(len, -1);
int k = -1;
FOR(q,1,len-1) {
while (k >= 0 && str[k + 1] != str[q]) k = pi[k];
if (str[k + 1] == str[q]) ++k;
pi[q] = k;
}
REP(i,len) {
if (pi[i] == -1) continue;
else {
int d = i - pi[i];
if ((i + 1) % d == 0) cout << i + 1 << " " << (i + 1) / d << endl;
}
}
}
int main() {
int kase = 1;
while (cin >> len && len != 0) {
cin >> str;
cout << "Test case #" << kase++ << endl;
run();
cout << endl;
}
return 0;
}