Codeforces Beta Round #7 D. Palindrome Degree

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

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 exceed5·106. The string is case-sensitive.

Output

Output the only number — the sum of the polindrome degrees of all the string's prefixes.

Sample test(s)
input
a2A
output
1
input
abacaba
output
6


哈希判断回文。。

/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 19日 星期六 10:18:00 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
	int ret=0;bool isN=0;char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') isN=1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		ret=ret*10+c-'0';
		c=getchar();
	}
	return isN?-ret:ret;
}

inline void output(ll x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[20];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  

const ll mod=1000000007;
const ll base=137;
const int N=5000005;
char a[N],b[N];
int vis[N];
ll p[N],h[N],rh[N];
ll tmp[N];
bool inq[N];
int l;

inline void init(){
	p[0]=1;
	rep(i,1,N) p[i]=p[i-1]*base%mod;
	rep(i,0,l) b[l-i-1]=a[i];
}

inline void hash(char *s,ll *ha){
	ha[0]=s[0];
	rep(i,1,l){
		ha[i]=ha[i-1]+s[i]*p[i];
		ha[i]%=mod;
	}
}

inline ll get(int s,int t,ll *h){
	if(s==0) return h[t];
	else return (h[t]-h[s-1]+mod)%mod;
}

inline bool is_Palidrome(int a,int b,int c,int d){
	ll h1=get(a,b,h);
	ll h2=get(l-d-1,l-c-1,rh);
	return h1*p[l-d-1]%mod == h2*p[a]%mod;
}

inline ll solve(){
	ll ans=1;
	tmp[0]=1;
	rep(i,1,l){
		int a=0;
		int b=(i+1)/2-1;
		int c=(i%2==1?b+1:b+2);
		int d=i;
		if(is_Palidrome(a,b,c,d)){
			tmp[i]=tmp[b]+1;
			ans+=tmp[i];
			//printf("%d %d\n",i,tmp[i]);
		}
	}
	return ans;
}

int main(){
	scanf("%s",a);l=strlen(a);
	init();clr(vis,0);
	hash(a,h),hash(b,rh);
	output(solve());
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值