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[ ...
随机推荐
- mysql锁之Next-Key Locks
一个Next-key锁结合了行锁和gap锁. InnoDB执行一个行级别锁在这样的一个途径,那就是它搜索或者扫描一个表索引时,它设置共享或者独占锁在它遭遇的索引记录上.于是,行级锁是真实的行记录锁.一 ...
- Mybatis与Hibernate的详细对比
前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...
- Django-rest-framework多条件查询/分页/多表Json
http://www.mamicode.com/info-detail-1648765.html
- Oracle cursor_sharing 参数 详解
一. 官网的说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams042.htm#REFRN10025 ...
- 在android开发中添加外挂字体
1.在项目目录中,右键app——New——Folder—— Assets Folder 2.把.ttf或者.oft文件拷进这个assets文件夹 3.在onCreate()中 Typeface typ ...
- 多线程使用信号量sem_init,sem_wait,sem_post
信号量的主要函数有: int sem_init(sem_t *sem,int pshared,unsigned int value); int sem_wait(sem_t *sem); int se ...
- 摆花 (DP动态规划)
2012_p3 摆花 (flower.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 17 解决: 10[提交][状态][讨论版][命题人:外部导入] 题目描述 3. ...
- Java-Maven-Runoob:Maven Eclipse
ylbtech-Java-Maven-Runoob:Maven Eclipse 1.返回顶部 1. Maven Eclipse Eclipse 提供了一个很好的插件 m2eclipse ,该插件能将 ...
- 第九章 Servlet工作原理解析
9.1 从Servlet容器说起 Servlet容器:Jetty, Tomcat等. 这里以Tomcat为例, 真正管理Servlet的容器是Context容器,一个Context对应一个WE ...
- python's twenty-first day for me 抽象类和接口类以及多态
归一化设计: 不管是哪一个类的对象,都调用同一个函数去完成相似的功能. class Alipay: def pay(self,money): print('使用支付宝支付了%s' % money) c ...