【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 ...
随机推荐
- JSP的几种跳转方式的异同
1 <jsp:foward page="url" /> 服务端跳转,立即跳转,后续语句不会执行: 2 <% response.sendRedirect(" ...
- linux 下创建GRE隧道
其他国家的互联网如同一个孤岛.要想访问国外网站异常的缓慢,甚至被和谐了.可以建立一条隧道来避免这种情况,下面说说GRE隧道如何建立. 1. GRE介绍 GRE隧道是一种IP-over-IP的隧道,是通 ...
- 通过读取配置文件,启动mongodb
在实际的项目中,经常利用mongodb数据库做缓存,mongodb的并发性比较高,所以对于快速存储.读取信息有很多优点.在项目中对于第一次的数据请求会直接访问数据库,而对于获得的信息通常都会在此时刻存 ...
- statement preparestatement CallableStatement
大家都知道Statement.PrepareStatement 和CallableStatement 对象,其实它们是interface,为什么JDBC2.0中要提供这三个对象呢?对于Statemen ...
- 将Tomcat配置到你的mac电脑上,命令行启动tomcat
1.下载tomcat7文件 2.编辑打开.bash_profile vim .bash_profile 3.在该文件最后面添加(CATALINA_HOME为tomcat解压目录) CATALINA_H ...
- 手把手教你在Ubuntu上分别安装Nginx、PHP和Mysql
手把手教你在Ubuntu上分别安装Nginx.PHP和Mysql
- Mysql的锁机制与PHP文件锁处理高并发简单思路
以购买商品举例: ① 从数据库获取库存的数量. ② 检查一下库存的数量是否充足. ③ 库存的数量减去买家购买的数量(以每个用户购买一个为例). ④ 最后完成购买. 仅仅这几行逻辑代码在并发的情况下会出 ...
- MFRC522
https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md https://github.com/mxgx ...
- ZOJ - 3818 字符串
思路:对于"ABABA"可以先枚举"AB",然后检查下一个串是否等于"AB",剩下的串就是A,最后检查A和B是否相等:对于"ABA ...
- Caffe可视化之VisualDL
Visual DL是由 PaddlePaddle 和 ECharts 合作推出的一款深度学习可视化工具,其能够可视化scalar.参数分布.模型结构.图像等.底层采用C++编写,上层SDK以pytho ...