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的更多相关文章

  1. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  2. HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)

    题目链接  2017 CCPC Hangzhou  Problem E 题意  给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...

  3. HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)

    题目链接  2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块.    分块的时候满足每个块是一个 ...

  4. 2017 CCPC 哈尔滨站 题解

    题目链接  2017 CCPC Harbin Problem A Problem B Problem D Problem F Problem L 考虑二分答案. 设当前待验证的答案为x 我们可以把第二 ...

  5. 2017 CCPC秦皇岛 A题 A Ballon Robot

    The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be  teams parti ...

  6. 2017 CCPC秦皇岛 M题 Safest Buildings

    PUBG is a multiplayer online battle royale video game. In the game, up to one hundred players parach ...

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

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

  9. 2017 CCPC 哈尔滨站 HDU 6242

    Geometry Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Other ...

随机推荐

  1. 「题目代码」P1054~P1059(Java)

    P1054 猴子吃桃 import java.util.*; import java.io.*; import java.math.BigInteger; import java.lang.Chara ...

  2. 不老的神器--namp,awvs

    要会使用的工具 NESSUS nmap awvs hydra burpsuit 工具的话,都有文档,应该多使用 -h 多看官方文档,就会用了. 1.namp基本用法 -iL <inputfile ...

  3. Memcache的客户端连接系列(二) Python

    关键词: Memcached   Python 客户端 声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(Memcached)的用户指南.客户端连接方法通用,故摘抄过来分享给大家. Python ...

  4. Apache——访问控制

    Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny  先执行允许,再执行拒绝 Order deny,allow ...

  5. 关闭Tomcat进程 一条语句(必看)

    写在开始 MAC系统下进行JAVA研发,经常遇到的一个问题就是杀死异常Tomcat 通常都是用两条指令,先查询出Tomcat占用的进程,再kill掉该进程, 其实有一种联合语句的方式可以一条语句直接关 ...

  6. Some good articles

    https://alligator.io/vuejs/introduction-render-functions/ https://alligator.io/vuejs/vue-jwt-pattern ...

  7. [leetcode-748-Largest Number At Least Twice of Others]

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  8. PHPCMS调取当前栏目的描述、文章位置导航、当前栏目链接、当前栏目名称

    当我们填写了栏目描述,怎么调用出来. 使用 {$CATEGORYS[$catid][description]} 就可以把栏目的描述调用出来 下面三个也比较常用{catpos($catid)} 显示文章 ...

  9. Java 变量和输入输出

    一些重要知识 一个源文件里只能有一个public类,其它类数量不限.文件名与public类名相同 JAVA程序严格区分大小写 JAVA应用程序的执行入口是main方法固定写法:public stati ...

  10. UVALive - 6868 Facility Locations 想法题

    题目链接: http://acm.hust.edu.cn/vjudge/problem/88634 Facility Locations Time Limit: 3000MS 题意 给你一个m*n的矩 ...