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 ...
随机推荐
- react-native android 初始化问题
最近开始接触rn,官方起手,装了一堆工具,然后启动项目的时候出现了一堆问题,这里针对我遇到的一些问题提供一些解决方案. 本人开发环境mac,在启动ios的时候没啥大问题,可以直接启动,这里提示一点,因 ...
- android分析windowManager、window、viewGroup之间关系(一)
本文将主要介绍addview方法,在windowManager.window.viewGroup中的实现原理.首先将介绍这些类结构关系,然后分析其内在联系,介绍实现原理,最后介绍重要的一个参数wind ...
- git配置github链接
1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...
- word record 2
word record 2 scavenger // si ga wen ger a person, animal or insect who takes what others have left ...
- 爬虫2.2-scrapy框架-文件写入
目录 scrapy框架-文件写入 1. lowb写法 2. 高端一点的写法 3. 优化版本 scrapy框架-文件写入 1. lowb写法 ~pipelines.py 前提回顾,spider.py中 ...
- geth账户密码
xiaocong geth账户密码 123 {d6abe909013d8da914ae2a08c9b58e7b76601b39} 账户密码 123456 0x4A7F15104F54dB3214D2F ...
- 十六:The YARN Service Registry
yarn 服务注册功能是让长期运行的程序注册为服务一直运行. yarn中运行的程序分为两类,一类是短程序,一类一直运行的长程序.第二种也称为服务.yarn服务注册就是让应用程序能把自己注册为服务,如h ...
- 基于范围的for语句
一.关键点 1. 作用过程 遍历给定序列中的每个元素并对序列中的每个值执行某种操作. 2. 若要修改序列中元素的值,需将类型定义为引用 string s("Hello World!!!&qu ...
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- C语言 命令行参数 函数指针 gdb调试
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...