1. 程式人生 > >CodeForces 7D Palindrome Degree 字符串hash

CodeForces 7D Palindrome Degree 字符串hash

esp vector ret http stream math.h i++ turn iostream

題目鏈接:點擊打開鏈接





#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<queue>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 5001000
#define mod 1000000007
#define hehe 137731735
#define ll __int64
ll n;
char s[N];
ll x[N], y[N];
ll dp[N];
int main(){
	ll i,j;
	while(gets(s)) {
		dp[0] = 0;
		for(i=0;s[i];i++) {
			if(‘0‘<=s[i]&&s[i]<=‘9‘)
				s[i] = s[i]-‘0‘;
			else if(‘a‘<=s[i]&&s[i]<=‘z‘)
				s[i] = s[i]-‘a‘+10;
			else s[i] = s[i]-‘A‘+36;
		}
		ll len = i;
		x[0] = 0;
		ll dou = 1;
		for(i=1;i<=len;i++){ x[i] = (x[i-1]+s[i-1]*dou)%mod; dou = (dou*hehe)%mod; }
		y[len+1] = 0;
		for(i=1;i<=len;i++) { y[i] = (y[i-1]*hehe+s[i-1])%mod;}
		ll ans = 0;
		for(i=1;i<=len;i++) if(x[i]==y[i]) {
			dp[i] = dp[i>>1]+1;
			ans += dp[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


CodeForces 7D Palindrome Degree 字符串hash