2017 ccpc哈尔滨 A题 Palindrome
2017 ccpc哈尔滨 A题 Palindrome
题意:
给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\)
思路:
很明显这里的回文串长度为奇数,所以用\(manacher\)处理时不需要添加间隔字符
所以这里的\(Len[i]\)表示的就是以\(i\)为中心的回文串向左右最远能延伸的长度
那么\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\)就等价于
找到一对$(i,j), 满足i - Len[i] + 1 <= j < i 且 j + Len[j] - 1 >= i \(
可以用主席树来维护,更简单的方法就是
将\)j + Len[j] - 1按升序排列\(,然后对于\)j$丢到树状数组里查询贡献就好了。
#include<bits/stdc++.h>
#define P pair<int,int>
#define LL long long
using namespace std;
const int maxn = 5e5 + 10;
const int N = 1e6 + 10;
char s[N];
int Len[N];
int lowbit(int x){return x & (-x);}
int tr[N],R;
int getsum(int pos){
int ans = 0;
for(;pos;pos -= lowbit(pos)) ans += tr[pos];
return ans;
}
void up(int pos){
for(;pos <= R;pos += lowbit(pos)) tr[pos]++;
}
void Manacher(char *s){
int len = strlen(s + 1);
s[0] = '#';
int mx = 0,center = 0;
///mx为当前计算回文串最右边字符的最大值
///center为取得mx最大值的中心
for(int i = 1;i <= len;i++){
if(mx > i) Len[i] = min(mx - i, Len[2 * center - i]);///考虑i关于center的对称的Len
else Len[i] = 1;
while(s[i - Len[i]] == s[i + Len[i]]) Len[i]++;
if(Len[i] + i > mx) mx = Len[i] + i, center = i; ///更新最右
}
}
struct node{
int x,l;
node(int x,int l):x(x),l(l){};
node(){};
bool operator<(const node &rhs)const{
return l > rhs.l;
}
}q[N];
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s + 1);
Manacher(s);
int len = strlen(s + 1);
R = len;
for(int i = 1;i <= R;i++) tr[i] = 0;
for(int i = 1;i <= len;i++) q[i] = node(i, i + Len[i] - 1);
sort(q + 1, q + len + 1);
int l = 1;
LL ans = 0;
for(int i = len;i >= 1;i--){
while(l <= len && q[l].l >= i) up(q[l++].x);
ans += getsum(i - 1) - getsum(i - Len[i]);
}
printf("%lld\n",ans);
}
return 0;
}
2017 ccpc哈尔滨 A题 Palindrome的更多相关文章
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)
题目链接 2017 CCPC Hangzhou Problem E 题意 给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...
- HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)
题目链接 2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块. 分块的时候满足每个块是一个 ...
- 2017 CCPC 哈尔滨站 题解
题目链接 2017 CCPC Harbin Problem A Problem B Problem D Problem F Problem L 考虑二分答案. 设当前待验证的答案为x 我们可以把第二 ...
- 2017 CCPC秦皇岛 A题 A Ballon Robot
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be teams parti ...
- 2017 CCPC秦皇岛 M题 Safest Buildings
PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...
- 2017 CCPC秦皇岛 L题 One Dimensions Dave
BaoBao is trapped in a one-dimensional maze consisting of grids arranged in a row! The grids are nu ...
- 2017 CCPC秦皇岛 E题 String of CCPC
BaoBao has just found a string of length consisting of 'C' and 'P' in his pocket. As a big fan of ...
- 2017 CCPC 哈尔滨站 HDU 6242
Geometry Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other ...
随机推荐
- 翻译:利用GDAL生成cogeoff文件
翻译自: Introducing the AWS Lambda Tiler https://hi.stamen.com/stamen-aws-lambda-tiler-blog-post-76fc11 ...
- 关于 NPOI 导出的 Excel 出现“部分内容有问题” 的解决方法
近期发现使用 NPOI 导出的 Excel 文件,有部分用户反映在打开时报错,测试了一下,发现在低版本的 Office 中(2003版,配合2007格式兼容包)打开正常,但在高版本 Office 中, ...
- hdu2544最短路(floyd基础)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- ISE 14.7安装教程最新版(Win10安装)——解决Win10安装完后打不开快捷方式的方法
ISE 14.7安装教程最新版(Win10安装) Xilinx ISE是一款世界著名的硬件设计软件,它为设计流程的每一步都提供了直观的生产力增强工具,覆盖从系统级设计探索.软件开发和基于HDL硬件设计 ...
- javascript常用对象方法
concat:连接产生一个新数组 [1,2].concat([3,4]) >> [1, 2, 3, 4] filter:返回符合条件的一个新数组 [1,2,3,4,5].filte ...
- JAVA基础学习之路(八)[1]String类的基本特点
String类的两种定义方式: 直接赋值 通过构造方法赋值 //直接赋值 public class test2 { public static void main(String args[]) { S ...
- Python3 小工具-MAC泛洪
from scapy.all import * import optparse def attack(interface): pkt=Ether(src=RandMAC(),dst=RandMAC() ...
- 使用树莓派实现(山寨)高清视频叠加(HDMI OSD)
项目需要在HDMI上叠加一些字符包括汉字和数值,要求不能使用台式机,本身也没有HDMI采集卡驱动开发能力,所以通过海思的HDMI编码器将HDMI编码为h.264网络视频流,然后通过树莓派解码显示,做字 ...
- [C++] Copy Control (part 1)
Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...
- Python3 循环表达式
一 While循环 基本循环 while 条件: 执行内容 #循环体 ... #循环体 ... #循环体 # 若条件为真,执行循环体内容 # 若条件为假,不执行循环体内容 实例1(Python 3.0 ...