【CodeChef】Palindromeness(回文树)
【CodeChef】Palindromeness(回文树)
题面
题解
构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点
定义\(half\)表示长度最长并且长度小于等于当前节点长度一半的回文串所代表的节点
\(half\)的求法,如果当前点的\(len=1\),\(half\)不存在
否则,从构建回文树时的父亲节点(不是\(fail\)指针)所代表的那个点的\(half\)开始
暴力跳\(fail\),直到找到满足条件的点,假设是\(pos\)
那么,当前点的\(half\)就是\(trans[pos][当前字符]\)
完全不记得回文树怎么写了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 111111
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int len,ff;
int son[26];
}t[MAX];
struct PAM
{
int last,tot;
ll cnt[MAX];
int val[MAX],half[MAX];
void init()
{
memset(t,0,sizeof(t));memset(cnt,0,sizeof(cnt));
t[tot=1].len=-1;t[0].ff=t[1].ff=1;
}
void extend(int c,int n,char *s)
{
int p=last;
while(s[n-t[p].len-1]!=s[n])p=t[p].ff;
if(!t[p].son[c])
{
int v=++tot,k=t[p].ff;
t[v].len=t[p].len+2;
while(s[n-t[k].len-1]!=s[n])k=t[k].ff;
t[v].ff=t[k].son[c];
t[p].son[c]=v;
if(t[v].len==1)half[v]=0;
else
{
int pos=half[p];
while(s[n-t[pos].len-1]!=s[n]||(2+t[pos].len)*2>t[v].len)pos=t[pos].ff;
half[v]=t[pos].son[c];
}
val[v]=1+(t[v].len/2==t[half[v]].len?val[half[v]]:0);
}
last=t[p].son[c];
cnt[last]++;
}
ll Calc()
{
ll ret=0;
for(int i=tot;i;--i)cnt[t[i].ff]+=cnt[i];
for(int i=tot;i;--i)ret+=cnt[i]*val[i];
return ret;
}
}PT;
char ch[MAX];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%s",ch+1);
PT.init();
for(int i=1,l=strlen(ch+1);i<=l;++i)PT.extend(ch[i]-97,i,ch);
printf("%lld\n",PT.Calc());
}
}
【CodeChef】Palindromeness(回文树)的更多相关文章
- HDU3948 & 回文树模板
Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【CF17E】Palisection(回文树)
[CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【BZOJ2160】拉拉队排练(回文树)
[BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...
- 【CF932G】Palindrome Partition(回文树,动态规划)
[CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...
- 【BZOJ2342】双倍回文(回文树)
[BZOJ2342]双倍回文(回文树) 题面 BZOJ 题解 构建出回文树之后 在\(fail\)树上进行\(dp\) 如果一个点代表的回文串长度为\(4\)的倍数 并且存在长度为它的一半的回文后缀 ...
- 【BZOJ2565】最长双回文串(回文树)
[BZOJ2565]最长双回文串(回文树) 题面 BZOJ 题解 枚举断点\(i\) 显然的,我们要求的就是以\(i\)结尾的最长回文后缀的长度 再加上以\(i+1\)开头的最长回文前缀的长度 至于最 ...
随机推荐
- 编译Chromium出现warning C4819的解决办法
编译Chromium时出现 warning C4819: The file contains a character that cannot be represented in the current ...
- fiddler不经意的功能
捕获指定客户端的请求,直接食用 窗口分离,直接食用 Hide this column 隐藏此列Ensure all columns are visible 显示默认所有列Customize co ...
- Android intel X86 图像渲染
最近几天有个项目需要在intel 芯片的系统上集成我们的视频通话软件.之前只是在ARM平台上使用,对于intel 没测试过,直接运行apk后,本端渲染的图像出错,渲染出的图像很像I420被作为RGB5 ...
- oracle 学习随笔一: 字段大小写
字段上加大小写:"reportId" 即可
- 聊一聊session
最近从上家公司离职了,到了一家新公司,这几天一直在了解他们的项目,所以我自己的那个小项目也暂时搁浅了.. 今天差不多把他们的项目了解了,来院子写写我在这里边遇到的问题,影响最深刻的是seesion的. ...
- Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )
Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...
- [leetcode-666-Path Sum IV]
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- BZOJ 3166 HEOI2013 ALO 可持久化trie+st表
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3166(洛谷上也有) 题意概述: 给出一个序列,对于一个区间,其权值为区间中的次大值亦或区 ...
- Python中的from等价于import的语法
Python中导入module文件有两种方式:import和from.这里并不会列举import和from的具体使用方法,而是比较两者之间的差别. 对于from语句来说,它其实是等价于下面的impor ...
- 常用算法Java实现之直接插入排序
直接插入排序是将未排序的数据插入至已排好序序列的合适位置. 具体流程如下: 1.首先比较数组的前两个数据,并排序: 2.比较第三个元素与前两个排好序的数据,并将第三个元素放入适当的位置: 3.比较第四 ...