思路:正反预处理字符串的哈希值用数组存下前缀和,枚举要消掉的字符即可。
# include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 2e5+3;
char s[N];
ull a[N], b[N],mod = 1e9+7;
int main()
{
int len, f=0;
ull pow=1, seed=23;
scanf("%s",s+1);
len = strlen(s+1);
for(int i=1; i<=len; ++i)
{
a[i] = (a[i-1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
pow = 1;
for(int i=len; i>=1; --i)
{
b[i] = (b[i+1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
for(int i=1; i<=len; ++i)
{
ull l = ((a[i-1]*seed)%mod + (a[len]-a[i]+mod)%mod)%mod;
ull r = ((b[i+1]*seed)%mod + (b[1]-b[i]+mod)%mod)%mod;
if(l==r)
{
f = 1;
printf("YES\n%d\n",i);
break;
}
}
if(!f) puts("NO");
return 0;
}