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 ...
随机推荐
- bootstrap分页查询传递中文参数到后台(get方式提交)
<!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...
- Cookie、Session 和 自定义分页
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- 实现UE添加自定义按钮之添加菜单
1.ueditor.config.js配置文件中配置 2.在ueditor.all.js配置文件中配置点开的的弹框位置 3.在ueditor1_4_3-utf8-jsp\themes\default\ ...
- HDU 6198 2017沈阳网络赛 线形递推
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...
- KVC, KVO 实现原理
Key-Value Coding: 键值编码 (KVC) 方法调用: // 对象属性 // 类似: Person -> name setValue: forKey: // 对象的属性或者 属性的 ...
- python脚本-实现自动按规则创建指定大小和指定个数的文件案例
# -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...
- 在Ubuntu上使用pip安装错误 read timed out 处理方法
在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.
- Webcollector应用(一)
webcollector是一个开源的Java网络爬虫框架.最近的爬虫改用java写了,对这一周的工作进行简要总结.对于内部机制了解不深入,主要侧重在应用. 一.环境搭建 需要安装一个webcollec ...
- scrollreveal(页面滚动显示动画插件支持手机)
scrollreveal.js是一款可以轻易实现桌面和移动浏览器元素随页面滚动产生动画的js插件.该插件通过配置可以在页面滚动,元素进入视口时产生炫酷的动画效果,同时还支持元素的3D效果,非常的实用. ...
- .Net Core 部署到 CentOS7 64 位系统中的步骤
建议使用 root 管理员账户操作 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务器一开机就启动服务器 上发 ...