Codeforces Beta Round #7 D. Palindrome Degree hash
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的更多相关文章
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- Codeforces Beta Round #25 (Div. 2 Only)
Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- 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]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
随机推荐
- 深入理解Spring系列之七:web应用自动装配Spring配置
转载 https://mp.weixin.qq.com/s/Lf4akWFmcyn9ZVGUYNi0Lw 在<深入理解Spring系列之一:开篇>的示例代码中使用如下方式去加载Spring ...
- 常见的bug
常见bug 一. Android系统功能测试设计的测试用例: a.对所测APP划分模块 b.详细列出每个模块的功能点(使用Xmind绘制功能图) c.使用等价类划分.边界值.场景法等对各功能点编写测试 ...
- linux内存分配方法总结【转】
转自:http://www.bkjia.com/Linuxjc/443717.html 内存映射结构: 1.32位地址线寻址4G的内存空间,其中0-3G为用户程序所独有,3G-4G为内核占有. 2.s ...
- Android的休眠与唤醒
Android 休眠(suspend),在一个打过android补丁的内核中,state_store()函数会走另外一条路,会进入到request_suspend_state()中,这个文件在earl ...
- 新装linux系统最基本设置
1,dns设置:vi /etc/resolv.conf 添加内容:nameserver 8.8.8.8 2,vi设置: vi ~/.bashrc 添加内容:alias vi='vim': 然后执 ...
- Machine Learning系列--隐马尔可夫模型的三大问题及求解方法
本文主要介绍隐马尔可夫模型以及该模型中的三大问题的解决方法. 隐马尔可夫模型的是处理序列问题的统计学模型,描述的过程为:由隐马尔科夫链随机生成不可观测的状态随机序列,然后各个状态分别生成一个观测,从而 ...
- 多播知识by 陈胜君
简单的讲一下多拨的说明:一.多拨分物理多拨和虚拟多拨. 物理多拨是电信老套餐,就是一个宽带支持四个内网设备同时拨号上网,即2004年以前,允许家里四台电脑直接连LAN网口启动拨号,同时允许四拨在线.现 ...
- 制作一棵ztree
我们在做web项目时,常会用到一些树形菜单.在此,我们利用ztree实现树形菜单的效果.zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTr ...
- 开源介绍:Google Guava、Google Guice、Joda-Time
一.Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency lib ...
- git配置用户名跟邮箱
因为我有两个git账号 所以我现在要改变我的默认用户名跟邮件 我就需要去终端设置用户名跟邮箱 具体的命令行就是 设置git的用户名 git config --global user.name &quo ...