[洛谷P3501] [POI2010]ANT-Antisymmetry
洛谷题目链接:[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;
}
[洛谷P3501] [POI2010]ANT-Antisymmetry的更多相关文章
- 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)
洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...
- 【BZOJ2084】【洛谷P3501】[POI2010]ANT-Antisymmetry(Manache算法)
题意描述 原题: 一句话描述:对于一个0/1序列,求出其中异或意义下回文的子串数量. 题解 我们可以看出,这个其实是一个对于异或意义下的回文子串数量的统计,什么是异或意义下呢?平常,我们对回文的定义是 ...
- 洛谷 P3496 [POI2010]GIL-Guilds
P3496 [POI2010]GIL-Guilds 题目描述 King Byteasar faces a serious matter. Two competing trade organisatio ...
- 洛谷 P3507 [POI2010]GRA-The Minima Game
P3507 [POI2010]GRA-The Minima Game 题目描述 Alice and Bob learned the minima game, which they like very ...
- 洛谷 P3505 [POI2010]TEL-Teleportation
P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...
- 【字符串】【hash】【倍增】洛谷 P3502 [POI2010]CHO-Hamsters 题解
这是一道字符串建模+图论的问题. 题目描述 Byteasar breeds hamsters. Each hamster has a unique name, consisting of lo ...
- 洛谷P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...
- [洛谷P3509][POI2010]ZAB-Frog
题目大意:有$n$个点,每个点有一个距离(从小到大给出),从第$i$个点跳一次,会跳到距离第$i$个点第$k$远的点上(若有两个点都是第$k$远,就跳到编号小的上).问对于从每个点开始跳,跳$m$次, ...
- [洛谷P3512 [POI2010]PIL-Pilots]
题目链接: 传送门走这里 题目分析: 感觉不是很难啊--不像是蓝题(AC量也不像)恶意评分? 少打了一个+1调了半天,就这样居然还能过60pts?我思路和题解第一篇高度重合是什么鬼啊,太过分了吧本来还 ...
随机推荐
- 11 TCP实现QQ聊天
1.客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SOCK_STREA ...
- Java RMI 入门指南
开通博客也有好些天了,一直没有时间静下心来写博文,今天我就把两年前整理的一篇关于JAVA RMI入门级文章贴出来,供有这方面需要的同学们参考学习. RMI 相关知识 RMI全称是Remote Meth ...
- Eclipse 导入项目与 svn 插件关联全过程记录
文章摘自:http://www.cnblogs.com/xmmcn/archive/2013/03/01/2938365.html 感谢博友分享! Eclipse 导入项目与 svn 插件关联全过程记 ...
- git使用ssh密钥(转)
git使用https协议,每次pull, push都要输入密码,相当的烦.使用git协议,然后使用ssh密钥.这样可以省去每次都输密码. 大概需要三个步骤:一.本地生成密钥对:二.设置github上的 ...
- APK反编译后添加日志
一.反编译 参考前一篇文章 二.添加寄存器(locals) 因为要添加日志,我们一般需要用一个变量来存储TAG,所以需要增加一个寄存器 如: # virtual methods .method pub ...
- Python 常见的错误类型和继承关系
Python所有的错误都是从BaseException类派生 BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit ...
- Qt QML之不显示标题栏、边框
原文连接:http://blog.csdn.net/u010780613 我使用的Qt版本是Qt 5.3.0,Qt Creator 是3.1.1. QML做界面实在太方便了,动画效果很不错. 创建一个 ...
- 英特尔CEO科再奇:尚未发现通过漏洞获取用户数据的行为
1月9日消息,英特尔CEO科再奇在美国西部时间1月8日举行的2018年CES中发表主题演讲,他在开场时面向产业界谈到了最近报道的安全研究发现.科再奇表示:“在我们开始之前,我想借此机会感谢整个行业,为 ...
- Daily Scrum02 11.29
今天大家都已经开始了进行第二轮迭代的工作!相比第一轮迭代,每个人都已经有了一定开发经验,这次做起来顺手很多.薛神和肖犇的挑战最大,他们需要实现好友功能,手机间的通信.服务器的搭建都是难点,但他们的热情 ...
- 第十五次ScrumMeeting会议
第十五次Scrum Meeting 时间:2017/12/5 地点:主201 人员:全体成员 目前工作情况 名字 完成的工作 计划工作 蔡帜 -- -- 游心 完成Scrum会议记录,更新wiki游戏 ...