Description
对于一个01串,如果将这个串按位取反再将整个串反过来后和原串一样,它就被称作“反对称”字符串。
比如00001111和010101就是反对称的,1001就不是。
现在给出一个长度为N的01字符串,求它有多少个反对称字符串。
Input
第一行一个正整数N (N <= 500,000)。
第二行一个长度为N的01字符串。
Output
反对称子串的个数
Sample Input Copy
6
010101
Sample Output Copy
9
PS:刚刚掐着时间过的。
#include<bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
const int N = 500010;
int n;
char s[2*N];
int p[2*N];
void manacher()
{
int r =0,flag=0;
int ans=0;
for(int i=1; i<=n; i++)
{
p[i] = 1;
while(s[i+p[i]]!=s[i-p[i]]||s[i+p[i]]=='#'||s[i-p[i]]=='#')
{
if(s[i-p[i]]=='@'||s[i+p[i]]=='@') break;
if(s[i+p[i]]=='#')
{
p[i]++;
continue;
}
if(s[i+p[i]]!='#'&&s[i]=='#') ans++;
p[i]++;
}
}
cout<<ans<<'\n';
}
int main()
{
cin>>n;
n = 2*n+1;
for(int i=2; i<=n; i+=2)
{
cin>>s[i];
s[i+1] = '#';
}
s[0] = '@';
s[1] = '#';
s[n+1] = '@';
manacher();
return 0;
}