显然只有偶数长度的串符合题意,并且如果一个串符合题意,那么从其首尾各截掉一个字符也符合题意。

于是枚举中心,二分可以向左右扩展的最远距离,累计答案。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 500001
typedef unsigned long long ull;
const ull seed=3;
ull seeds[N],pre[N],rsuf[N],ans;
int n;
char s[N],s2[N];
ull Get(int l,int r){return pre[r]-pre[l-1]*seeds[r-l+1];}
ull Get2(int l,int r){return rsuf[l]-rsuf[r+1]*seeds[r-l+1];}
bool check(int x,int len){return Get(x-len+1,x+len)==Get2(x-len+1,x+len);}
void solve(int x)
{
int l=0,r=min(x+1,n-x-1);
while(r>l)
{
int mid=(l+r+1>>1);
if(check(x,mid)) l=mid;
else r=mid-1;
}
ans+=(ull)l;
}
int main()
{
scanf("%d%s",&n,s);
seeds[0]=1;
for(int i=1;i<=n;++i) seeds[i]=seeds[i-1]*seed;
for(int i=0;i<n;++i) pre[i]=pre[i-1]*seed+(s[i]-'0');
for(int i=0;i<n;++i) s2[i]=(s[i]=='1'?'0':'1');
for(int i=n-1;i>=0;--i) rsuf[i]=rsuf[i+1]*seed+(s2[i]-'0');
for(int i=0;i<n-1;++i)
solve(i);
cout<<ans<<endl;
return 0;
}

【二分答案】【字符串哈希】bzoj2084 [Poi2010]Antisymmetry的更多相关文章

  1. [BZOJ2084][Poi2010]Antisymmetry 二分+hash

    2084: [Poi2010]Antisymmetry Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 812  Solved: 503[Submit] ...

  2. BZOJ2084: [Poi2010]Antisymmetry

    2084: [Poi2010]Antisymmetry Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 187  Solved: 125[Submit] ...

  3. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

  4. 【哈希 二分】bzoj2084: [Poi2010]Antisymmetry

    可以用manacher或者SA搞过去的:非常有趣的hash题 Description 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如0000 ...

  5. UOJ219 NOI2016 优秀的拆分 二分、字符串哈希

    传送门 题目可以转化为求\(AA\)的数量,设\(cnt1_x\)表示左端点为\(x\)的\(AA\)的数量,\(cnt2_x\)表示右端点为\(x\)的\(AA\)的数量,那么答案就是\(\sum ...

  6. BZOJ2084[Poi2010]Antisymmetry——回文自动机

    题目描述 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是.现在给出一个长度为N的0 ...

  7. BZOJ2084 [Poi2010]Antisymmetry Manachar

    题目传送门 - BZOJ2084 题解 对于一个0我们把它看作01,1看作10,然后只要原串中的某个子串可以通过这两个变换成为回文串就可以满足条件了. 对于转换过的串,Manachar随便弄几下就可以 ...

  8. BZOJ4755 JSOI2016扭动的回文串(二分答案+哈希)

    显然答案应该是由单串以某位置为中心的极长回文串继续在另一个串里拓展得到的.枚举中间位置二分答案,哈希判断即可.注意考虑清楚怎么处理偶回文,比如像manacher一样加分隔符. #include< ...

  9. 【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串

    二分答案,然后搞出hash值扔到哈希表里.期望复杂度O(n*log(n)). <法一>next数组版哈希表 #include<cstdio> #include<cstri ...

随机推荐

  1. HttpClient测试类请求端和服务端即可能出现乱码的解决

    junit HttpClient 请求端 代码: package com.taotao.httpclient; import java.util.ArrayList; import java.util ...

  2. HTML5之SVG详解(一):基本概括

    转载自:http://www.cnblogs.com/hupeng/archive/2012/12/21/2828456.html 1.背景 SVG是Scalable Vector Graphics的 ...

  3. javascript简易下拉菜单效果

    JS代码: window.onload=function(){ var oDiv=document.getElementById('navMenu'); var aUl=oDiv.getElement ...

  4. centos yum 安装 mysql

      centos7下使用yum安装mysql 时间:2015-03-07 21:26:20      阅读:87445      评论:0      收藏:1      [点我收藏+] 标签: Cen ...

  5. HDU 5881--Tea 思维规律

    感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...

  6. 【Foreign】划分序列 [线段树][DP]

    划分序列 Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 9 4 ...

  7. 【BZOJ4657】tower [网络流]

    炮塔 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output 一行一个整数表示答案. Sample Input 4 5 0 ...

  8. hdu 1686 Oulipo (kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...

  9. a标签里文本居中

    text-align:center; height: 30px; line-height:30px;

  10. For循环中不可以嵌套RDD操作

    今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...