【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傻逼题 只是用回文树写写而已.. ...
随机推荐
- 二维码开源库ZBar-MDK STM32F429移植
前两篇文章已经实现ZBar在Windows平台下的编译和使用,本文将介绍如何把ZBar移植到STM32F429,IDE使用MDK. 1. MDK工程设置 (1)不勾选Use MicroLIB ,使用I ...
- LeetCode - 307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- JavaScript使用点滴
JavaScript使用点滴 一.字符串替换的小插曲 遇到一个小插曲,想要把后台返回的字符串输出给前端视图,字符串中包含\n换行,需要使用javascript对其进行替换成<br />. ...
- 安装php的memcached模块和扩展支持sasl
memcached的1.2.4及以上增加了CAS(Check and Set)协议,对于同一key的多进行程的并发处理问题.这种情况其实根数据库很像,如果同时有几个进程对同一个表的同一数据进行更新的话 ...
- bzoj[1835][ZJOI2010]base 基地选址
bzoj[1835][ZJOI2010]base 基地选址 标签: 线段树 DP 题目链接 题解 这个暴力DP的话应该很容易看出来. dp[i][j]表示造了i个通讯站,并且j是第i个的最小费用. \ ...
- 使用PowerDesigner对NAME和COMMENT互相转换
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 在使用PowerDesigner对数据库进行概念模型和物理模型设计时 ...
- 微信小程序 页面跳转传递数据
点击view 跳转页面 <view class="album_image" data-album-obj="{{item}}" bindtap=" ...
- linux磁盘及分区详解
1.Linux 分区简介 1.1 主分区 vs 扩展分区 硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两个分区,一个是主分区(Primary Partion)一个是扩展分区(extend ...
- Java采用内部构造器Builder模式进行对类进行构建
好处: 能保证重叠构造器模式的安全性: 能保证JAVABeans模式的可读性: package cn.lonecloud.builder; /** * 使用内部类构建器来对这个类进行构造 * @Tit ...
- uva208
一道简单的路径打印,首先需要一次dfs判断能否从1到达目标点,否则可能会超时.还有一点就是那个格式需要注意下,每条路径前没有空格(看起来好像有3个空格)-. AC代码: #include<cst ...