Queries for Number of Palindromes(区间dp)
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
caaaba
5
1 1
1 4
2 3
4 6
4 5
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)的更多相关文章
- 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] ...
- 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 ...
- Codeforces245H - Queries for Number of Palindromes(区间DP)
题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...
- [CF245H] Queries for Number of Palindromes (容斥原理dp计数)
题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...
- cf245H Queries for Number of Palindromes (manacher+dp)
首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- 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 ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
随机推荐
- 系统UISearchController详解
原文链接:https://www.jianshu.com/p/aa9a153a5b58
- python6.1创建类
class Dog(object): type1="宠物"#类变量 #初始化方法 def __init__(self,name,age,color): self.name=name ...
- cxuan读者的外包面试之旅
最开始是打算面试外包公司刷经验,等经验差不多了,再去甲方尝试,可惜不太顺利. 一.迈思(面试了30分钟) 自我介绍 左连接(a 表左连接 b 表,a 表全部数据出来,b 表没有的数据为空) a 表左连 ...
- Java不可不知的泛型使用
前面的文章: 详解Java的对象创建 一文打尽Java继承的相关问题 一文打尽Java抽象类和接口的相关问题 本文介绍了Java的泛型的基本使用. 1. 为什么使用泛型 看下面一个例子: 为了说明问题 ...
- MySQL数据库——查询数据
增加数据: insert into "表名" values( '字段'',字段'); 或insert into '表名'( '字段'',字段') values( '字段'',字段 ...
- Butterfly侧边栏引入一言
此教程涉及修改源码 背景 在修改每页显示7篇文章后,出现了这种情况. 这是完美主义(强迫症)的我所不能忍受的,有什么可以占据这里的呢?{% btn 'https://hitokoto.cn/',一言, ...
- 个性探测综述阅读笔记——Recent trends in deep learning based personality detection
目录 abstract 1. introduction 1.1 个性衡量方法 1.2 应用前景 1.3 伦理道德 2. Related works 3. Baseline methods 3.1 文本 ...
- 用心整理的 献丑啦 一些关于http url qs fs ...模块的方法
http: const http = require("http"); http.createServer((req , res)=>{ req:request 请求 ...
- Spring MVC中处理中文问题
之前练习过提交表单数据,但是提交的数据包含中文,这一篇练习如何处理中文.在Spring MVC中处理中文问题和Filter处理中文问题是一样的手段 @ 目录 Filter addProduct.jsp ...
- Ubuntu图形桌面切换到命令行界面
Ubuntu提供两种进入方式,一个是我们平常最熟悉的图形界面形式,还有一种是纯命令行方式. 1.按 Ctrl + Alt + (F1~F6中的任意一个)即可进入纯命令行模式. 进入后,需要输入用户名, ...