个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的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
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<vector>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 5000005
#define num 2005
int n;
const long long N = , M = ;
char ch[maxnum];
long long number[maxnum];
long long sum;
int main()
{
scanf("%s",ch);
int len=strlen(ch);
long long a=,b=,p=;
memset(number,,sizeof(number));
for(int i=;i<len;i++){
a=a*M+ch[i];b+=ch[i]*p;p*=M;
if(a==b) sum+=number[i+]=(number[(i+)/]+);
}
printf("%lld\n",sum);
return ;
}

Palindrome Degree(hash的思想题)的更多相关文章

  1. Codeforces Beta Round #7 D. Palindrome Degree hash

    D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...

  2. codeforces7D Palindrome Degree(manacher&amp;dp或Hsh&amp;dp)

    D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Misha and Palindrome Degree

    Misha and Palindrome Degree 题目链接:http://codeforces.com/problemset/problem/501/E 贪心 如果区间[L,R]满足条件,那么区 ...

  4. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  5. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  6. Palindrome Degree(CodeForces 7D)—— hash求回文

    学了kmp之后又学了hash来搞字符串.这东西很巧妙,且听娓娓道来. 这题的题意是:一个字符串如果是回文的,那么k值加1,如果前一半的串也是回文,k值再加1,以此类推,算出其k值.打个比方abaaba ...

  7. CodeForces 7D Palindrome Degree 字符串hash

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  8. poj1077 Eight【爆搜+Hash(脸题-_-b)】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298840.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  9. UVA11019 Matrix Matcher【hash傻逼题】【AC自动机好题】

    LINK1 LINK2 题目大意 让你在一个大小为\(n*m\)的矩阵中找大小是\(x*y\)的矩阵的出现次数 思路1:Hash hash思路及其傻逼 你把一维情况扩展一下 一维是一个bas,那你二维 ...

随机推荐

  1. springboot的controller使用及url参数的获取

    类上加上@RequestMapping其访问的地址就是类上的加上方法上的菜能访问到该方法,例如上图的地址就是/hello/say @RequestMapping(value = "/hell ...

  2. 百度编辑器(ueditor)@功能之获取坐标

    //获取百度编辑器的工具类 var domUtils = UE.dom.domUtils; //获取编辑器的坐标 var $ueditor_offset = $("#ueditor_0&qu ...

  3. [Mybatis]Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    转自:https://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html Mappe ...

  4. ctci1.3

    ; i < len; i++){         if(str0[i] != str1[i])             return false;     }          return t ...

  5. Kotlin Android Studio 环境搭建

    Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源. Kotlin 可以编译成Java字节码,也可以编 ...

  6. BT5 set_config各个选项的配置

    最近研究了一下bt5的社会工程学工具SET,本来国内的bt5的资料就很少了,详细分析SET的资料就更少了,在各大网站找了找,都不靠谱,还是得自力更生啊,我在这里就把自己的过程写下来,希望对大家有点帮助 ...

  7. POJ 3167 Cow Pattern ★(KMP好题)

    题意 给你一个数字序列S,再给一个数字序列pattern,S和pattern中的数字都是1到s(s<=25).每个序列里的数字都有个排名,也就是第几小,现在我们要用pattern来匹配S.在本题 ...

  8. magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案

    在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...

  9. 使用libimobiledevice + ifuse提取iOS沙盒文件

    简介 libimobiledevice:一个开源包,可以让Linux支持连接iPhone/iPod Touch等iOS设备. Git仓库: https://github.com/libimobiled ...

  10. log4cpp之Category

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...