CodeForces - 7D Palindrome Degree【字符串hash】【dp】

23 篇文章 0 订阅
6 篇文章 0 订阅

【题目描述】
String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length 在这里插入图片描述are (k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.

Let’s call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, “abaaba” has degree equals to 3.

You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes.

【输入】
The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5·106. The string is case-sensitive.

【输出】
Output the only number — the sum of the polindrome degrees of all the string’s prefixes.

【样例输入】
a2A

【样例输出】
1

【样例输入】
abacaba

【样例输出】
6

题目链接:https://codeforces.com/contest/7/problem/D

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
static const int SEED=103;
static const int MAXN=5000000;
ll f[MAXN+10];
int dp[MAXN+10];
char s[MAXN+10];
int main()
{
    scanf("%s",s); //string和cin都会T
    int len=strlen(s);
    f[0]=1;
    for(int i=1;i<len;i++)
        f[i]=f[i-1]*SEED;
    ll pre=0,tail=0,ans=0;
    for(int i=0;i<len;i++)
    {
        pre=pre*SEED+s[i];
        tail=tail+f[i]*s[i];
        if(pre==tail)
        {
            dp[i+1]=dp[i+1>>1]+1;
            ans+=dp[i+1];
        }
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值