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分页类分享

    /** * 获取分页的HTML内容 * @param integer $page 当前页 * @param integer $pages 总页数 * @param string $url 跳转url地 ...

  2. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  3. python基础===利用unittest进行测试用例执行的几种方式

    利用python进行测试时,测试用例的加载方式有2种:  一种是通过unittest.main()来启动所需测试的测试模块:  一种是添加到testsuite集合中再加载所有的被测试对象,而tests ...

  4. 在新版linux上编译老版本的kernel出现kernel/timeconst.h] Error 255

    在使用ubuntu16.4编译Linux-2.6.31内核时出现这样的错误 可以修改timeconst.pl的内容后正常编译. 以下是编译错误提示的内容: Can't use 'defined(@ar ...

  5. Ubuntu 14.04 开机手动开启numlock led

    在windows开机后,数字键盘灯是亮着的,但是当切换到ubuntu系统后登录用户名和密码时,如果你设定的有数字,都要先打开数字键盘区NUMLOCK键,然后再输入了,很不方便. 首先,需要安装一个小软 ...

  6. sqlalchemy更新和插入操作

    def save_app_info(self): try: # update app_info print(self.dicts) data = db_session.query(App_Info). ...

  7. 大数据系列之分布式数据库HBase-1.2.4+Zookeeper 安装及增删改查实践

    之前介绍过关于HBase 0.9.8版本的部署及使用,本篇介绍下最新版本HBase1.2.4的部署及使用,有部分区别,详见如下: 1. 环境准备: 1.需要在Hadoop[hadoop-2.7.3]  ...

  8. openjudge-NOI 2.6基本算法之动态规划 专题题解目录

    1.1759 最长上升子序列 2.1768 最大子矩阵 3.1775 采药 4.1808 公共子序列 5.1944 吃糖果 6.1996 登山 7.2000 最长公共子上升序列 8.2718 移动路线 ...

  9. 设计模式之笔记--代理模式(Proxy)

    代理模式(Proxy) 定义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 类图 描述 Subject,定义了ConcreteSubject和Proxy的共用接口,这样就可以 ...

  10. Java Web 远程调试

    Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...