CodeForces

来源:互联网 发布:mac版网络游戏 编辑:程序博客网 时间:2024/06/18 13:17
You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.


Let's introduce several definitions:


A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj.
The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].
Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.


Input
The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.


Output
In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.


Example
Input
ABACABA
Output
3
1 4
3 2
7 1
Input
AAA
Output
3
1 3
2 2
3 1


给定一个串的定义,在原串前缀和后缀相等的才算一个串,并且找出这样的串在原串中出现的次数。


用后缀数组 找出sa[i] 为0的串所在的排名,说明是从0开始的,然后往上往下找,满足有相同的长度并且这种长度必须有其中的一个
sa[i]+height[i]〉=len 这个串成立。然后后缀数组有个特殊的性质,就是长度大的必定对长度小的有贡献,那么就是最后记录一下后缀和就好
。。手残党一开始写的时候想着各种用set map 存下来哪种串可行,但是遍历前后是有顺序的这样做不可行,所以直接数组标记长度就好,存下所有的和,
那么往前往后只要有一个在此长度可行的就好。




首先利用后缀数组处理出sa[i]代表排名第i位的后缀的起始位置
处理出rank[i]代表第i个位置起始的后缀的rank
处理出height[i]代表排名第i位的和排名i-1位的公共前缀的长度。

那么我们要找后缀和前缀相等的就是找到rank[0],然后按照排名,向前向后遍历,任意两个后缀的公共前缀就是他们[i,j]区间内所有height的最小值。只要得到的公共前缀等于后缀的长度,那么证明前缀和后缀匹配上。记录该长度的前缀能够匹配

记录某一个子串出现的次数,就是每次记录和原字符串的公共前缀的长度,然后在对应位置+1,因为比这个长度小的也会得到一个贡献,所以最后求一下后缀和即可。 



#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5+500;int t1[MAXN],t2[MAXN],c[MAXN];bool cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(char  str[],int sa[],int ra[],int height[],int n,int m){    n++;    int i,j,p,*x=t1,*y=t2;    for(i=0;i<m;i++) c[i]=0;    for(i=0;i<n;i++) c[x[i]=str[i]]++;    for(i=1;i<m;i++) c[i]+=c[i-1];    for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for(j=1;j<=n;j<<=1)    {        p=0;        for(i=n-j;i<n;i++) y[p++]=i;        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;        for(i=0;i<m;i++) c[i]=0;        for(i=0;i<n;i++) c[x[y[i]]]++;        for(i=1;i<m;i++) c[i]+=c[i-1];        for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;x[sa[0]]=0;        for(i=1;i<n;i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        if(p>=n) break;        m=p;    }    int k=0;    n--;    for(i=0;i<=n;i++) ra[sa[i]]=i;    for(i=0;i<n;i++)     {        if(k) k--;        j=sa[ra[i]-1];        while(str[i+k]==str[j+k])k++;        height[ra[i]]=k;    }}int ra[MAXN],height[MAXN];int sa[MAXN],num[MAXN];char str[MAXN];int ans[MAXN];int ff[MAXN];int main(){scanf("%s",str);int len=strlen(str);str[len]=0;da(str,sa,ra,height,len,100);int n=len;int temp=1e9;int x=ra[0];for(int i=x+1;i<=len;i++){temp=min(temp,height[i]);if(!temp) break;if(temp+sa[i]>=len)ff[temp]=1;ans[temp]++;}temp=height[x];for(int i=x-1;i>=0;i--){ans[temp]++;if(temp+sa[i]>=len) ff[temp]=1;temp=min(temp,height[i]);if(!temp) break;}int cnt=0;ff[len]=1;ans[len]=1;for(int i=MAXN-10;i>0;i--){if(ff[i]) cnt++;ans[i]+=ans[i+1];}printf("%d\n",cnt );for(int i=1;i<MAXN-10;i++){if(ff[i]) printf("%d %d\n",i,ans[i] );}}





原创粉丝点击