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 ...
随机推荐
- muduo源代码分析--Reactor模式在muduo中的使用
一. Reactor模式简单介绍 Reactor释义"反应堆",是一种事件驱动机制.和普通函数调用的不同之处在于:应用程序不是主动的调用某个API完毕处理.而是恰恰相反.React ...
- iOS NSMutableDictionary中UIImage的存储和读取
思路:将UIImage转换成NSData,然后插入到NSMutableDictionary中.读取时,用NSData读出来,然后再转换成UIImage -存储 UIImage *image = [se ...
- 第二天,初步slide第一版和家的照片墙
今天基本完成任务, 1. 写了昨天的总结, 2. 完成slides的第一个完整版. 3. 家里布置了照片墙. 4. 其他的 未完成: 1. 框架搭建:Creasy没来. 领导力:为公司利益早起,任务说 ...
- C++ - 使用copy函数打印容器(container)元素
使用copy函数打印容器(container)元素 本文地址: http://blog.csdn.net/caroline_wendy C++能够使用copy函数输出容器(container)中的元素 ...
- storm与hadoop的对照
hadoop 是实现了 mapreduce 的思想,将数据切片计算来处理大量的离线数据. hadoop处理的数据必须是已经存放在 hdfs 上或者类似 hbase 的数据库中.所以 hadoop ...
- OSX:不同OSX版本号的标记可能不兼容
现象: 依据測试,中文OS X 10.9和中文10.10的文件标记彼此不兼容. 也就是说.比方在10.9中的颜色标记,在10.10DP2中不能删除,但能够加入/删除10.10自己的颜色标记,反之亦然. ...
- 通过shell脚本批处理es数据
#!/bin/sh [按照指定的域名-website集合,遍历各个域名,处理url] #指定待删除的变量集合 arr=(6.0) cur="`date +%Y%m%d%H%M%S`" ...
- 容器ConcurrentHashMap原理(学习)
一.概述 HashMap 是非线程安全的,在不考虑性能问题的时候,我们的解决方案有 Hashtable 或者Collections.synchronizedMap(hashMap),这两种方式基本都是 ...
- CPU上电时序详细分析
首先是RTC电源,这部分电力是永远不关闭的,除非电池(纽扣电池)没电并且没接任何外部电源(比如电池和电源适配器). RTC用以保持机器内部时钟的运转和保证CMOS配置信息在断电的情况下不丢失:其次,在 ...
- c# IP从192.168.1.1转成int类型
找了一些资料,总结如下: 方法1 .net提供的方法转换IP地址 //字符串转换为数字 System.Net.IPAddress ipaddress = System.Net.IPAddress.Pa ...