K. Palindromization、字符串hash

题目链接、

 

队友非得考考我字符串hash,然后就写了下、好久没写有些生疏了

居然忘记了将两个字符串合起来hash怎么变的、

这题就一个枚举去掉点的位置,然后就是双hash两边(正着一边倒着一边)就完了,没啥坑点

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ull,ull> pull;
inline pull operator - (pull a,pull b){return make_pair((a.first -  b.first),(a.second - b.second));}
inline pull operator * (pull a,pull b){return make_pair((a.first *  b.first),(a.second * b.second));}
inline pull operator + (pull a,pull b){return make_pair((a.first +  b.first),(a.second + b.second));}
inline pull operator + (pull a,int  b){return make_pair((a.first +  b      ),(a.second + b       ));}
const int maxn = 2e5+7;
const pull p{131LL,13331LL},one{1LL,1LL},zero{0LL,0LL};
char s[maxn];
pull Hsh[maxn],RHsh[maxn],Pw[maxn];
pull Get(int l,int r){
    return Hsh[r]-Hsh[l-1]*Pw[r-l+1];
}
pull invGet(int l,int r){
    return RHsh[l]-RHsh[r+1]*Pw[r-l+1];
}
signed main(){
    scanf("%s",s+1);
    int n = strlen(s+1);
    RHsh[n+1] = zero,Hsh[0] = zero,Pw[0] = one;
    for(int i=1;i<=n;i++)Hsh[i] = Hsh[i-1]*p+s[i],Pw[i] = Pw[i-1]*p;
    for(int i=n;i   ;i--)RHsh[i] = RHsh[i+1]*p+s[i];
    int flag = 0;
    if(Get(2,n) == invGet(2,n))
        return 0*printf("YES\n1\n");
    if(Get(1,n-1) == invGet(1,n-1))
        return 0*printf("YES\n%d\n",n);
    for(int i=2;i<n;i++){
        pull a = Get(1,i-1)*Pw[n-i]+Get(i+1,n),b = invGet(1,i-1)+invGet(i+1,n)*Pw[i-1];
        if(a == b)
            return 0*printf("YES\n%d\n",i);
    }
    printf("NO\n");
    return 0;
}
/**
7 2
1 3 9 5 2 16 8
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值