【题解】Palindrome pairs [Codeforces159D]
【题解】Palindrome pairs [Codeforces159D]
传送门:\(Palindrome\) \(pairs\) \([CF159D]\)
【题目描述】
给定一个长度为 \(N\) 的字符串 \(S\),求有多少四元组 \((l_1,r_1,l_2,r_2)\) 满足 \(1 \leqslant l_1 \leqslant r_1 \leqslant l_2 \leqslant r_2 \leqslant N\) 且 \(S[l1...r1],\) \([Sl2...r2]\) 都是回文串。
【样例】
样例输入:
aa
样例输出:
1
样例输入:
aaa
样例输出:
5
样例输入:
abacaba
样例输出:
36
【数据范围】
\(100\%\) \(n \leqslant 2000\)
【分析】
由于这道题数据较小,直接写暴力也可以过(见隔壁大佬),但\(Palisection\) \([CF17E]\)就过不了了,这时候我们需要更高效的算法。
首先,用 \(Manacher\) 求出一个 \(f[i]\) 数组,用其表示以 \(a[i]\) 为中心最多可以匹配的回文串半径,那么就可以递推了。
实际上是要求互不相交的回文串对数,与\(Palisection\) \([CF17E]\)恰恰相反。
对于每一个回文串 \([l,r]\),凡是 \([1,l-1]\) 中的回文串都可以与之形成合法四元组,可以用 \(st[i],ed[i]\) 分别表示以 \(a[i]\) 开头和以 \(a[i]\) 结尾的回文串个数,那么 \(ans=\sum_{i=2}^{n}{\sum_{j=1}^{i-1}st[j]*ed[i]}\) 。
初始化时要对 \(st,ed\) 进行区间修改,查询时是单点查询,可以用线段树或者树状数组,不过有点麻烦,可以直接存差分数组,然后统计答案时用一个变量 \(S\) 优化掉枚举 \(st\) 求和的过程。
时间复杂度:\(O(n)\) 。
【Code】
#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
const int N=2003;
int n=1,m,p,q,f[N<<1];LL S,ans,st[N<<1],ed[N<<1];char a[N],b[N<<1];//记得开long long
inline int min(Re a,Re b){return a<b?a:b;}
int main(){
scanf("%s",a+1),m=strlen(a+1);
for(Re i=1;i<=m;++i,++n)b[++n]=a[i];//玄学填空法
b[0]=1,b[n+1]=2;//放置首尾两边多余部分被匹配
for(Re i=1;i<=n;++i){//Manacher
f[i]=q>i?min(f[(p<<1)-i],q-i):1;
while(b[i-f[i]]==b[i+f[i]])++f[i];
if(i+f[i]>q)q=(p=i)+f[i];
}
for(Re i=1;i<=n;++i){
++st[i-f[i]+1],--st[i+1];//区间修改[i-f[i]+1,i]
++ed[i],--ed[i+f[i]-1+1];//区间修改[i,i+f[i]-1]
}
for(Re i=1;i<=n;++i){
st[i]+=st[i-1],ed[i]+=ed[i-1];//差分求和算出单个的st,ed
if(!(i%2))ans+=S*st[i],S+=ed[i];
}
printf("%lld",ans);
}
【题解】Palindrome pairs [Codeforces159D]的更多相关文章
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- 336. Palindrome Pairs(can't understand)
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
随机推荐
- disable_function绕过--利用LD_PRELOAD
0x00 前言 有时候直接执行命令的函数被ban了,那么有几种思路可以bypass 1.使用php本身自带的能够调用外部程序的函数 2.使用第三方插件入(如imagick) 但是这两种无非就是利用ph ...
- JavaWeb 使用Session实现一次性验证码
表单 <form action="loginServlet" method="post"> 请输入验证码:<input type=" ...
- 面试官:优化代码中大量的if/else,你有什么方案?
一个快速迭代的项目,时间久了之后,代码中可能会充斥着大量的if/else,嵌套6.7层,一个函数几百行,简!直!看!死!人! 这个无限循环嵌套,只是总循环的一部分...我已经绕晕在黄桷湾立交 仔细数了 ...
- iOS 上传appstore 一直卡在正在通过 App Store 进行鉴定(转)
原帖地址:https://www.jianshu.com/p/d28714f3ef74 手动操作一下执行xcode包里面的命令行操作: (1)找到前往>应用程序里面的xcode (2)显示包内容 ...
- Shell 编程 数组
本篇主要写一些shell脚本数组的使用. 数组定义 数组名=(value0 value1 vlaue2 ...) 数组名=([0]=value [1]=value [2]=vlaue ...) 列表名 ...
- DF1协议常用命令
PCCC:Programmable Controller Communication Commands. AB PLC常用指令 根据http://www.iatips.com/pccc_tips.ht ...
- wordpress中文目录出现“有点尴尬诶!该页无法显示"
原因不详,可能是.htaccess.网上说删除后再更新固定链接会再生成,但是我没有.我又把原来的.htaccess上传后更改固定链接为“数字型”,测试后可以正常浏览. 然后又再更改为原来的“日期和名称 ...
- 09-numpy-笔记-repeat
repeat:复制元素 axis = 0 复制每行 axis = 1 复制每列 2 表示复制一遍 不设置axis,复制每个,按行展开成一行. >>> import numpy as ...
- application platform as a service (aPaaS)
Application platform as a service (aPaaS) is a cloud service that provides environments for the deve ...
- 【java】oracle好用,但java运行缺失右括号
可能原因SQL拼接有空格被省略导致sql粘连. 解决办法,扩大拼接或者缩小拼接范围.