Codeforces 17E Palisection 【Manacher】
Codeforces 17E Palisection
E. Palisection
In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam».
Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.
Let’s look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:
• «b» — 1..1
• «bab» — 1..3
• «a» — 2..2
• «b» — 3..3
• «bb» — 3..4
• «b» — 4..4
Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:
- 1..1 cross with 1..3
- 1..3 cross with 2..2
- 1..3 cross with 3..3
- 1..3 cross with 3..4
- 3..3 cross with 3..4
- 3..4 cross with 4..4
Since it’s very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not.
Input
The first input line contains integer n (1 ≤ n ≤ 2·106) — length of the text. The following line contains n lower-case Latin letters (from a to z).
Output
In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987.
Examples
input
4
babb
output
6
input
2
aa
output
2
恶心死了
对于两个回文串的位置关系
我们只减少回文串右端点严格小于另一个回文串左端点的情况
所以两个串的关系最多只会被减少一次
需要排除没有意义的情况
还需要差分计算贡献,注意倒着跑
还需要单独考虑回文串和单个字符的交,还是找找规律求一个前缀和就好
前缀和:因为我们对于一个点,只求出了最长回文串,又因为这个回文串包含的小回文串(同一中心)的左右节点变化是每次加或碱2,所以可以前缀和求出
#include<bits/stdc++.h>
using namespace std;
#define N 2000010
#define Mod 51123987
#define LL long long
int len,n;
int r[N<<1],p[N<<1],psum[N<<1];
char t[N],s[N<<1];
void Manacher(){
int id=0,pos=0,x;
for(int i=1;i<n;i++){
if(pos>i)x=min(p[id*2-i],pos-i+1);
else x=1;
while(s[i-x]==s[i+x])x++;
x--;
if(x+i>pos)pos=x+i,id=i;
p[i]=x;
}
}
int main(){
scanf("%d%s",&len,t);
s[n=0]='!';
for(int i=0;i<len;i++)s[++n]='#',s[++n]=t[i];
s[++n]='#';s[++n]='?';
Manacher();
int ans=0,tmp=0;
for(int i=1;i<n;i++){
if(p[i]==0&&s[i]=='#')continue;
if(p[i]==1&&s[i]!='#')continue;
//枚举左右边界,对当前回文串包含的所有回文串进行累加
r[i-p[i]-1]--;r[i-1]++;//差分求贡献
tmp=(tmp+p[i]/2)%Mod;
}
//r差分累加
//r第一次累加 前缀和
//r第二次累加 计算答案
for(int i=n-1;i>0;i--)r[i]+=r[i+1];
for(int i=n-1;i>=1;i--){
if(i&1)r[i]=r[i+1];
else r[i]=(r[i]+r[i+1])%Mod;
}
for(int i=n-3;i>0;i--)r[i]=(r[i]+r[i+2])%Mod;
ans=1ll*tmp*(tmp-1)/2%Mod;
for(int i=1;i<n;i++){
if(p[i]==0&&s[i]=='#')continue;
if(p[i]==1&&s[i]!='#')continue;
ans-=r[i+3]-r[i+p[i]+3];
ans=(ans%Mod+Mod)%Mod;
}
//讨论回文串和单个字符的交
for(int i=2;i<n;i++)psum[i]=(i+psum[i-2])%Mod;
for(int i=1;i<n;i++)ans=(ans+psum[p[i]])%Mod;
printf("%d",ans);
return 0;
}
Codeforces 17E Palisection 【Manacher】的更多相关文章
- [CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...
- 【manacher】HDU3068-最长回文
[题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...
- 【Manacher】Colorful String
The value of a string s is equal to the number of different letters which appear in this string. You ...
- 【manacher】HDU4513-吉哥系列故事——完美队形II
[题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...
- Codeforces 17E Palisection - Manacher
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个串$s$询问,有多少对回文子串有交. 好像很简单的样子. 考虑能不能直接求,感觉有点麻烦.因为要考虑右端点在当前回文子串内还有区间包含 ...
- BZOJ2160 拉拉队排练【Manacher】
Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...
- Codeforces 490B Queue【模拟】
题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE 我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号 ex[] ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Codeforces 839C Journey【DFS】
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...
随机推荐
- java 资源文件夹下的MEAT-INF
META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗 https://www.cnblogs.com/demingblog/p/5653844.html Jar ...
- 索引选择性与cardinality
索引选择性 索引选择性是索引基数(cardinality)与表中数据行数(n_row_in_table)的比值,即 索引选择性=索引基数/数据行 其中cardinality是索引中不重复记录的预估值. ...
- nRF5芯片外设GPIO和GPIOTE介绍
nRF51/nRF52同时包含GPIO和GPIOTE两种外设,经常有人将两者搞混,今天我们就来介绍一下这2种外设有什么不同,及使用注意事项. GPIO和GPIOTE都属于芯片外设,但两者功能完全不一样 ...
- led,key通用IO的端口
1 注意通用IO端口, GPBCON 只能控制一个GPBDAT位(对应的位),而GPBUP可以使能GPBCON.
- 英语每日阅读---4、VOA慢速英语(翻译+字幕+讲解):专家:城市发展将加剧住房危机
英语每日阅读---4.VOA慢速英语(翻译+字幕+讲解):专家:城市发展将加剧住房危机 一.总结 一句话总结:城市化(越来越多的人会住进城市)是必然趋势,人口增长也是必然趋势,人口增长必然会加大住房危 ...
- JMeter中响应数据显示乱码问题解决
方法一.UTF-8 路径:JMeter-->bin-->jmeter.properties 打开之后 #sampleresult.default.encoding=ISO-8859-1 改 ...
- 《深入理解mybatis原理3》 Mybatis数据源与连接池
<深入理解mybatis原理> Mybatis数据源与连接池 对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构 ...
- [转]RGB数据保存为BMP图片
转自:http://blog.csdn.net/yixianfeng41/article/details/52591585 一.BMP文件由文件头.位图信息头.颜色信息和图形数据四部分组成. 1.BM ...
- Highcharts 配置语法;Highcharts 配置选项详细说明
Highcharts 配置语法 本章节我们将为大家介绍使用 Highcharts 生成图表的一些配置. 第一步:创建 HTML 页面 创建一个 HTML 页面,引入 jQuery 和 Highchar ...
- leetcode 849. Maximize Distance to Closest Person
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...