codeforces 7D Palindrome Degree 字符串hash

30 篇文章 0 订阅
3 篇文章 0 订阅

     如果一个长度为n的字符串s,如果前[n/2]和后[n/2]位相同,并且是k-1度回文串,那么s为k度回文串,求一个字符串所有前缀的度数的和。

f[i]记录一下前i位的度数,没扫描以为更新一下前缀的正hash值和反hash值,如果这两个值相同,那么更新f[i]=f[i>>1]+1;并且累加度数,最后输出结果就行。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int n,m;
char s[10100000];
int f[10100000];
const ull x=257;
int main()
{
//    freopen("in.txt","r",stdin);
    while(~scanf("%s",s))
    {
        ull l=0,r=0,e=1;
        int len=strlen(s);
        memset(f,0,sizeof f);
        ull ans=0;
        for (int i=0; i<len; i++)
        {
            l=l*x+s[i];
            r=s[i]*e+r;
            e*=x;
            if (l==r) ans+=(f[i+1]=f[(i+1)>>1]+1);
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值