BAPC 2018 Preliminaries I 字符hash

I Isomorphic Inversion

题目描述

Let s be a given string of up to 106 digits. Find the maximal k for which it is possible to partition s into k consecutive contiguous substrings, such that the k parts form a palindrome.
More precisely, we say that strings s0, s1, . . . , sk−1 form a palindrome if si = sk−1−i for all 0 ≤ i < k.
In the first sample case, we can split the string 652526 into 4 parts as 6|52|52|6, and these parts together form a palindrome. It turns out that it is impossible to split this input into more than 4 parts while still making sure the parts form a palindrome.

输入

A nonempty string of up to 106 digits.

输出

Print the maximal value of k on a single line.

样例输入

132594414896459441321

样例输出

9

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull has[1000010], base[1000010];
const int b = 311;///hash 基数  或131
char str[1000010];
int main()
{
    ios;
    cin>>str+1;
    int len=strlen(str+1);
    base[0]=1;
    for(int i=1;i<=len+2;i++)
    {
        base[i]=base[i-1]*b;
    }
    has[0]=0;
    for(int i=1;i<=len;i++)
    {
        has[i]=has[i-1]*b+(str[i]-'0'+1);
    }
    ull s,e;
    ll ans=0;
    int lasts=1,laste=len;
    for(int i=1,j=len;i<=len/2;i++,j--)
    {
        s=has[i]-has[lasts-1]*base[i-lasts+1];
        e=has[laste]-has[j-1]*base[laste+1-j];
        if(s==e)
        {
            ans+=2;
            lasts=i+1;
            laste=j-1;
        }
    }
    if(len%2==0&&lasts>laste)cout<<ans<<endl;
    else cout<<ans+1<<endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值