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. 一文打尽Java继承的相关问题

    相关文章: <面向对象再探究>:介绍了面向对象的基本概念 <详解Java的对象创建>:介绍了对象的创建.构造器的使用 在<面向对象再探究>这篇文章中已经笼统的介绍过 ...

  2. Flask框架(二):路由与蓝图

    一.路由 使用 route() 装饰器来把函数绑定到 URL: @app.route('/') def index(): return 'Index Page' @app.route("/h ...

  3. 为写程序而生的连字字体 Fira Code

    Fira Code,等宽的编程连字字体 **等宽 ** 是指所有字符的宽度相同,如: W 和 i 用一样的宽度去显示 连字(ligatures)为文字排印的一个特性,比如「f」和「 i」放在一起的时候 ...

  4. stat 命令家族(1)- 详解 vmstat

    性能测试必备的 Linux 命令系列,可以看下面链接的文章哦 vmstat 介绍 Virtual Meomory Statistics,报告虚拟内存统计信息 会统计进程信息.内存.交换区.IO.磁盘. ...

  5. python3.3while循环

    #while循环与for循环不同的是,while循环的停止条件是自己设置! i=0#初始值while i<=10:#循环条件 print(i)#while循环嵌套if条件 if i==5: pr ...

  6. 趣讲 PowerJob 超强大的调度层,开始表演真正的技术了

    本文适合有 Java 基础知识的人群 作者:HelloGitHub-Salieri HelloGitHub 推出的<讲解开源项目>系列. 写在前面的碎碎念:终于到了万众期待的调度层原理了. ...

  7. 23、Interpreter 解释器模式

    1.Interpreter 解释器模式 解释器模式是一种使用频率相对较低但学习难度较大的设计模式,它用于描述如何使用面向对象语言构成一个简单的语言解释器.在某些情况下,为了更好地描述某一些特定类型的问 ...

  8. linux7手动添加systemctl启动程序

    1.路径 [root@angrymushrooms01 system]# pwd /usr/lib/systemd/system 2.脚本内容 vim xxx.service ------------ ...

  9. BN系列-未完待续

    BN.LN.IN.GN Cross-Iteration Batch Normalization 因为有时候我们的计算能力有限,所以BN设置的比较小,这样BN效果就比较差. 因此我们将最近几次的batc ...

  10. springboot中使用定时器

    springboot使用注解注册定时器 @Configuration @EnableScheduling public class WeatherDataTask implements Schedul ...