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

然后跑一遍就好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e7+5;
  4. char s[maxn];
  5. int dp[maxn];
  6. long long p1=131,t1=1;
  7. long long p2=127,t2=1;
  8. long long h1=0,rh1=0;
  9. long long h2=0,rh2=0;
  10. int main()
  11. {
  12. scanf("%s",s+1);
  13. long long ans = 0;
  14. int l = strlen(s+1);
  15. for(int i=1;i<=l;i++)
  16. {
  17. h1=h1*p1+s[i];
  18. rh1=s[i]*t1+rh1;
  19. t1=t1*p1;
  20. h2=h2*p2+s[i];
  21. rh2=s[i]*t2+rh2;
  22. t2=t2*p2;
  23. if(h1==rh1&&h2==rh2)
  24. dp[i]=dp[i/2]+1;
  25. ans+=dp[i];
  26. }
  27. cout<<ans<<endl;
  28. }

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经典实例 <?php function dirSize($dir){ //定义大小初始值 $sum=; //打开 $dd=opendir($dir); //遍历 while ...

  2. spring-boot-资源处理

    WebMvcConfigurerAdapter 使用 1.实现 HandlerInterceptorAdapter 2.添加拦截器 重写WebMvcConfigurerAdapter中的addInte ...

  3. TCP 传输控制协议(转)

    开头先说几个协议: IP:网际协议 TCP:传输控制协议 Http:超文本传输协议 AMQP:高级消息队列协议 一:TCP是什么? TCP(Transmission Control Protocol ...

  4. Linux内核中的队列 kfifo【转】

    转自:http://airekans.github.io/c/2015/10/12/linux-kernel-data-structure-kfifo#api 在内核中经常会有需要用到队列来传递数据的 ...

  5. MySQL启动很慢的原因

    我们在启动MySQL的时候,常常会遇到的是, 当执行启动命令后,它会"Start MySQL ....." 一直不停的执行,也不中断,也不成功 这里会出现此现象的原因有以下三条: ...

  6. Redis安装和客户端cli常见操作

    安装Redis $ wget http://download.redis.io/releases/redis-4.0.6.tar.gz $ tar xzf redis-4.0.6.tar.gz $ c ...

  7. Linux Supervisor的安装与使用入门---SuSE

    Linux Supervisor的安装与使用入门 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事 ...

  8. PC端网站跳转手机端网站

    <SCRIPT LANGUAGE="JavaScript"> function mobile_device_detect(url) { var thisOS=navig ...

  9. HDU 17111 Number Sequence(KMP裸题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:给你两个数字数组a和b,若b是a的子序列则输出b在a中第一次出现的位置,否则输出-1. ...

  10. thinkphp5 返回数组提示variable type error: array

    浏览器访问控制器函数,而函数返回的是数组: function timeArr(){ $time = array(); for($i=1;$i<=7;$i++){ $d= date('d',Tim ...