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 ...
随机推荐
- ASP.NET数据库连接
启动Visual Studio,新建一个web工程 点开工程目录下web.config文件, 找到节点,新增数据库配置 aspx界面新建一个button和一个文本框用于测试数据库连接, 其中butto ...
- selenium--driver.switchTo()
在自动化测试中,会遇到多窗口.多iframe.多alert的情况.此时,会使用driver.switchTo()来解决. 下面时关于driver.switchTo()的详细介绍: 1.多windows ...
- SIG蓝牙mesh笔记2_mesh组成
目录 SIG 蓝牙 mesh 组成 mesh网络概述 网络和子网 设备和节点 devices & nodes 入网 mesh中的几个概念 智能插座例子 SIG 蓝牙 mesh 组成 mesh网 ...
- 十 Writing YARN Applications
本节介绍: 使用yarn 高级提交写yarn应用程序.其实已经yarn底层API.MR计算框架对底层的API实现了封装. 高级提交指直接使用yarn的三种接口来提交应用程序: 1)YarnCl ...
- Git 命令详解及常用命令
Git 命令详解及常用命令 Git作为常用的版本控制工具,多了解一些命令,将能省去很多时间,下面这张图是比较好的一张,贴出了看一下: 关于git,首先需要了解几个名词,如下: 1 2 3 4 Work ...
- Python—文件
def fileCopy(src, dst, srcEncoding, dstEncoding): with open(src, 'r', encoding=srcEncoding) as srcfp ...
- Martian Addition
In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are ...
- "Hello world!"团队第八次会议
Scrum会议 今天是我们"Hello world!"团队第八次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...
- java利用POI实现读取Word并获取指定样式的文本
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.model.StyleDescription; import o ...
- c# 四则运算出错
不同类型值之间不可直接相减,long和short得出的差继续参与运算出错. 有待深究.