【BZOJ2084】【洛谷P3501】[POI2010]ANT-Antisymmetry(Manache算法)
题意描述
原题:
一句话描述:对于一个0/1序列,求出其中异或意义下回文的子串数量。
题解
我们可以看出,这个其实是一个对于异或意义下的回文子串数量的统计,什么是异或意义下呢?平常,我们对回文的定义是,对于任意$i$,$S[i]=S[n-i+1]$,而我们把相等改为异或操作,那么,当且仅当$1$与$0$相匹配时,返回值为$1$ 也就是 “真”。
那么,我们可以尝试使用Manache算法来解决。当然,编程时,我们并不必真的去把0/1序列转换为数字序列,进行异或操作,这样会给自己增加一波常数(迷),我们构造一个to数组,$to[x]$数组的定义为 对于字符$x$ 我们允许匹配的对应字符,显然,$to['0']='1'$,$to['1']='0'$,特别的$ to['\#']='\#' $ $to['\$']='\$' $。(此处'#'与'$'是Manache算法的分隔字符与防止溢出字符,可以自定义)。
对于Manache算法有任何不了解的地方,可以戳!!!这里!!!,又看不懂的地方,也可以联系文文(434935191)
对于代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
const int maxn = ;
typedef unsigned long long ull;
char SS1[maxn],S[maxn],to[];
int n,len[maxn],tot=;
int main() {
scanf("%d%s",&n,SS1+);S[]='$',S[]='#';
for(register int i=;i<=n;++i) S[++tot]=SS1[i],S[++tot]='#';
to['']='',to['']='',to['#']='#',to['$']='$';
int pos=,mx=;ull ans=;
for(register int i=;i<=tot;i+=) {
len[i]=(i<mx?std::min(mx-i,len[(pos<<)-i]):);
while(S[i+len[i]]==to[S[i-len[i]]]) len[i]++;
if(len[i]+i>mx) {
mx=len[i]+i;pos=i;
}
ans+=len[i]>>;
}
printf("%llu\n",ans);
return ;
}
【BZOJ2084】【洛谷P3501】[POI2010]ANT-Antisymmetry(Manache算法)的更多相关文章
- [洛谷P3501] [POI2010]ANT-Antisymmetry
洛谷题目链接:[POI2010]ANT-Antisymmetry 题目描述 Byteasar studies certain strings of zeroes and ones. Let be su ...
- 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)
洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...
- 洛谷 P3805 【模板】manacher算法
洛谷 P3805 [模板]manacher算法 洛谷传送门 题目描述 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 字符串长度为n 输入格式 一行小写英文字符 ...
- 洛谷P4581 [BJOI2014]想法(玄学算法,拓扑排序)
洛谷题目传送门 萝卜大毒瘤 题意可以简化成这样:给一个DAG,求每个点能够从多少个入度为\(0\)的点到达(记为\(k\)). 一个随机做法:给每个入度为\(0\)的点随机一个权值,在DAG上求出每个 ...
- 洛谷 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 ...
随机推荐
- Ubuntu12.04编译vlc-android详细流程
作者:wainiwann 出处:http://www.cnblogs.com/wainiwann/ 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- FM模型
[ 闲聊DNN CTR预估模型] http://www.mamicode.com/info-detail-1465813.html http://blog.csdn.net/bitcarmanlee/ ...
- class.__subclasses__()
[class.__subclasses__()] Each class keeps a list of weak references to its immediate subclasses. Thi ...
- 59. Spiral Matrix II (Array)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Socket.IO for Unity 简要介绍
在项目中使用到了Socket.IO for unity这个Asset Store上免费的库,这里将简要的介绍一下它的结构,已经使用中的注意事项. 目录结构 上面为包的目录结构,简单的介绍一下具体的内容 ...
- Java方法_数组
/* 方法:完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中函数被称为方法. 方法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) { ...
- 【记录】CentOS7安装NODEBB
NodeBB介绍: NodeBB 是一个更好的论坛平台,专门为现代网络打造.它是免费的,易于使用. NodeBB 论坛软件是基于 Node.js开发,支持 Redis 或 MongoDB 的数据库.它 ...
- CodeForces 688A Opponents (水题)
题意:给定 n 行数,让你找出连续最多的全是1的个数. 析:好像也没什么可说的,那就判断一下,并不断更新最大值呗. 代码如下: #include <iostream> #include & ...
- Understanding String Table Size in HotSpot
In JDK-6962930[2], it requested that string table size be configurable. The resolved date of that b ...
- Array对象的创建及其操作方法
一.创建数组,即实例化数组对象 有三种方式:1. new Array(); 2.new Array(size); ...