题目分析 :首先得到母串的next数组,next数组的含义是next[j]的值表示str[0...j-1](我的next[0]是-1)这个子串的前后缀匹配的最长长度,如样例1
index 0 1 2 3 4 5 6 7
str A B A C A B A
next -1 0 0 1 0 1 2 3
next[6] = 2即ABACAB这个子串的前后缀最长匹配是2(AB)
由此性质我们可以发现满足条件的子串即是next[next[len。。。]]不断往前递归直到为0,因为长的可能会包含短的,我们可以递归得到dp数组(dp记录的就是子串出现的次数)re数组的递归式为dp[next[i]] += dp[i];
#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;
char str[maxn];
int next[maxn], dp[maxn];
int ans1[maxn], ans2[maxn], cnt;
void KMP(){
next[0] = -1;
int i = 0, j = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
void solve(){
int len = strlen(str);
for(int i = 0; i <= len; i++){
dp[i] = 1;
}
for(int i = len; i >= 1; i--){
if(next[i] != -1){
dp[next[i]] += dp[i];
}
}
int pos = len;
while(pos){
ans1[cnt] = pos;
ans2[cnt++] = dp[pos];
pos = next[pos];
}
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%s", str);
KMP();
solve();
printf("%d\n", cnt);
while(cnt--){
printf("%d %d\n", ans1[cnt], ans2[cnt]);
}
return 0;
}