Queries for Number of Palindromes (区间DP)
5 seconds
256 megabytes
standard input
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.
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.
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.
caaaba
5
1 1
1 4
2 3
4 6
4 5
1
7
3
4
2
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],需要拿来计算。
#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)的更多相关文章
- 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 ...
- 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\)的范围很小,但是 ...
随机推荐
- 图论:最短路-Dijkstra
Dijkstra+堆优化具有稳定的时间复杂度,在一些数据范围要求比较严格(准确来说是图比较苛刻)的时候能够保证稳定的时间复杂度 但是Dijkstra不能够解决负边权的问题,所以在使用的时候一定要仔细读 ...
- 省队集训Day1 睡觉困难综合征
传送门:https://www.luogu.org/problem/show?pid=3613 [题解] 按二进制位分开,对于每一位,用“起床困难综合征”的方法贪心做. 写棵LCT,维护正反两种权值, ...
- BigDecimal的用法详解
BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以 10 的负scale 次幂. f ...
- 将已编写的静态的网页发布到github上
最近在学习前端框架的过程中,一直想把自己学习中做的demo 发布到github 上去.但是在查看了很多相关资料也没能找到一个比较满意的结果. 无奈之下,只能尝试做用了一种自认为最low 的方式来达到部 ...
- shell 文件内容替换 sed用法
sed 's/要被替换的字符串/新的字符串/g' sed 's/test/mytest/g' example-----在整行范围内把test替换为mytest. 如果没有g标记,则只有每行第一个匹配的 ...
- JS阶段测试
JS阶段测试 一.选择题 1.表单中的数据要提交到的处理文件由表单的( c )属性指定. A. method B. name C. action D. 以上都不对 2.在CSS样式 ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- 阿里云ECS的使用
一.阿里云ECS的使用 1.Linux CentOS Ubuntu Readhat 2.远程登录 xshell 远程登录 winScp 远程文件操作 3.Linux命令 cd 目录名 ls . ls ...
- JS判断客户端是否是iOS或者Android手机移动端
var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...
- 算法入门系列1:k-means
k-means是一种无监督学习算法,用于聚类. 下图(来自http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006910.html)展示了k-m ...