码迷,mamicode.com
首页 > 其他好文 > 详细

Seek the Name, Seek the Fame

时间:2019-10-03 12:32:31      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:type   seek   can   ble   include   sum   时间   class   c++   

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

标签:type   seek   can   ble   include   sum   时间   class   c++   

原文地址:https://www.cnblogs.com/fangbozhen/p/11619379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!