H. Queries for Number of Palindromes
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
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<stdio.h>
#include<string.h>
int dp[][],and1[][];
int main()
{
char s[];
int q,l,r,sta,end,len;
int i;
gets(s);
len=strlen(s);
for(i=; i<len; i++)
{
dp[i][i]=and1[i][i]=;
and1[i+][i]=;
}
for(i=;i<=len;i++)
for(sta=;sta<len;sta++)
{
end=sta+i-;
if(and1[sta+][end-]&&s[sta]==s[end])
and1[sta][end]=;
dp[sta][end]=dp[sta+][end]+dp[sta][end-]-dp[sta+][end-]+and1[sta][end];
}
scanf("%d",&q);
while(q--)
{
scanf("%d %d",&l,&r);
printf("%d\n",dp[l-][r-]);
}
return ;
}

Queries for Number of Palindromes(求任意子列的回文数)的更多相关文章

  1. 求第N个回文数 模板

    备忘. /*看到n可以取到2*10^9.说明普通方法一个个暴力计算肯定会超时的,那打表呢?打表我们要先写个打表的代码,这里不提供.打完表观察数据,我们会发现数据其实是有规律的.完全不需要暴力的把所有数 ...

  2. 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 ...

  3. 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 ...

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

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

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

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

  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. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  8. hdu5340—Three Palindromes—(Manacher算法)——回文子串

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. 4190. Prime Palindromes 一亿以内的质数回文数

    Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...

随机推荐

  1. 为何被主流抛弃-江西IDC机房价格为何居高不下缺少竞争力-2014年5月江西IDC排行榜

     经常有人问江西IDC排行榜,为什么江西市场缺乏活力. 榜单调研者们有时仅仅能表示无解和无奈. 在IDC领域,其实已经形成了一二三线的城市之分. 一线城市是以上海.北京.深圳为代表的拥有最早国际宽 ...

  2. DirectX11 学习笔记7 - 支持自由移动的摄像机

    如今将又一次制定一个camera摄像机.能够自由移动. 比方前进 后退,上游 下潜. 各个方向渲染之类的. 首先设置按键. 这个时候须要在 XWindow.h 里面 bool XWindow::fra ...

  3. tt1

    DIm #include iostream.h#include iostream.h# #include iostream.h http://www.cnblogs.com/cmt/archive/2 ...

  4. c# WinForm的一些问题

    工作中,用WinForm写了一段程序,刚开始运行正常,后来替换为公司框架的时候,发现原来用Label拼的表格控件,里面的Text无法显示,后来发现,父控件的ForColor为Control导致,子空间 ...

  5. 2016/2/24 . html . htm . shtml 的区别

    htm.html.shtml网页区别     htm.html.shtml区别 接下来我们来了解下htm.shtml.html这三者之间区别.首先htm.html.shtml都是静态网页的后缀,三者也 ...

  6. struts2 下载

    struts  官网 : https://struts.apache.org/

  7. YTU 2535: C++复数运算符重载(+与<<)

    2535: C++复数运算符重载(+与<<) 时间限制: 1 Sec  内存限制: 128 MB 提交: 867  解决: 532 题目描述 定义一个复数类Complex,重载运算符&qu ...

  8. BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho

    BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho Description Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...

  9. Java 泛型 五:泛型与数组

    简介 上一篇文章介绍了泛型的基本用法以及类型擦除的问题,现在来看看泛型和数组的关系.数组相比于Java 类库中的容器类是比较特殊的,主要体现在三个方面: 数组创建后大小便固定,但效率更高 数组能追踪它 ...

  10. 23. Ext xtype : "combo" 下拉选择框

    转自:https://blog.csdn.net/majishushu/article/details/52601161