洛谷题目链接:[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算法是用来求最长回文的.那么我们要怎么样才能求出回文个数呢?其实很简单,就在处理回文串半径的时候将每个回文的半径都加入答案中就可以了.另外要注意一下只能记录长度为偶数的回文字串,因为如果是奇数长度的回文串一定不满足题意(想一下为什么).

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=10000000+5;
  4. typedef long long lol;
  5. int n, cnt, p[N*2];
  6. lol ans = 0;
  7. char s[N], ss[N*2], c[1000];
  8. void init(){
  9. cnt = 1; ss[0] = '$', ss[cnt] = '#';
  10. for(int i=1;i<=n;i++)
  11. ss[++cnt] = s[i], ss[++cnt] = '#';
  12. c['0'] = '1', c['1'] = '0', c['#'] = '#';
  13. ss[cnt+1] = '*';
  14. }
  15. void manacher(){
  16. int id = 0, mx = 0;
  17. for(int i=1;i<=cnt;i++){
  18. if(i <= mx) p[i] = min(p[id*2-i],mx-i);
  19. else p[i] = 1;
  20. while(ss[i+p[i]] == c[ss[i-p[i]]]) p[i]++;
  21. if(mx < i+p[i]) mx = i+p[i], id = i;
  22. }
  23. for(int i=1;i<=cnt;i+=2)
  24. ans += (p[i]-1)/2;
  25. }
  26. int main(){
  27. //freopen("ghost.in","r",stdin);
  28. //freopen("ghost.out","w",stdout);
  29. cin >> n; scanf("%s",s+1);
  30. init(); manacher();
  31. printf("%lld\n",ans);
  32. return 0;
  33. }

[洛谷P3501] [POI2010]ANT-Antisymmetry的更多相关文章

  1. 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)

    洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...

  2. 【BZOJ2084】【洛谷P3501】[POI2010]ANT-Antisymmetry(Manache算法)

    题意描述 原题: 一句话描述:对于一个0/1序列,求出其中异或意义下回文的子串数量. 题解 我们可以看出,这个其实是一个对于异或意义下的回文子串数量的统计,什么是异或意义下呢?平常,我们对回文的定义是 ...

  3. 洛谷 P3496 [POI2010]GIL-Guilds

    P3496 [POI2010]GIL-Guilds 题目描述 King Byteasar faces a serious matter. Two competing trade organisatio ...

  4. 洛谷 P3507 [POI2010]GRA-The Minima Game

    P3507 [POI2010]GRA-The Minima Game 题目描述 Alice and Bob learned the minima game, which they like very ...

  5. 洛谷 P3505 [POI2010]TEL-Teleportation

    P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...

  6. 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解

        这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...

  7. 洛谷P3507 [POI2010]GRA-The Minima Game

    题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...

  8. [洛谷P3509][POI2010]ZAB-Frog

    题目大意:有$n$个点,每个点有一个距离(从小到大给出),从第$i$个点跳一次,会跳到距离第$i$个点第$k$远的点上(若有两个点都是第$k$远,就跳到编号小的上).问对于从每个点开始跳,跳$m$次, ...

  9. [洛谷P3512 [POI2010]PIL-Pilots]

    题目链接: 传送门走这里 题目分析: 感觉不是很难啊--不像是蓝题(AC量也不像)恶意评分? 少打了一个+1调了半天,就这样居然还能过60pts?我思路和题解第一篇高度重合是什么鬼啊,太过分了吧本来还 ...

随机推荐

  1. 初步学习pg_control文件之八

    接前文  初步学习pg_control文件之七  继续 看:catalog_version_no 代码如下: static void WriteControlFile(void) { ... /* * ...

  2. LeetCode:15. 3Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...

  3. centos7下安装oracle11gR2的详细步骤

    环境准备 安装包: CentOS-7-x86_64-DVD linux.x64_11gR2_database_1of2.zip linux.x64_11gR2_database_2of2.zip 本教 ...

  4. elasticsearch-mathc和term的区分

    elasticsearch和mysql在思想上是有不同的,elasticsearch有分词一说,比如北京奥运分词成北京,奥运,北京奥运.分词要要考虑两点,一个是查询字符串要不要分词,还有就是原存储字段 ...

  5. Linux 下 PHP 扩展 PDO 编译安装

    1.进入 PHP 的软件包 pdo 扩展目录中(注:不是 PHP 安装目录) [root@tester /]# /home/tdweb/php-5.4.34/ext/pdo_mysql 执行 phpi ...

  6. Entity Framework + WCF 远程调用出错

            在使用Entity Framework中使用WCF,在程序中调用服务一直报错,我一直以为是WCF的哪个地方的配置有问题,找来找去,一直没有解决.         最后在网上找到一篇文章 ...

  7. 今日Linux下安装部署禅道

    我的linux系统是在虚拟机上安装的Ubuntu,禅道在官网www.zentao.net下载安装的开源版的linux64位,采用一键安装包安装.安装前要求:系统上不能有自己安装的mysql .下载的安 ...

  8. 第九篇 Python数据类型之集合

    集合 set 写在最前,必须要会的:1.长度len2.成员运算in和not in3.|合集4.&交集5.-差集6.^对称差集7.==8.父集:>,>= 9.子集:<,< ...

  9. VC++之运算符重载简单小结

    封装继承和多态是面向对象三大基本支柱.在面向对象系统中有两种编译方式:静态联编和动态联编静态联编:也叫早期联编:指系统在编译时就决定如何实现某一动作,提供了执行速度快的优点.动态联编:也叫滞后联编:指 ...

  10. swarm 服务器安装

    apt install docker.io-----------------------配置加速器.私有仓库地址---------------------mkdir -p /etc/dockertee ...