- 题解
抄吧孩子们
- @ 2026-4-12 18:22:48
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int MAXN = 5e5 + 5;
const ull B = 131;
ull h1[MAXN], h2[MAXN], p[MAXN];
int n;
char s[MAXN];
ull get1(int l, int r) {
return h1[r] - h1[l-1] * p[r-l+1];
}
ull get2(int l, int r) {
return h2[l] - h2[r+1] * p[r-l+1];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> s+1;
p[0] = 1;
for(int i=1; i<=n; i++) p[i] = p[i-1]*B;
for(int i=1; i<=n; i++)
h1[i] = h1[i-1]*B + (s[i]=='0'?1:2);
for(int i=n; i>=1; i--)
h2[i] = h2[i+1]*B + (s[i]=='1'?1:2);
long long ans = 0;
for(int i=1; i<n; i++) {
int l=0, r=min(i, n-i), res=0;
while(l<=r) {
int mid = l+r>>1;
int L = i-mid+1, R = i+mid;
if(L<1 || R>n) { r=mid-1; continue; }
if(get1(L, i) == get2(i+1, R)) {
res = mid;
l = mid+1;
} else r = mid-1;
}
ans += res;
}
cout << ans << '\n';
return 0;
}
0 条评论
目前还没有评论...