【题解】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]的更多相关文章

  1. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. 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 ...

  4. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  5. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  6. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  7. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  8. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  9. 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 ...

随机推荐

  1. FPM九:配置FPM Launchpad

    1.事物代码LPD_CUST,点击新建输入角色和实例保存. 2.新建文件夹: 3.新建应用程序 这样一个菜单的LAUNCHPAD就好了. 4.FPM_WB运行FPM工作台,新建OVP应用程序. 保存本 ...

  2. h5表单亲测

    Document 下载进度: 标签. 牛奶 面包 男 女 one two three 按钮 搜索 请输入搜索内容 加密强度 用户名 Email 密码 年龄 身高 生日 这一系列是很酷的一个类型,完全解 ...

  3. dapi 基于Django的轻量级测试平台七 怎样部署到生产环境

    QQ群: GitHub:https://github.com/yjlch1016/dapi Nginx+uWSGI 前置条件:以下所有操作均在root账号下面进行如果不是root用户请注意权限问题因为 ...

  4. asp.net core mvc基于Redis实现分布式锁,C# WebApi接口防止高并发重复请求,分布式锁的接口幂等性实现

    使用背景:在使用app或者pc网页时,可能由于网络原因,api接口可能被前端调用一个接口重复2次的情况,但是请求内容是一样的.这样在同一个短暂的时间内,就会有两个相同请求,而程序只希望处理第一个请求, ...

  5. Windows中的txt文件到Mac打开是乱码 解决办法

    在Mac下打开“文本编辑”程序之后,选择菜单“文本编辑” -> “偏好设置”.2)在“偏好设置”中选择第二个标签页“打开和存储”,选择“纯文本文件编码”中的“打开文件”和“存储文件”修改成为“中 ...

  6. CentOS7配置本地Yum源

    从CentOS7官网下载DVD中存在需要的大部分软件,所以在没有网络的情况下可以配置yum源为本地的DVD,下载速度快,软件稳定.1. 如果使用虚拟机,那么就在虚拟机中挂载DVD的iso文件.2. 使 ...

  7. mave常用指令

    Maven常用命令有以下几种: 清理 maven clean 测试 maven test 编译 maven compile 打包 maven package 部署 maven install 第一个m ...

  8. LA2955 Vivian难题——梅森素数

    题意 输入 $k$(1 \leq k \leq 100)个正整数 $p_1, p_2, ..., p_k$(1 < p_i < 2{31}),找出 $k$ 个非负整数 $e_i$ 使得 $ ...

  9. Python面向对象 | 类方法 classmethod

      类方法:必须通过类的调用,而且此方法的意义:就是对类里面的变量或者方法进行修改添加. 例一个商店,店庆全场八折,代码怎么写呢? class Goods: __discount = 0.8 # 折扣 ...

  10. Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)