Queries for Number of Palindromes(求任意子列的回文数)
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 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.
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».
代码:
#include<stdio.h>
#include<string.h>
int dp[][],and1[][];
int main()
{
char s[];
int q,l,r,sta,end,len;
int i;
gets(s);
len=strlen(s);
for(i=; i<len; i++)
{
dp[i][i]=and1[i][i]=;
and1[i+][i]=;
}
for(i=;i<=len;i++)
for(sta=;sta<len;sta++)
{
end=sta+i-;
if(and1[sta+][end-]&&s[sta]==s[end])
and1[sta][end]=;
dp[sta][end]=dp[sta+][end]+dp[sta][end-]-dp[sta+][end-]+and1[sta][end];
}
scanf("%d",&q);
while(q--)
{
scanf("%d %d",&l,&r);
printf("%d\n",dp[l-][r-]);
}
return ;
}
Queries for Number of Palindromes(求任意子列的回文数)的更多相关文章
- 求第N个回文数 模板
备忘. /*看到n可以取到2*10^9.说明普通方法一个个暴力计算肯定会超时的,那打表呢?打表我们要先写个打表的代码,这里不提供.打完表观察数据,我们会发现数据其实是有规律的.完全不需要暴力的把所有数 ...
- 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 ...
- 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 ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- hdu5340—Three Palindromes—(Manacher算法)——回文子串
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 4190. Prime Palindromes 一亿以内的质数回文数
Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...
随机推荐
- Java解惑四:异常之谜
谜题36 finally语句中的return语句会覆盖掉try语句中的. 谜题37 该部分还须要进一步理解 一个方法能够抛出的被检查异常集合是它所适用的全部类型声明要抛出的被检查集合的交集.
- C中多线程开发
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期.solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,可是在一个进程(proce ...
- Android学习笔记-tween动画之java实现
Android动画分为Tween动画和Frame动画,近期学习了,体tween动画,现在讲学习的心得以及相关知识介绍如下. Tween又称为补间动画,可以把对象进行缩小.放大.旋转和渐变等操作. ...
- Zend Studio配置Xdebug
按照网上的教程一直没有配置好,上官网看到一句话, If you don't know which one you need, please refer to the custom installati ...
- 2016/1/14 java生成指定范围的随机数
java生成指定范围的随机数 package edu.sjtu.erplab.io; import java.util.Random; public class RandomTest { public ...
- 洛谷 P1759 通天之潜水
P1759 通天之潜水 19通过 65提交 题目提供者lych 标签动态规划洛谷原创 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 直达通天路·小A历险记第三篇 题目 ...
- hibernate 下载
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/ http://sourceforge.net/p ...
- hihocoder1699
链接:http://hihocoder.com/problemset/problem/1699 快毕业了的菜菜,做了个比赛,遇到四维偏序,调成了傻逼,所以记录下,看了下以前的傻逼代码,发现自己的cdq ...
- Called attach on a child which is not detached
问题:Called attach on a child which is not detached: ViewHolder#出现问题的原因 经过google后发现,出现该问题的原因是由于recycle ...
- Handle/Body pattern(Wrapper pattern)
Handle Body Pattern 一些设计模式,通过一系列非直接的间接的方式(这种间接的方式,可称其为 handle(把手)),完成接口与实现(实现可称为 body(主体))的分离 Handle ...