题目链接、
队友非得考考我字符串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
*/