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

题意:求一个区间内回文串的数量

思路:要利用一个判断l~r区间的回文数组来判断这个区间是否是回文串是则为1不是则为0 然后定义一个dp数组来记录l~r区间内回文串的数量

代码:

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

K - 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. Codeforces245H - Queries for Number of Palindromes(区间DP)

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

  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. Palindrome subsequence(区间dp+容斥)

    In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting so ...

  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. 【CF245H】Queries for Number of Palindromes(回文树)

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

随机推荐

  1. 企业签名和TF签名哪个好?TF签名和企业签名怎么选?

    很多开发者在App无法上架Appstore,需要内测或者开放给苹果用户使用的时候,需要选择企业签名来帮助自己的App开放下载链接,给苹果用户使用.苹果企业签名的类型有很多,TF签名最近又很火爆,那么企 ...

  2. java多线程编程实例

    [转]这篇文章主要介绍了java多线程编程实例,分享了几则多线程的实例代码,具有一定参考价值,加深多线程编程的理解还是很有帮助的,需要的朋友可以参考下. 1.三个售票窗口同时出售20张票程序分析:   ...

  3. 033_go语言中的打点器

    代码演示 package main import "fmt" import "time" func main() { ticker := time.NewTic ...

  4. C#-用Winform制作一个简单的密码管理工具

    为什么要做? 首先是为了练习一下c#. 想必大家都有过记不起某个平台的账号密码的经历,那种感受着实令人抓狂.那这么多账号密码根本记不住!我之前用python写过一个超级简单(连账号信息都写在代码里那种 ...

  5. 11 Linux(CentOS)用户与权限

    用户管理 root 在root用户下可以进行以下操作 useradd 用户名      新增用户 userdel 用户名       删除用户,同时应该删除家目录下的用户文件,与邮箱目录下的用户文件 ...

  6. tableauRFM分析

    1.数据源 2.创建相关字段 2.1 购买点会员生命期 2.2 会员最后购买时间 2.3 最后购买点生命期 3.近一个月老客户的生命期情况 排除了当天创建当天购买的情况,可以看到超市的大部分用户是两年 ...

  7. 2020-07-10:sql如何调优?

    福哥答案2020-07-10:此答案来自群成员: SQL提高查询效率的几点建议 1.如果要用子查询,那就用EXISTS替代IN.用NOT EXISTS替代NOT IN.因为EXISTS引入的子查询只是 ...

  8. 2020-05-08:mycat部署数据库集群的时候 遇到了哪些坑

    福哥答案2020-05-08:答案仅供参考,来自群员 使用activity时,连接mycat设置进去的序列化的流程变量,反序列化会报错这个类型字段类型是blob类型,mycat对这种类型处理时有点问题

  9. JavaScript Object初始化的不同方式

    不带原型的对象,纯对象 const plaintObject = Object.create(null) 带原型的对象 const originObject = new Object()

  10. HBase存储及读写原理介绍

    一.HBase介绍及其特点 HBase是一个开源的非关系型分布式数据库,它参考了谷歌的BigTable建模,实现的编程语言为Java.它是Apache软件基金会的Hadoop项目的一部分,运行于HDF ...