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

【题意】给你一个字符串,Q次询问,每次给出一个区间问这个区间内有多少个回文字符串。

【分析】ispal[i][j]表示字符串从 i 到 j 这一段是否为回文,若是,则为1。不难得到初始化时ispal[i][i] = 1。那么,对于长度为len的字符串,判断它是否为回文,则有ispal[i][i+len-1] = ispal[i+1][i+len-2]&&str[i]==str[i+len-1];同时ispal[i][i-1]要也要初始化为1,因为当len=2时,ispal[i+1][i+len-2] = ispal[i+1][i],需要拿来计算。

dp[i][j]表示从 i 到 j 包含的回文串的个数,同样的,初始化dp[i][i] = 1。计数时有:
dp[i][i+len-1] = dp[i][i+len-2]+dp[i+1][i+len-1]-dp[i+1][i+len-2]+ispal[i][i+len-1];然后O(n^2)把每个区间的结果计算出来,对于询问s到e,直接输出dp[s][e]即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 5e3+;;
const int M = ;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n;
char str[N];
int ispal[N][N],dp[N][N];
int main(){
scanf("%s",str+);
int len=strlen(str+);
for(int i=;i<=len;i++)ispal[i][i]=ispal[i][i-]=dp[i][i]=;
for(int l=;l<=len;l++){
for(int i=;i+l-<=len;i++){
ispal[i][i+l-]=ispal[i+][i+l-]&(str[i]==str[i+l-]);
}
}
for(int l=;l<=len;l++){
for(int i=;i+l-<=len;i++){
dp[i][i+l-]=dp[i][i+l-]+dp[i+][i+l-]-dp[i+][i+l-]+ispal[i][i+l-];
}
}
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. dp --- Codeforces 245H :Queries for Number of Palindromes

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

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

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

    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. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  2. jquery 遮罩层显示img

    如果点击iframe中的image显示整个页面的遮罩层,可参考如下: http://blog.csdn.net/shiaijuan1/article/details/70160714 具体思路就是,顶 ...

  3. Creating a new dynamic form project, business modeling.

    The domain logic is like there are a bunch of objects, as well as a lot of configurations, according ...

  4. ASP.NET 简单鼠标右键效果contextmenutrip

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  5. hdu 2795 Billboard(线段树+单点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others ...

  6. bootstrap-table设置某列序号自增

    col = [{ field: 'SerialNumber', title: '序号', formatter: function (value, row, index) { return index+ ...

  7. Kaggle机器学习之模型集成(stacking)

    Stacking是用新的模型(次学习器)去学习怎么组合那些基学习器,它的思想源自于Stacked Generalization这篇论文.如果把Bagging看作是多个基分类器的线性组合,那么Stack ...

  8. perl HTML::HeadParser获取html头部信息

    use LWP::Simple; use HTML::HeadParser; use utf8; binmode(STDOUT, ":encoding(gbk)"); #设置win ...

  9. 【转】CVE-2010-4258 漏洞分析

    一. 漏洞简介 CVE-2010-4258这个漏洞很有意思,主要思路是如果通过clone函数去创建进程,并且带有CLONE_CHILD_CLEARTID标志,那么进程在退出的时候,可以造成内核任意地址 ...

  10. centos_7.1.1503_src_4

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ libkcompactdisc-4.10.5-3.el7.src.rpm 05-Jul-20 ...