【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 ...
随机推荐
- C语言高级程序设计——进制算法以及位算符号
语言不够官方:意会: 数据储存运算是以二进制的,二进制数有原码 反码 补码三种.通常所说的二进制就是原码.(语言不官方) 原码 :4的原码可以为:0000 0100:最高位0 可以为符号数 反码:正数 ...
- mysql常用语句及关键字
一.常用sql语句 1.创建数据库userCREATE DATABASE user; 2.删除数据库userDROP DATABASE user; 3.使用数据库userUSE user;显示数据库 ...
- git的基本命令
在当前目录新建一个git代码库$ git init 设置提交代码时的用户信息:$ git config [--global] user.name "[name]"$ git con ...
- react-navigation 3.x版本的安装以及react-native-gesture-handler配置
一.安装依赖,使用npm或yarn命令,3.x版本必须安装react-native-gesture-handler react-navigation react-native-gesture-hand ...
- Linq操作DataTable
IEnumerable<DataRow> q = from dr in dt.AsEnumerable() where dr.Field& ...
- java 主类的main方法调用其他方法
方法1:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生,如下: public class test{ class A{ void fA(){ S ...
- [Selenium] 针对下拉菜单出现之后又立马消失的问题,通过Javascript改变元素的可见属性
public void clickDateDropDownButton() { SeleniumUtil.jsClick(driver, page.getDateDropdownButtonOfInv ...
- Android使用ListView使用方法
Android使用ListView应该注意的地方 在ListView中设置Selector为null会报空指针? mListView.setSelector(null);//空指针 试试下面这种: ...
- 关于使用 ps脚本来处理图片的排层问题
问题是这样,在三维软件 把模型切割,给切割的部件排上序号 如 :tianji_1-431_bujian13.png 中间一段数据就是表示的 1.431 该数据之后 会到ps总排层使用 在ps排层出三维 ...
- About Game Controllers
[About Game Controllers] Game Controller(GC),框架从iOS 7和OS X v10.9开始加入,用于便捷使用控制器(手柄). Once discovered, ...