Palindrome Degree(hash的思想题)

个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的key值一样就一定是回文串,

那么问题来了,正向还能保证一遍遍历,逆向呢,卡住我了,后面发现网上大神的秦九韶算法用上了,厉害了。

关于哈希,这题用的是最简单的哈希思想,就是用M取合理的素数,这题取得是3,

然后正向就是 a+=a*M+ch[i]。逆向用秦九韶可以在循环的时候算出来,

举个例子比如三个值1,2,3。

假如逆向开始 则 (((0+s[3])*M+s[2])*M+s[1]);化简就等于s[1]+s[2]*M+s[3]*M^2;

哇,规律就来了,那么在正向就能确定逆向的值,服气服气

题目:

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.

Example

Input
a2A
Output
1
Input
abacaba
Output
6
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<set>
 5 #include<vector>
 6 #include<cstring>
 7 #include<iomanip>
 8 #include<algorithm>
 9 using namespace std;
10 #define inf 1<<29
11 #define nu 4000005
12 #define maxnum 5000005
13 #define num 2005
14 int n;
15 const long long N = 13, M = 3;
16 char ch[maxnum];
17 long long number[maxnum];
18 long long sum;
19 int main()
20 {
21     scanf("%s",ch);
22     int len=strlen(ch);
23     long long a=0,b=0,p=1;
24     memset(number,0,sizeof(number));
25     for(int i=0;i<len;i++){
26         a=a*M+ch[i];b+=ch[i]*p;p*=M;
27         if(a==b) sum+=number[i+1]=(number[(i+1)/2]+1);
28     }
29     printf("%lld\n",sum);
30     return 0;
31 }

 

 



 

posted @ 2017-12-02 18:25  余生漫漫浪  阅读(452)  评论(0编辑  收藏  举报