【题解】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 ...
随机推荐
- ng-zorror-antd------Input输入框
使用 ng-zorror-antd 来美化界面,带着问题挖掘技术: 一:基本使用 效果图: 问题一:如何人为控制输入框长度,如上效果图,让一个输入框长,一个输入框短?(注意:该输入框是最简单的输入框, ...
- CAS 集群部署
业务场景 单点登录服务器如果压力过大的情况,那么可以使用集群分担压力,但是cas 默认不支持session同步. 所以可以需要做session同步,可以使用j2cache 实现session同步.另外 ...
- Odoo treeView列表视图详解
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826414.html TreeView:列表视图 1:<tree>标签的属性 [tree标签内 ...
- 短uuid生成
UUID UUID是128位的全局唯一标识符,通常由32字节的字符串表示.它可以保证时间和空间的唯一性,python中称为UUID,其他语言中可能称为GUID. 它通过MAC地址.时间戳.命名空间.随 ...
- python将Excel文件内容导入Mysql数据
为了方便起见,将所有字段类型设置为str,理解mysql的语法这个玩意贼简单 # _*_ coding:utf-8 _*_import pandas as pd #先装个pandas ,pip ins ...
- Candies POJ - 3159
题目链接:https://vjudge.net/problem/POJ-3159 思路: 能看出是差分约束的题, 我们想假设一个人是 p(1),另一个人是p(2),他们之间糖果差为w, 那么需要满足的 ...
- LIST OF BEST OPEN SOURCE BLOCKCHAIN PLATFORMS
https://www.blockchain-council.org/blockchain/list-of-best-open-source-blockchain-platforms/ Open so ...
- ESA2GJK1DH1K基础篇: Android实现SmartConfig简单Demo
下载源码去 百度安信可 导入源码 等待加载完 我的提示更新下软件 ,我就更新下 安装完成以后重新导入工程 安装到手机 注意,由于Android 9.0 以后的获取WIFI名称需要打开GPS,所以如果提 ...
- selenium--上传文件
前戏 在进行web自动化的时候,经常需要用到上传文件的功能,selenium可以使用send_keys()来上传文件,但是使用send_keys()上传文件有很大的局限性,只能上传input标签的,好 ...
- ZROI 暑期高端峰会 A班 Day6 离线问题
FBI Warning:本文含有大量人类本质之一. 动态联通问题 允许离线. 模板,不讲了. 归并排序 %@)(#&%)++%($@)%!#(&%)(&@))) 主定理 U^( ...