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[ ...
随机推荐
- CentOS7 配置光盘iso镜像为本地yum源
因为系统使用的最小化安装,所以很多软件没有安装上,又无法上网,为了方便,所以直接将上传的iso镜像直接作为yum源. 我已经将光盘的iso镜像上传至服务器. 接下来就是将iso的光盘镜像进行挂载了 m ...
- 十三、python沉淀之路--文件操作
一.文件的读操作 例1 f = open('学习',encoding='utf-8') #首先要打开文件,不然直接读,是读不出来的 data = f.read() #read后的括号里不添加任何东西 ...
- JAMstack 技术要点
1. 简要说明 Modern web development architecture based on client-side JavaScript, reusable APIs,and preb ...
- Linux环境安装redis
redis官网地址:http://www.redis.io/ 最新版本:2.8.3 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget ...
- Mouse without Borders 跨屏鼠标
Mouse without Borders 跨屏鼠标 软件方式 当你有几台电脑同时工作时,不停的换鼠标和键盘是非常不方便的. 如果用的 Mouse without Borders 就不一样了,可以在多 ...
- 双击 cui
//改变属性块的双击事件 //将菜单文件中的双击改一下,退出时还原文件 acad.bak.cui 改这个名字 每次用这个更新为新的 acad.cui进行修改 //退出时再用 acad.bak.cui还 ...
- Object-C 多线程中锁的使用-NSLock
在多线程的编程环境中,锁的使用必不可少! 于是,今天来总结一下为共享资源加锁的操作方法. 一.使用synchronized方式 //线程1 dispatch_async(dispatch_ge ...
- Java-API-Package:java.sql百科
ylbtech-Java-API-Package:java.sql百科 提供使用 JavaTM 编程语言访问并处理存储在数据源(通常是一个关系数据库)中的数据的 API.此 API 包括一个框架,凭借 ...
- thinkphp中的验证码的实现
1.php端生成验证码函数 public function verify(){ // 验证码 import("@.Util.Image"); Image::buildImageVe ...
- C#获取网络状态
/// <summary> /// 获取网络状态 /// </summary> /// <param name="ip">目标IP地址</ ...