题干:

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

题目大意:

现有一个字符串 s = s1s2... s|s| of length |s|, 由小写字符组成. 现在有 q 次查询, 每次查询给两个整数 li, ri (1 ≤ li ≤ ri ≤ |s|). 每次查询你的程序要给出此字符串的子串 s[li... ri]有多少个回文串.

解题报告:

显然是区间dp,,刚开始写搓了,还是区间dp不太熟练。。

正经题解:先处理处长度为2的来,并且预处理出任意 l r 子串是否是回文串。然后从len=3开始跑区间dp就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 5000 + 5;
char s[MAX];
int dp[MAX][MAX];
bool is[MAX][MAX];
int l,r;
int main()
{cin>>(s+1);int n = strlen(s+1);for(int i = 1; i<=n; i++) dp[i][i] = 1,is[i][i]=1;for(int i = 1; i<=n-2+1; i++) {dp[i][i+1]=2;  if(s[i] == s[i+1]) dp[i][i+1]++,is[i][i+1]=1;}for(int len = 3; len<=n; len++) {for(int l = 1; l<=n-len+1; l++) {int r = l+len-1;if(s[l] == s[r]) is[l][r] = is[l+1][r-1];}}for(int len = 3; len<=n; len++) {for(int l = 1; l<=n-len+1; l++) {int r = l+len-1;dp[l][r] = dp[l+1][r] + dp[l][r-1] - dp[l+1][r-1];if(is[l][r]) dp[l][r]++;}}int q;cin>>q;while(q--) {scanf("%d%d",&l,&r);printf("%d\n",dp[l][r]);}return 0 ;}

另一个AC代码:(好像我写的那个跑1600ms,,这个代码跑200+ms))本来以为可能是用了按位与,,但是发现这样跑出来也不是很快啊1700+ms。。。可能是数据加强了?

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 5000 + 5;
char s[MAX];
int dp[MAX][MAX];
bool is[MAX][MAX];
int l,r;
int main()
{cin>>(s+1);int n = strlen(s+1);for(int i = 1; i<=n; i++) dp[i][i] = 1,is[i][i]=1,is[i+1][i]=1;
//  for(int i = 1; i<=n-2+1; i++) {
//      dp[i][i+1]=2;
//      if(s[i] == s[i+1]) dp[i][i+1]++,is[i][i+1]=1;
//  }for(int len = 1; len<=n; len++) {for(int j = 1; j<=n; j++) {is[j][j+len] = is[j+1][j+len-1] & (s[j] == s[j+len]);dp[j][j+len] = dp[j][j+len-1] + dp[j+1][j+len] - dp[j+1][j+len-1] + is[j][j+len];}}int q;cin>>q;while(q--) {scanf("%d%d",&l,&r);printf("%d\n",dp[l][r]);}return 0 ;}

【CodeForces - 245H 】Queries for Number of Palindromes (带容斥的区间dp)相关推荐

  1. CF245H Queries for Number of Palindromes

    题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...

  2. CodeForces - 416A Guess a number

    A. Guess a number! time limit per test1 second memory limit per test256 megabytes A TV show called & ...

  3. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  5. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  6. CodeForces - 1486F Pairs of Paths(树上计数+容斥)

    题目链接:点击查看 题目大意:给出一棵 nnn 个点的树,再给出 mmm 条路径,现在问有多少个路径对 (x,y)(x,y)(x,y),满足第 xxx 条路径和第 yyy 条路径有且仅有一个交点 题目 ...

  7. Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers 高维前缀和 + 容斥

    传送门 文章目录 题意: 思路: 题意: 思路: 完全想不到容斥啊,看了半天也没看懂渍渍渍. 定义f[i]f[i]f[i]表示iii的超集个数,那么选择的方案就是2f[i]−12^{f[i]}-12f ...

  8. Codeforces Round #225 (Div. 1) E. Vowels 容斥 + sosdp

    传送门 文章目录 题意: 思路: 题意: 给你nnn个长度为333的串,串的每个字母都在a−za-za−z范围内,定义一个串合法当且仅当这个串中含有至少一个元音字母.现在他忘记了元音字母都有那几个,显 ...

  9. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

最新文章

  1. System Center产品相关开发(1)-SCOM和SCVMM简介
  2. 超适合新手的基础Linux命令
  3. 微信开发系列之一 - 微信公众号开发的开发环境搭建
  4. java对象内存模型_Java对象的内存模型
  5. struts文件上传 java_Struts上传文件
  6. 推特上马斯克BTC赠送骗局已获利10枚BTC,价值超55万美元
  7. Win10 IIS本地部署MVC网站时不能运行?
  8. mysql触发器检验有效性_mysql触发器实例 两个触发器保证数据有效性
  9. Structs2 总结
  10. JNI调用dll库或so库
  11. interlace video encode PAFF 和MBAFF
  12. 如何使用Visual Studio调试Windows Vista侧栏小工具
  13. 且听风吟,王者峡谷英雄汇,让我们探一探英雄背后的故事
  14. 2022hdu多校1C题题解
  15. 宝宝起名神器微信小程序源码下载支持多种流量主模式
  16. python3 tk_python3.5 安装python3-tk
  17. JVM中OOM和SOF的产生
  18. 你知道数据运营日常主要工作吗?
  19. REXROTH比例阀的运用和特点
  20. Hudson使用之执行Hudson

热门文章

  1. 影院平台搭建 - (6)一个靠谱的视频播放方案的感想
  2. python中parse是什么_Python中optparse模块使用浅析
  3. 计算机2级ps教学大纲,《PhotoShop》教学大纲
  4. linux 取出字符中数字,使用awk提取字符串中的数字或字母
  5. linux h5 动画软件下载,技术|7款绚丽的jQuery/HTML5动画及源码
  6. MongoClient类参考文档
  7. c语言math函数 sgn,常用矩阵计算C语言代码
  8. UE4 左右立体参数
  9. ubuntu ip设置
  10. mysql 碎片率_计算MySQL表碎片的SQL整理