公开20190809博客,汗
http://codeforces.com/contest/432/problem/D
ekmp模板题
此题kmp做法:
ac代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
int ans[maxn],nxt[maxn],anss[maxn];
char str[maxn];
void get_nxt(char a[])
{
int i = 0,j = -1,len = strlen(a);
nxt[0] = -1;
while(i < len)
{
if( j == -1 || a[i] == a[j] )
nxt[++i] = ++j;
else
j = nxt[j];
}
for(int i = len - 1;i>=0;i--)
{
if(nxt[i+1] -1 == -1)
continue;
ans[nxt[i+1]-1] += ans[i];
}
}
int main()
{
while(~scanf("%s",str))
{
int len = strlen(str);
for(int i = 0;i<=len;i++)
ans[i] = 1,anss[i] = 0;
get_nxt(str);
int cnt = 0;
int j = len-1;
anss[j] = 1;
while(nxt[j+1]-1!=-1)
{
anss[nxt[j+1]-1] ++;
j = nxt[j+1]-1;
}
for(int i = 0;i<len;i++)
{
if(anss[i])cnt ++;
}
printf("%d\n",cnt);
for(int i = 0;i<len;i++)
{
if(anss[i]>0)
printf("%d %d\n",i+1,ans[i]);
}
}
}
ans【i】存储每个前缀(s【1】~s【i】)在整个字符串中以子串形式出现的次数
anss【i】是j = len,j = nxt[ j = nxt[ ...j = nxt[ j ] ] ] - 1在j = -1之前途径的j对应的即为答案