传送门

分析

有中文题面所以就不写题目大意了

我们先建出回文树

然后根据fail信息建出一棵树

从根向下dfs,中间记录每一个len出现在哪个节点即可

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int n,fail[],son[][],sum[],len[],cnt,last;
int val[],pos[],nxt[],head[],to[],tot;
long long Ans;
char s[];
inline void add(int x,int y){nxt[++tot]=head[x];head[x]=tot;to[tot]=y;}
inline void new_node(int x){len[++cnt]=x;sum[cnt]=;}
inline int get_fail(int x,int n){while(s[n]!=s[n-len[x]-])x=fail[x];return x;}
inline void build(){
for(int i=;i<=n;i++){
int x=s[i]-'a',now=get_fail(last,i);
if(!son[now][x]){
new_node(len[now]+);
fail[cnt]=son[get_fail(fail[now],i)][x];
son[now][x]=cnt;
}
last=son[now][x];
sum[last]++;
}
}
inline void dfs(int x){
if(x>)pos[len[x]]=x,Ans+=(long long)(val[x]=val[pos[len[x]/]]+)*sum[x];
for(int i=head[x];i;i=nxt[i])dfs(to[i]);
if(x>)pos[len[x]]=;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",s+);
n=strlen(s+);
memset(sum,,sizeof(sum));
memset(son,,sizeof(son));
memset(fail,,sizeof(fail));
memset(len,,sizeof(len));
memset(head,,sizeof(head));
tot=;
Ans=;
cnt=-;
new_node(),new_node(-);
fail[]=fail[]=;
last=;
build();
add(,);
for(int i=cnt;i>;i--)sum[fail[i]]+=sum[i],add(fail[i],i);
dfs();
printf("%lld\n",Ans);
}
return ;
}

Palindromeness CodeChef - PALPROB的更多相关文章

  1. 「Codechef April Lunchtime 2015」Palindromeness

    「Codechef April Lunchtime 2015」Palindromeness 解题思路 : 考虑对于回文子串 \(s\) 贡献的定义: \[ value_s = [\ s[1,\lflo ...

  2. 【CodeChef】Palindromeness(回文树)

    [CodeChef]Palindromeness(回文树) 题面 Vjudge CodeChef 中文版题面 题解 构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点 ...

  3. Codechef Palindromeness 和 SHOI2011 双倍回文

    Palindromeness Let us define the palindromeness of a string in the following way: If the string is n ...

  4. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  5. 【BZOJ4260】 Codechef REBXOR 可持久化Trie

    看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...

  6. codechef 两题

    前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...

  7. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  8. BZOJ3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 85[Submit][St ...

  9. CodeChef CBAL

    题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...

随机推荐

  1. SQLserver2008使用表达式递归查询

    --由父项递归下级 with cte(id,parentid,text) as ( --父项 select id,parentid,text from treeview where parentid ...

  2. springboot 不同环境不同的配置

    前言 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频 ...

  3. 向HDFS中追加内容

    向生成好的hdfs文件中追加内容,但是线上使用的版本是1.0.3,查看官方文档发现,在1.0.4版本以后才支持文件append 以下是向hdfs中追加信息的操作方法 如果你只在某一个driver中追加 ...

  4. Python学习系列(二)(基础知识)

    Python基础语法 Python学习系列(一)(基础入门) 对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比, ...

  5. hdu 5730 Shell Necklace——多项式求逆+拆系数FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...

  6. centOS下 JDK的三种安装方式

    由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创 ...

  7. MongoDB 4.X搭建

    一.MongoDB4.X搭建 1.下载mongdb安装包,在官网上找到对应的版本,我的是centos7 找到上面的连接,通过命令行: 2.将下载的mongodb-linux-x86_64-4.0.0. ...

  8. PL/SQL 训练07--发现问题

    drop table ma_schedue_task ; ---test_task(:1,:2) create table ma_schedue_task( created_by ) default ...

  9. web deploy 部署网站

    一.服务端配置 1. 确保在服务器端(我目前是win server 2012 R2)安装管理服务 安装后服务器会重启, 2)安装webdeploy http://www.iis.net/downloa ...

  10. 温故而知新java事务

    一.什么是Java事务 通常的观念认为,事务仅与数据库相关. 事务必须服从ISO/IEC所制定的ACID原则.ACID是原子性(atomicity).一致性(consistency).隔离性 (iso ...