【CF245H】Queries for Number of Palindromes(回文树)
【CF245H】Queries for Number of Palindromes(回文树)
题面
题解
回文树,很类似原来一道后缀自动机的题目
后缀自动机那道题
看到\(n\)的范围很小,但是\(Query\)很多
所以提前预处理出每一段\(l,r\)的答案
时间复杂度\(O(n^2+Q)\)
#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 5050
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 ans[MAX][MAX];
char s[MAX];
char ch[MAX];
struct PT
{
struct Node
{
int son[26];
int len,ff;
}t[MAX];
int last,tot,dep[MAX];
void init()
{
t[tot=1].len=-1;
t[last=0].ff=t[1].ff=1;
}
void clear()
{
memset(t,0,sizeof(t));
memset(dep,0,sizeof(dep));
init();
}
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].ff=t[k].son[c];
t[v].len=t[p].len+2;
t[p].son[c]=v;
dep[v]=dep[t[v].ff]+1;
}
last=t[p].son[c];
}
}PT;
int main()
{
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;++i)
{
PT.clear();
for(int j=i;j<=n;++j)ch[j-i+1]=s[j];
for(int j=i;j<=n;++j)
{
PT.extend(ch[j-i+1]-97,j-i+1,ch);
ans[i][j]=ans[i][j-1]+PT.dep[PT.last];
}
}
int Q=read();
while(Q--)
{
int l=read(),r=read();
printf("%d\n",ans[l][r]);
}
return 0;
}
【CF245H】Queries for Number of Palindromes(回文树)的更多相关文章
- SPOJ Number of Palindromes(回文树)
Number of Palindromes Time Limit: 100MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu ...
- SP7586 NUMOFPAL - Number of Palindromes(回文树)
题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...
- CF245H Queries for Number of Palindromes(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
- [CF245H] Queries for Number of Palindromes (容斥原理dp计数)
题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...
- cf245H Queries for Number of Palindromes (manacher+dp)
首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...
- CF245H Queries for Number of Palindromes
题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
随机推荐
- 插上腾飞的翅膀:为asp.net core添加protobuf支持
没时间解释了,快上车. 通过NuGet获取Zaabee.AspNetCoreProtobuf Install-Package Zaabee.AspNetCoreProtobuf 在Startup.cs ...
- 关于http与https之间的区别
年前的时候进行了一家公司的电话面试两轮,视频面试一轮(已拿到offer),过程中遇到了一个关于http与https的问题,当时回答的并不好,今天将其进行了总结和整理,望读者喜欢: 前言 谷歌在2018 ...
- MySQL 存储过程的简单使用
首先创建一张 students 表 SQL脚本如下: create table students( id int primary key auto_increment, age int, name v ...
- OpenCV角点检测源代码分析(Harris和ShiTomasi角点)
OpenCV中常用的角点检测为Harris角点和ShiTomasi角点. 以OpenCV源代码文件 .\opencv\sources\samples\cpp\tutorial_code\Trackin ...
- Mac下安装php5.6/7.1
安装环境 OS X EI Capitan 10.11.4 Homebrew安装 homebrew是一个类似于ubuntu中apt-get的一个软件管理器,安装比较简单,在命令行中输入如下代码: rub ...
- Redis 实践3-操作
string常用操作 set key1 aminglinux get key1 set key1 aming //一个key对应一个value,多次赋值,会覆盖前面的value setnx k ...
- 图论算法-网络最大流【EK;Dinic】
图论算法-网络最大流模板[EK;Dinic] EK模板 每次找出增广后残量网络中的最小残量增加流量 const int inf=1e9; int n,m,s,t; struct node{int v, ...
- 我的2017年终总结(PF项目框架设计心得分享 1.0rc new)
一晃眼又过去了一年,在这一年里尽管有许多不如意的事,却阻挡不了我前进的脚步.先用一句话来总结去年一年的状态,那就是“无休无止的忙碌”.而这样的忙碌状态对我来说是不可取的,因为匮乏的忙碌只能让头脑处于一 ...
- [转载]LVS+Keepalived之三大模式
LVS + Keepalived之三大模式 ============================================================================== ...
- Python个人总结_02
个人学习总结: python 第二课 解释型和编译型语言 计算机是不能够识别高级语言的,当我们运行一个高级语言的时候,需要将高级语言 翻译成计算机能够读懂的机器语言.这个过程分为两类,一个是编译,一个 ...