【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
题面
洛谷
求一个串中包含几个回文串
题解
Manacher傻逼题
只是用回文树写写而已。。
#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 10000
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;
}
int n;
char s[MAX];
int size[MAX];
struct PT
{
struct Node
{
int son[26];
int ff,len;
}t[MAX];
int last,tot;
void init()
{
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;
while(s[n-t[k].len-1]!=s[n])k=t[k].ff;
t[v].len=t[p].len+2;
t[v].ff=t[k].son[c];
t[p].son[c]=v;
}
++size[last=t[p].son[c]];
}
void Calc()
{
for(int i=tot;i;--i)size[t[i].ff]+=size[i];
}
}PT;
int ans;
int main()
{
PT.init();
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;++i)PT.extend(s[i]-97,i,s);
PT.Calc();
for(int i=2;i<=PT.tot;++i)ans+=size[i];
printf("%d\n",ans);
return 0;
}
【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)的更多相关文章
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- CF245H Queries for Number of Palindromes(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
- 【Aizu2292】Common Palindromes(回文树)
[Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...
- CF17E Palisection(manacher/回文树)
CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...
- BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)
BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...
- SPOJ Number of Palindromes(回文树)
Number of Palindromes Time Limit: 100MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu ...
- SP7586 NUMOFPAL - Number of Palindromes 解题报告
SP7586 NUMOFPAL - Number of Palindromes 题意翻译 求一个串中包含几个回文串 输入输出格式 输入格式: The string S. 输出格式: The value ...
- URAL 2040 Palindromes and Super Abilities 2(回文树)
Palindromes and Super Abilities 2 Time Limit: 1MS Memory Limit: 102400KB 64bit IO Format: %I64d ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...
随机推荐
- 【国家集训队2010】小Z的袜子[莫队算法]
[莫队算法][国家集训队2010]小Z的袜子 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程, ...
- 关于echarts 报错 初始化对象未定义
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 腾讯云微计算实践:从Serverless说起,谈谈边缘计算的未来
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:黄文俊,腾讯云高级产品经理,曾经历过企业级存储.企业级容器平台等产品的架构与开发,对容器.微服务.无服务器.DevOps等都有浓厚兴趣. ...
- Spring-Security+Freemarker 开启跨域请求伪造防护功能
CSRF简介--摘抄自<Spring实战(第4版)> 我们可以回忆一下,当一个POST请求提交到"/spittles"上时,SpittleController ...
- Mysql 远程登录及常用命令
第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密码 键入命令mysql -uro ...
- PHP的错误机制
特别说明:文章的PHP版本使用5.5.32 PHP的错误级别 首先需要了解php有哪些错误.截至到php5.5,一共有16个错误级别 注意:尝试下面的代码的时候请确保打开error_log: erro ...
- Shell脚本查看linux系统性能瓶颈(转)
Shell脚本查看linux系统性能瓶颈(转自:http://blog.51cto.com/lizhenliang/1687612) [root@test ~]# cat show_sys_info. ...
- 1.3 fractions模块
数学世界中,浮点数还可以用分数形式展示,不可约简的分数形式往往更简洁直观. 问题来了,Python中如何输出不可约简的分数形式呢? 答案:用Fraction类来实现.这个类属于标准库的fracti ...
- jsp去除空行的web.xml配置
在jsp中我们引入的标签,例如jstl的标签,循环遍历等等,可能会产生很多空行,其实也没什么,不会影响展示,但是空行多多少少会影响性能,这是我们只需要在web.xml中配置一下我们就可以很简单的去掉, ...
- POJ1639 - Picnic Planning
原题链接 Description 给出一张个点的无向边权图并钦定点,求使得点的度不超过的最小生成树. Solution 首先无视掉与相连的所有边,原图会变成若干互不连通的个块.对每个块分别求MST,再 ...