A M形字符串
解题思路:哈希。因为已经知道了前缀的长度,所以可以利用hash轻易的求出前缀的对称的hash值,进行比较。
巩固一下hash,
对于一个二进制字符串
0 | 1 | 2 | 3 | 4 | 5 | 5 | |
---|---|---|---|---|---|---|---|
bit | 1 | 2 | 4 | 8 | 16 | 32 | 64 |
二进制 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
a | 1 | 2 | 5 | 10 | 21 | 43 | 86 |
求2~4的二进制的十进制"101",a[4]-a[1]*bit[3],
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e6+10;
const int M = 50005;
const ll base = 131;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double eps = 1e-4;
inline int read(){
int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';ch=getchar();}return x*f;}
inline string readstring(){
string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){
s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){
str+=s;s=getchar();}return str;}
int random(int n){
return (int)(rand()*rand())%n;}
void writestring(string s){
int n = s.size();for(int i = 0;i < n;i++){
printf("%c",s[i]);}}
ll fast_power(ll a,ll p){
ll ret = 1;
while(p){
if(p&1) ret = (ret*a)%mod;
p >>= 1;
a = (a*a)%mod;
}
return ret;
}
char str[N];
ll a[N],b[N];
ll bit[N];
int main(){
//srand((unsigned)time(NULL));
//freopen( "out.txt","w",stdout);
scanf("%s",str+1);
bit[0] = 1;
for(int i = 1;i < N;i++){
bit[i] = (bit[i-1]*base);
}
int n = strlen(str+1);
for(int i = 1;i <= n;i++){
a[i] = a[i-1]*base+(str[i]-97);
}
for(int i = n;i >= 1;i--){
b[i] = b[i+1]*base+(str[i]-97);
}
int ans = 0;
for(int i = 1;i <= n;i++){
if(i+i <= n){
ll x = a[i];
ll p = b[1]-b[i+1]*bit[i];
ll y = a[i+i]-a[i]*bit[i];
ll t = b[i+1]-b[i+i+1]*bit[i];
if(x==y&&x==t&&x==p) ans++;
}
if(i+i-1 <= n){
ll x = a[i];
ll y = a[i+i-1]-a[i-1]*bit[i];
ll t = b[i]-b[i+i]*bit[i];
if(x == y && y == t) ans++;
}
}
cout<<ans<<endl;
return 0;
}