反对称字符串
作者:
请叫我皮皮神
,
2024-04-06 21:27:32
,
所有人可见
,
阅读 10
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int N = 1e6 , P = 131;
ULL p[N], h[N], g[N];//前缀哈希 后缀哈希
char s[N], t[N];//原字符串 取反字符
int n;
ULL res = 0;
/*ULL get(int l ,int r ,bool flag)
{
if(flag)
return h[r] - h[l-1] * P[r - l + 1];
}*/
void fun(int i)
{
int l = 0, r = i; // r = min( i , n - i );
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (h[i] - h[i - mid] * p[mid] == g[i + 1] - g[i + 1 + mid] * p[mid])
l = mid;
else r = mid - 1;
}
res += l;
}
// 字符串哈希
void init()
{
p[0] = 1;
for(int i = 1; i <= n ; i++)
{
h[i] = h[i-1] * P + s[i];
p[i] = p[i-1] * P;
}
}
int main()
{
cin >> n;
scanf("%s", s + 1);
init();
// 取反
for (int i = 1; i <= n; i ++) t[i] = s[i] == '1' ? '0' : '1';
for (int i = n; i >= 1; i --) g[i] = g[i + 1] * P + t[i]; // 后缀哈希
for (int i = 1; i < n; i ++) fun(i);
cout << res << endl;
return 0;
}