题意翻译

题目描述

给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串。

输入格式

第1行,给出s,s的长度小于5000 第2行给出q(1<=q<=10^6) 第2至2+q行 给出每组询问的l和r

输出格式

输出每组询问所问的数量。

题目描述

You've got a string s=\(s_{1}\)\(s_{2}\)...\(s_{|s|}\) of length |s| , consisting of lowercase English letters. There also are qq queries, each query is described by two integers \(l_{i}\),\(r_{i}\) (1<=\(l_{i}\)<=\(r_{i}\)<=|s|) . The answer to the query is the number of substrings of string \(s[\) \(l_{i}\) ... \(r_{i}\) \(]\) , which are palindromes.

String \(s[l...\ r]\)=\(s_{l}\)\(s_{l+1}\)...\(\ s_{r}\)(1<=l<=r<=∣s∣) is a substring of string \(s=s_{1}s_{2}...\ s_{|s|}\) .

String tt is called a palindrome, if it reads the same from left to right and from right to left. Formally, if \(t=t_{1}t_{2}...\ t_{|t|}=t_{|t|}t_{|t|-1}...\ t_{1}\).

输入输出格式

输入格式:

The first line contains string ss (1<=|s|<=5000) . The second line contains a single integer qq (1<=q<=106) — the number of queries. Next qq lines contain the queries. The ii -th of these lines contains two space-separated integers \(l_{i}\),\(r_{i}\) (1<=\(l_{i}\)<=\(r_{i}\)<=|s|) — the description of the i-th query.

It is guaranteed that the given string consists only of lowercase English letters.

输出格式:

Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.

输入输出样例

输入样例#1: 复制

caaaba

5

1 1

1 4

2 3

4 6

4 5

输出样例#1: 复制

1

7

3

4

2

说明

Consider the fourth query in the first test case. String \(s[4...\ 6]\) = «aba». Its palindrome substrings are: «a», «b», «a», «aba».


题解

这个题目的思路非常巧妙?

因为时间复杂度允许达到\(n\)2,于是我们就从1开始一直到strlen(s),\(l\)的位置每向后移动一位就清空回文树,并把这个\(l...len\)的回文串重新放入回文树。用\(ans[l][r]\)来统计一下答案

然后o(1)查询就OK了。

然后我yy了一下莫队?

是不是离线的话时间复杂度就降到了\(n{\sqrt n}\)了呢?


代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int fail,len,ch[26],dep;
}t[5001];
int tot,k,ans[5001][5001];
char s[5001],ch[5001];
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} void clear()
{
memset(t,0,sizeof(t));
tot=1;k=0;t[0].fail=t[1].fail=1;t[1].len=-1;
} void solve()
{
int n=strlen(ch+1);
for(int i=1;i<=n;i++)
{
clear();
for(int j=i;j<=n;j++)s[j-i+1]=ch[j];
for(int j=i;j<=n;j++)
{
//clear();
while(s[j-i+1-t[k].len-1]!=s[j-i+1])k=t[k].fail;
if(!t[k].ch[s[j-i+1]-'a']){
t[++tot].len=t[k].len+2;
int l=t[k].fail;
while(s[j-i+1-t[l].len-1]!=s[j-i+1])l=t[l].fail;
t[tot].fail=t[l].ch[s[j-i+1]-'a'];
t[k].ch[s[j-i+1]-'a']=tot;
t[tot].dep=t[t[tot].fail].dep+1;
}
k=t[k].ch[s[j-i+1]-'a'];
ans[i][j]=ans[i][j-1]+t[k].dep;
}
}
} int main()
{
scanf("%s",ch+1);
solve();
int q=read();
for(int i=1;i<=q;i++)
{
int l=read(),r=read();
printf("%d\n",ans[l][r]);
}
return 0;
}

CF245H Queries for Number of Palindromes(回文树)的更多相关文章

  1. SPOJ Number of Palindromes(回文树)

    Number of Palindromes Time Limit: 100MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu ...

  2. SP7586 NUMOFPAL - Number of Palindromes(回文树)

    题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...

  3. [CF245H] Queries for Number of Palindromes (容斥原理dp计数)

    题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...

  4. cf245H Queries for Number of Palindromes (manacher+dp)

    首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...

  5. CF245H Queries for Number of Palindromes

    题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...

  6. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  7. Queries for Number of Palindromes(求任意子列的回文数)

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  8. 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,回答区间 ...

  9. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

随机推荐

  1. JQuery实现的弹窗效果

    这是笔者实际项目中的一个需求,我们先来看看特效. 页面加载时弹出窗口,点击关闭按钮,窗口消失并呈现动画效果. 实现代码如下: <!DOCTYPE HTML PUBLIC "-//W3C ...

  2. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  3. row_number函数的使用

    转 row_number函数的使用 SQL Server数据库ROW_NUMBER()函数的使用是本文我们要介绍的内容,接下来我们就通过几个实例来一一介绍ROW_NUMBER()函数的使用. 实例如下 ...

  4. <Three.js>(第三节)全景漫游

    一.实验内容 通过上次实验,了解了Three.js创建场景的基本步骤.这一节,我们将通过Three.js实现全景漫游功能.如下图: 全景图是获取一个3D场景中的不同角度的图片,然后通过拼接.融合实现3 ...

  5. 读 Real-Time Rendering 收获 - chapter 6. texturing

    Texturing, at its simplest, is a techinique for efficiently modeling the surface's properties.

  6. Windows7下Thingworx 7安装

    1.环境准备 Windows7 64位+JDK8+Tomcat8+PostgreSQL9 2.安装JDK8和Tomcat8 这里没有什么太多值得叙述的,基本都是一路下一步. 需要注意的是,一个是Tom ...

  7. linux查看系统cpu信息

    # 查看物理CPU个数 cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l # 查看每个物理CPU中core的个数(即 ...

  8. [agc016d]xor replace

    题意: 题解: 棒棒的神仙题...这题只是D题???(myh:看题五分钟,讨论两小时) 首先这个异或和是假的,比如我现在有$a=(a_1,a_2,a_3,a_4)$,操作一下$a_2$,就变成了$a= ...

  9. POJ 2228 Naptime(DP+环形处理)

    题解 这题一眼望去DP. 发现自己太智障了. 这题想的是O(n^3m)的. 环形处理只会断环成链....然后DP也想的不好. 我们先考虑如果除去环这题该怎么做? dp[i][j][0/1]代表到第i小 ...

  10. vuex 基本入门和使用(三)-关于 mutation

    vuex 基本入门和使用(三)-关于 mutation vuex 版本为^2.3.1,按照我自己的理解来整理vuex. 关于 mutation 这里应该很好理解. 更改 Vuex 的 store 中的 ...