Seek the Name, Seek the Fame

Posted fangbozhen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Seek the Name, Seek the Fame相关的知识,希望对你有一定的参考价值。

https://loj.ac/problem/10036

题目描述

  给出一些字符串,求每个字符串既是前缀又是后缀的字串长度。

思路

  显然这可以用KMP做,只要明确next数组的意思就行。不过一个更暴力的做法,直接字符串Hash,求每个前缀Hash值,再判断与它相同长度的后缀的Hash值是否相同,时间理论上和KMP是相同的,但更好写。

代码

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull p=47;
char s[400005];
ull power[400005],sum[400005];
int main() 

    power[0]=1;
    for(int i=1;i<400000;i++)
        power[i]=power[i-1]*p;
    while(~scanf(" %s",s+1))
    
        int len=strlen(s+1);
        for(int i=1;i<=len;i++)
            sum[i]=sum[i-1]*p+s[i];
        for(int i=1;i<=len;i++)
        
            ull s1=sum[i];
            ull s2=sum[len]-sum[len-i]*power[i];
            if(s1==s2)printf("%d ",i);
        
        printf("\n");
    
    return 0;

 

以上是关于Seek the Name, Seek the Fame的主要内容,如果未能解决你的问题,请参考以下文章