# LOJ#2452. 「POI2010」反对称 Antisymmetry

## 样例

#### 样例输入

8
11001011


#### 样例输出

7


## 数据范围与提示

Translated by vincent163

## 题解

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 500010;
const ull base = 13131;

ull h1[N], h2[N], p[N];
int n;
ll ans = 0;
char s[N];

ull get_h1(int l, int r) { return h1[r] - h1[l - 1] * p[r - l + 1]; }
ull get_h2(int l, int r) { return h2[l] - h2[r + 1] * p[r - l + 1]; }

int check(int x) {
int l = 1, r = min(x, n - x);
while(l <= r) {
int mid = (l + r) >> 1;
if(get_h1(x - mid + 1, x) == get_h2(x + 1, x + mid)) l = mid + 1;
else r = mid - 1;
}
return r;
}

int main() {
scanf("%d%s", &n, s + 1); p[0] = 1;
for(int i = 1; i <= n; ++i) p[i] = p[i - 1] * base, h1[i] = h1[i - 1] * base + (ull)s[i];
for(int i = n; i; --i) h2[i] = h2[i + 1] * base + (ull)(s[i] == '0' ? s[i] + 1 : s[i] - 1);
for(int i = 1; i < n; ++i) ans += check(i);
printf("%lld\n", ans);
}

posted @ 2019-01-25 18:02  henry_y  阅读(676)  评论(0编辑  收藏  举报