Description
对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串。比如00001111和010101就是反对称的,1001就不是。
现在给出一个长度为N的01字符串,求它有多少个子串是反对称的。
Solution
百度搜回文自动机,出来的怎么tm全是Manacher啊
跟普通Manacher差不多,只是将相同、不同交换而已。
Code
// luogu-judger-enable-o2
/************************************************
* Au: Hany01
* Date: Jun 22nd, 2018
* Prob: [BZOJ2084][POI2010] Antisymmetry
* Email: hany01@foxmail.com
* Institute: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 500005;
int n, p[maxn << 1], len, mx, id;
char s[maxn << 1], s_[maxn];
LL Ans;
int main()
{
#ifdef hany01
File("bzoj2084");
#endif
n = read(), scanf("%s", s_ + 1);
s[++ len] = '#';
For(i, 1, n) s[++ len] = '$', s[++ len] = s_[i];
s[++ len] = '$', s[++ len] = '!';
For(i, 1, len) {
if (s[i] != '$') continue;
p[i] = i <= mx ? min(mx - i, p[(id << 1) - i]) : 0;
while (s[i - p[i] - 1] + s[i + p[i] + 1] == 48 + 49 || (s[i - p[i] - 1] == '$' && s[i + p[i] + 1] == '$')) ++ p[i];
if (chkmax(mx, i + p[i])) id = i;
if (!(p[i] & 1)) Ans += p[i] >> 1;
}
printf("%lld\n", Ans);
return 0;
}
//浮云蔽白日,游子不顾返。
// -- 佚名《行行重行行》