D. Palindrome Degree

题目连接:

http://www.codeforces.com/contest/7/problem/D

Description

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.

Input

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

Output the only number — the sum of the polindrome degrees of all the string's prefixes.

Sample Input

a2A

Sample Output

1

Hint

题意

如果一个数是k回文串的话,那么他是回文串,且他的前半缀是k-1回文串,他的后半缀也是k-1回文串。

然后问你这个所有前缀的回文串等级的和是多少

题解:

跑的时候,维护这个前缀正着的的hash值,这个前缀倒着的hash值。

如果这两个hash值相同的话,说明这个串是一个回文串,那么他的等级d[i]=d[i/2]+1

然后跑一遍就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7+5;
char s[maxn];
int dp[maxn];
long long p1=131,t1=1;
long long p2=127,t2=1;
long long h1=0,rh1=0;
long long h2=0,rh2=0;
int main()
{
scanf("%s",s+1);
long long ans = 0;
int l = strlen(s+1);
for(int i=1;i<=l;i++)
{
h1=h1*p1+s[i];
rh1=s[i]*t1+rh1;
t1=t1*p1; h2=h2*p2+s[i];
rh2=s[i]*t2+rh2;
t2=t2*p2;
if(h1==rh1&&h2==rh2)
dp[i]=dp[i/2]+1;
ans+=dp[i];
}
cout<<ans<<endl;
}

Codeforces Beta Round #7 D. Palindrome Degree hash的更多相关文章

  1. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  2. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  3. Codeforces Beta Round #25 (Div. 2 Only)

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. php中的转义函数

    <?php parse_url 解析URL, 返回各组成部分 urlencode/urldecode url编码/解码 htmlentities 将字符串转化为html实体 htmlentiti ...

  2. 【转载】WebDriver(C#)之十点使用心得

    使用Selenium WebDriver驱动浏览器测试的过程中多多少少会遇到一些折腾人的问题,总结了一部分,做下分享. 一.隐藏元素处理(element not visible) 使用WebDrive ...

  3. css 水平、垂直居中

    水平居中 行内元素 行内元素:(img.span.文字等行内元素),通过在父级元素设置 text-align:center 使元素水平居中. 块级元素 块级元素:(div.p.h1...h6.ul.l ...

  4. angular项目中使用Primeng

    1.第一步把依赖添加到项目中 npm install primeng --save npm install @angular/animations --save npm install font-aw ...

  5. .htaccess教程:简介、访问控制、验证、目录浏览控制

    一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...

  6. chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...

  7. Educational Codeforces Round 25 D - Suitable Replacement(贪心)

    题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...

  8. beego学习笔记(2)

    BEEGO的几个特点: 简单化 RESTful 支持.MVC 模型,可以使用 bee 工具快速地开发应用,包括监控代码修改进行热编译.自动化测试代码以及自动化打包部署. 智能化 支持智能路由.智能监控 ...

  9. WMI技术介绍和应用——WMI概述

    https://blog.csdn.net/breaksoftware/article/details/8424317

  10. 【PAT】1010. 一元多项式求导 (25)

    1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数 ...