个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的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. gulp+es6构建页面

    遇到的问题: 1.es6如何使用,定义一个demo.js //demo.js export default class demo { // 构造函数 constructor(){ //在构造函数中调用 ...

  2. 动态延迟加载网页元素jQuery插件scrollLoading

    如果一个网页很长,那么该页面的加载时间也会相应的较长.而这里给大家介绍的这个jQuery插件scrollLoading的作用则是,对页面元素进行动态加载,通俗的说就是滚到哪就加载到哪,屏幕以下看不见的 ...

  3. Ghost:一款简约风格博客系统

    前言 本文将介绍一种最快速的创建Ghost博客系统的方法,并实现绑定二级域名到该博客系统.本文以本博客的“微博客”为例. 一键创建Ghost博客系统 Kite 是 Ghost 博客托管商,网址为:ht ...

  4. Docker总结

    Docker总结 一.Docker简介 1.问题:为什么会有docker出现 一款产品从开发到上线,从操作系统,到运行环境,再到应用配置.作为开发+运维之间的协作我们需要关心很多东西,这也是很多互联网 ...

  5. 1-15-2-RAID10 企业级RAID磁盘阵列的搭建(RAID1、RAID5、RAID10)

    RAID10的搭建: 有两种方法, 第一种:直接使用四块磁盘,创建级别为10的磁盘阵列 第二种:使用四块磁盘先创建两个RAID1,然后在用RAID1创建RAID0 第一步:添加五个磁盘到虚拟机 开机后 ...

  6. 奇怪的表达式求值 (java实现)

    题目参考:http://blog.csdn.net/fuxuemingzhu/article/details/68484749 问题描述; 题目描述: 常规的表达式求值,我们都会根据计算的优先级来计算 ...

  7. UVALive-4287 Proving Equivalences (有向图的强连通分量)

    题目大意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 题目分析:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图. ...

  8. 使用Easy4net编写代码生成器

    在项目中经常要手动创建和数据库对应的实体类,如果数据库表比较多或者表字段比较多,那会是一个工作量非常大的事情,所以我根据自己的需求写了一个简单的代码生成工具,工具使用Easy4net框架开发. 下面是 ...

  9. webstrom 配置eslint 自动修复错误

    1.展示效果 如何给vue项目添加eslint呢,如何自动修复呢? 先展示一下效果: 2.配置步骤 1.安装插件ESLint plugin webstrom 其实有个非常好用的插件,ESLint pl ...

  10. 利用CSS变量实现悬浮效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...