洛谷题目链接:[POI2010]ANT-Antisymmetry

题目描述

Byteasar studies certain strings of zeroes and ones.

Let be such a string. By we will denote the reversed (i.e., "read backwards") string , and by we will denote the string obtained from by changing all the zeroes to ones and ones to zeroes.

Byteasar is interested in antisymmetry, while all things symmetric bore him.

Antisymmetry however is not a mere lack of symmetry.

We will say that a (nonempty) string is antisymmetric if, for every position in , the -th last character is different than the -th (first) character.

In particular, a string consisting of zeroes and ones is antisymmetric if and only if .

For example, the strings 00001111 and 010101 are antisymmetric, while 1001 is not.

In a given string consisting of zeroes and ones we would like to determine the number of contiguous nonempty antisymmetric fragments.

Different fragments corresponding to the same substrings should be counted multiple times.

对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串。比如00001111和010101就是反对称的,1001就不是。

现在给出一个长度为N的01字符串,求它有多少个子串是反对称的。

输入输出格式

输入格式:

The first line of the standard input contains an integer

() that denotes the length of the string.

The second line gives a string of 0 and/or 1 of length .

There are no spaces in the string.

输出格式:

The first and only line of the standard output should contain a single integer, namely the number of contiguous (non empty) fragments of the given string that are antisymmetric.

输入输出样例

输入样例#1:

8

11001011

输出样例#1:

7

说明

7个反对称子串分别是:01(出现两次),10(出现两次),0101,1100和001011

简述一下题意: 给出一个01串,要求出其中反对称的回文字串的个数(就是将该字串反转后每一位都不相同).


因为要求回文串的个数,所以我们可以考虑用manacher来做.我们知道,manacher算法是用来求最长回文的.那么我们要怎么样才能求出回文个数呢?其实很简单,就在处理回文串半径的时候将每个回文的半径都加入答案中就可以了.另外要注意一下只能记录长度为偶数的回文字串,因为如果是奇数长度的回文串一定不满足题意(想一下为什么).

#include<bits/stdc++.h>
using namespace std;
const int N=10000000+5;
typedef long long lol; int n, cnt, p[N*2];
lol ans = 0;
char s[N], ss[N*2], c[1000]; void init(){
cnt = 1; ss[0] = '$', ss[cnt] = '#';
for(int i=1;i<=n;i++)
ss[++cnt] = s[i], ss[++cnt] = '#';
c['0'] = '1', c['1'] = '0', c['#'] = '#';
ss[cnt+1] = '*';
} void manacher(){
int id = 0, mx = 0;
for(int i=1;i<=cnt;i++){
if(i <= mx) p[i] = min(p[id*2-i],mx-i);
else p[i] = 1;
while(ss[i+p[i]] == c[ss[i-p[i]]]) p[i]++;
if(mx < i+p[i]) mx = i+p[i], id = i;
}
for(int i=1;i<=cnt;i+=2)
ans += (p[i]-1)/2;
} int main(){
//freopen("ghost.in","r",stdin);
//freopen("ghost.out","w",stdout);
cin >> n; scanf("%s",s+1);
init(); manacher();
printf("%lld\n",ans);
return 0;
}

最新文章

  1. js日期加减
  2. Jena TDB assembler syntax
  3. wait(0)
  4. having count(*) &gt; 1
  5. 前端,移动开发者,UI须懂: 不同设备的之间的尺寸
  6. 批量的单向的ssh 认证
  7. android layout物业介绍
  8. ural 1119. Metro(动态规划)
  9. 使用oracle数据库开发,异常总结
  10. MyBatis 插入时返回自增主键
  11. 简单聊聊Storm的流分组策略
  12. [C#]使用控制台获取天气预报
  13. 轻量级网络库libevent初探
  14. MySQL出现too many connections(1040)错误解决方法
  15. elk-logstash-kibana(三)
  16. java技术突破要点
  17. Git-简单的利用SourceTree提交代码
  18. php新手第一次安装mongo
  19. asp.net core webapi项目配置全局路由
  20. unexpected token: * 和 java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 解决办法

热门文章

  1. Python的入坑之路(1)
  2. 2016.01.04接触spring一年开始读spring源码
  3. Apache 配置说明
  4. 【多校联合】(HDU6043)KazaQ's Socks
  5. 在测试时用到的一些mysql的小技巧(持续更新)
  6. python3.x 编码问题
  7. ThreadPool线程池的几种姿势比较
  8. 梳理 Opengl ES 3.0 (三)顶点坐标变换
  9. 从源码安装opencv
  10. php自学笔记2