「BZOJ2084」[POI2010] Antisymmetry
Description
对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串。比如00001111和010101就是反对称的,1001就不是。
现在给出一个长度为N的01字符串,求它有多少个子串是反对称的。
Input
第一行一个正整数N (N <= 500,000)。第二行一个长度为N的01字符串。
Output
一个正整数,表示反对称子串的个数。
Sample Input
8
11001011
11001011
Sample Output
7
hint
7个反对称子串分别是:01(出现两次), 10(出现两次), 0101, 1100和001011
hint
7个反对称子串分别是:01(出现两次), 10(出现两次), 0101, 1100和001011
题解
枚举中点,然后二分+哈希求能匹配的最长距离。。。
这个好像是manacher裸题
hzwer是个不会hash的傻X,所以他要学习一下T T
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include<set> #include<map> #include<ctime> #include<queue> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 1000000000 #define pa pair<int,int> #define ll long long  #define mod 1000000007 using namespace std; ll read() { 	ll x=0,f=1;char ch=getchar(); 	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 	return x*f; } ll tot; int n; ll s1[500005],s2[500005],I[500005]; char ch[500005]; int query(int f,int l,int r) { 	ll t; 	if(!f)t=s1[r]-s1[l-1]*I[r-l+1]; 	else t=s2[l]-s2[r+1]*I[r-l+1]; 	t=(t%mod+mod)%mod; 	return t; } void solve(int x) { 	int l=1,r=min(x,n-x),ans=0; 	while(l<=r) 	{ 		int mid=(l+r)>>1; 		if(query(0,x-mid+1,x)==query(1,x+1,x+mid)) 			ans=mid,l=mid+1; 		else r=mid-1; 	} 	tot+=ans; } int main() { 	n=read(); 	scanf("%s",ch+1); 	I[0]=1;for(int i=1;i<=n;i++)I[i]=I[i-1]*149%mod; 	for(int i=1;i<=n;i++) 	{ 		s1[i]=s1[i-1]*149+ch[i]-'0'; 		s1[i]%=mod; 	} 	for(int i=n;i>=1;i--) 	{ 		s2[i]=s2[i+1]*149+((ch[i]-'0')^1); 		s2[i]%=mod; 	} 	for(int i=1;i<=n;i++) 		solve(i); 	printf("%lld",tot); 	return 0; } | 
 
			
%%%
跪求黄学长qq