Palindromeness CodeChef - PALPROB
分析
有中文题面所以就不写题目大意了
我们先建出回文树
然后根据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的更多相关文章
- 「Codechef April Lunchtime 2015」Palindromeness
「Codechef April Lunchtime 2015」Palindromeness 解题思路 : 考虑对于回文子串 \(s\) 贡献的定义: \[ value_s = [\ s[1,\lflo ...
- 【CodeChef】Palindromeness(回文树)
[CodeChef]Palindromeness(回文树) 题面 Vjudge CodeChef 中文版题面 题解 构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点 ...
- Codechef Palindromeness 和 SHOI2011 双倍回文
Palindromeness Let us define the palindromeness of a string in the following way: If the string is n ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- codechef 两题
前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...
- codechef January Challenge 2014 Sereja and Graph
题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...
- BZOJ3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 339 Solved: 85[Submit][St ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
随机推荐
- Python笔记-3
一.文件的操作 1.文件的读.写.新增 读文件的获取句柄的语法:f=open('path/file') 句柄的理解: <_io.TextIOWrapper name='/root/myfile/ ...
- Hibernate HQL查询(2)
hql是面向对象查询,格式:from + 类名 + 类对象 + where + 对象的属性 sql是面向数据库表查询,格式:from + 表名 + where + 表中字段 1.查询 一般在Hiber ...
- 【spring源码学习】spring的远程调用实现源码分析
[一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...
- Django: TemplateDoesNotExist (rest_framework/api.html)
需要在站点前面的INSTALLED_APP里面加上rest_framework
- Flyway客户端使用
一.flyway介绍 Flyway是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway可以独立于应用实现管理并跟踪数据库变更,支持数据库版本自动升级,并且有一套默认的规约,不需要 ...
- Linux 系统 LVM(Logical Volume Manager)逻辑卷管理
一.前言 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到 当前某个分区需要的容量,还要预见该分区以后可能需要 ...
- 自动工作负载库理论与操作(Automatic Workload Repository,AWR)
AWR的由来: 10g之前的oracle:用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接 断开后,其原来 ...
- CENTOS7配置静态IP后无法ping通外部网络的问题
我今天想谈论的并不是如何配置静态IP,这样的话题已经有好多高手再谈. 我想谈的是为什么,我按照他们的教程无论如何也要发生各种问题,没办法连接外网的问题. 先给大家看我的最终版配置方案:我只修改了一个文 ...
- 分布式锁之三:Redlock实现分布式锁
之前写过一篇文章<如何在springcloud分布式系统中实现分布式锁?>,由于自己仅仅是阅读了相关的书籍,和查阅了相关的资料,就认为那样的是可行的.那篇文章实现的大概思路是用setNx命 ...
- 4.solr学习速成之bean
以bean的形式提交索引,以bean的形式查询出来 package com.liucheng.solr; import java.io.Serializable; import org.apache. ...