You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes.

String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|.

String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1.

Input

The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query.

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

Output

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.

Examples

Input
caaaba
5
1 1
1 4
2 3
4 6
4 5
Output
1
7
3
4
2

Note

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

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath> const int maxn=5e3+;
typedef long long ll;
using namespace std;
char str[maxn]; int a[maxn][maxn],dp[maxn][maxn]; int main()
{
scanf("%s",str+);
int len=strlen(str+);
//cout<<len<<endl;
for(int t=;t<=len;t++)
{
a[t][t]=a[t][t-]=dp[t][t]=;
}
for(int t=;t<=len;t++)
{
for(int j=;j+t-<=len;j++)
{
if(str[j]==str[j+t-]&&a[j+][j+t-]%==)
a[j][j+t-]=;
else
{
a[j][j+t-]=;
}
}
}
for(int t=;t<=len;t++)
{
for(int j=;j+t-<=len;j++)
{
dp[j][j+t-]=dp[j][j+t-]+dp[j+][j+t-]-dp[j+][j+t-]+a[j][j+t-];
}
}
int n;
scanf("%d",&n);
while(n--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
} return ;
}

Queries for Number of Palindromes(区间dp)的更多相关文章

  1. codeforces 245H . Queries for Number of Palindromes 区间dp

    题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...

  2. K - Queries for Number of Palindromes(区间dp+容斥)

    You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There a ...

  3. Codeforces245H - Queries for Number of Palindromes(区间DP)

    题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...

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

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

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

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

  6. Queries for Number of Palindromes (区间DP)

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

  7. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  8. codeforces 245H Queries for Number of Palindromes RK Hash + dp

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

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

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

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

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

随机推荐

  1. Json字符串与QVariantList 对象相互转换

    在Qt中QVariantList 使用起来很方便,如果涉及到数据的传输,需要将QVariantList 数据转换为JsonArray字符串,这个转换Qt已经实现好了,只需要调用接口就可以完成转换,代码 ...

  2. 005_针对于go语言中速率限制的思考

    在之前的go语言的速率限制这篇文章里,我们尝试了普通的速率限制,和脉冲型速率限制.其中,脉冲型速率限制是放开了限制,里面有3个请求是一次性到达,然后再按照200ms的速度限制的,之前的代码如下所示: ...

  3. Python 爬虫工程师必看,深入解读字体反爬虫

    字体反爬虫开篇概述 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人 ...

  4. Java 基础 —— 注解 Annotation

    简介 Annotation 是从 JDK 5.0 引入的. 注解使得我们可以以编译器验证的格式存储程序的额外信息.注解可以生成描述符文件,甚至是新的类定义,并且有助于减轻编写"样板" ...

  5. ASP.NET Core3.x 基础(1)

    ASP.NET Core与2.x相比发生的一些变化: 项目结构 Blazor SignalR gRPC 关于Program类:Main方法,在系统执行时就会找到这个Main方法,实际上是配置了ASP. ...

  6. 2020-07-24:聊一下zookeeper的同步算法。

    福哥答案2020-07-24: 同步算法基于 ZAB 协议,一种快速 Paxos 算法. 快速Paxos算法Paxos算法可能出现死循环,就是在两个Proposer总是在交替prepare.并且,Pa ...

  7. C#LeetCode刷题之#63-不同路径 II​​​​​​​(Unique Paths II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...

  8. MarkDown总结(适合初学者快速入门)

    本文转自https://blog.csdn.net/sun8112133/article/details/79871702 总得的来说,MarkDown是一种简单.轻量级的标记语法,它是基于HTML之 ...

  9. 使用 VMware Workstation Pro 让 PC 提供云桌面服务——学习笔记(二)

    实验效果: 这次希望的效果是能够用远程桌面来实现 . 这里参考了博客 https://www.cnblogs.com/wwang/archive/2011/01/06/1928933.html 操作步 ...

  10. 极简 Node.js 入门 - 3.1 File System API 风格

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...