您的位置:首页 > 其它

CF 246 div2 D Prefixes and Suffixes (所有前缀的出现次数)

2014-06-13 09:36 399 查看
题目链接:http://codeforces.com/contest/432/problem/D

题意:对一个长度不超过10^5的字符串,按长度输出和后缀完全匹配的的前缀的长度,及该前缀在整个串中出现的次数。(可重叠)

#include <cstdio>
#include <cstring>

const int N=100005;

char str
;
int next
,cnt
,ansp
,ansn
,n;

void getNext (char s[],int len)
{
    next[0]=-1;
    int i=0,j=-1;
    while (i<len)
    {
        if (j==-1 || s[i]==s[j])
            next[++i]=++j;
        else j=next[j];
    }
}

void KMP ()
{
    int i=0,j=0;
    while (i<n)
        if (j == -1 || str[i] == str[j])
            i++,j++,cnt[j]++;
        else
            j=next[j];

    for (i=n;i>0;i--) //统计每个前缀出现次数,cnt[i]表示长度为i的前缀出现了cnt[i]次
        if (next[i] != -1)
            cnt[next[i]] += cnt[i];
}

int main ()
{
    scanf("%s",str);
    n=strlen(str);
    memset(cnt,0,sizeof(cnt));
    getNext(str,n);
    KMP();
	int t=n,sum=0;
    while (t) //前缀匹配后缀
    {
        ansp[sum]=t;
        ansn[sum++]=cnt[t];   //
        t=next[t];
    }
    printf("%d\n",sum);
    for (int i=sum-1;i>=0;i--)
        printf("%d %d\n",ansp[i],ansn[i]);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: