【CodeForces - 245H 】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

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441054.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php7 获取文件类型,太简单了!PHP获取文件扩展名的7中方法

PHP中获取文件扩展名的方法第一种&#xff1a;$file x.y.z.png;echo substr(strrchr($file, .), 1);解析&#xff1a;strrchr($file, .)strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置&#xff0c;并返回从该位置到字符串结尾的所有字符第二种&#xff1a;$file…

【EOJ Monthly 2018.12 - A,B,C】套题训练,部分题解

A. 题干&#xff1a; A. 仰望星空 单测试点时限: 2.0 秒 内存限制: 512 MB 你就这样静坐在草地上&#xff0c;离我稍远的地方。 我用眼角瞅着你&#xff0c;你什么话也别说。 语言是误会的根源。 但是&#xff0c;每天&#xff0c;你可以坐得离我近一些…… 你和她一起仰头…

php android 复制粘贴板,Android_Android剪贴板用法详解,本文实例详述了Android剪贴板的 - phpStudy...

Android剪贴板用法详解本文实例详述了Android剪贴板的用法&#xff0c;分享给大家供大家参考。具体方法分析如下&#xff1a;这里首先需要注意的一点&#xff0c;就是在使用Android剪贴板的时候大家只记住一点就行了&#xff0c;不管是安卓设备还是PC机&#xff0c;复制粘贴在同…

【Uva - 10047 】The Monocycle(搜索,bfs记录状态)

题干&#xff1a; Uva的题目就不粘贴题干了&#xff0c;&#xff0c;直接上题意吧。 有你一个独轮车&#xff0c;车轮有5种颜色&#xff0c;为了5中颜色的相对位置是确定的。有两种操作&#xff1a;1.滚动&#xff1a;轮子必须沿着顺时针方向滚动&#xff0c;每滚动一次会到达…

matlab中bwlabel意思,Matlab 里bwlabel 函数的具体含义

Matlab函数bwlabel&#xff1a;在二值图像中标记连通区域用法&#xff1a;L bwlabel(BW,n)返回一个和BW大小相同的L矩阵&#xff0c;包含了标记了BW中每个连通区域的类别标签&#xff0c;这些标签的值为1、2、num(连通区域的个数)。n的值为4或8&#xff0c;表示是按4连通寻找区…

php百度搜索框代码,基于jquery的仿百度搜索框效果代码_jquery

先看看整个的效果图&#xff1a;图一&#xff1a;图二&#xff1a;图三&#xff1a;图四&#xff1a;大概的效果图就这样&#xff0c;接下来直接看源码页面&#xff1a;CSS&#xff1a;.autoSearchText{border:solid 1px #CFCFCF;height:20px;color:Gray;}.menu_v{margin:0;pad…

【UVA - 227】Puzzle (模拟,水题)

题干&#xff1a; Puzzle A childrens puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the…

php 解析mib文件,Mib库解析

MibAnalyser介绍MibAnalyser可以解析MIB文件&#xff0c;并转化为对应的实体&#xff0c;持久化到本地。MibAnalyser分为三个模块&#xff1a;解析模块、持久化模块、工具库模块。解析模块解析模块用于解析MIB文件的语法&#xff0c;并最终生成实体列表。管理模块由于对MIB文件…

【CodeForces - 299C 】Weird Game (思维,模拟,贪心,博弈,OAE思想)

题干&#xff1a; Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesnt show up, Yaroslav and Andrey play another game. Roman leaves a word for each of them. Each word consists of 2n binary characte…

matlab大作业题题单,2011MATLAB大作业-题目-

(1)求解线性规划问题&#xff1a;minZ 4x1 x2 7x3s.t.x1 x2 x3 53x1 x2 x3 4x1 x2 4x3 7x1,x2 0问各xi分别取何值时&#xff0c;Z有何极小值。(2)编写一个函数&#xff0c;使其能够产生如下的分段函数&#xff1a;0.5x&#xff0c;x 2f(x) 1.5 0.25x&#xff0c;2 x 6&#xff…

【CodeForces - 298D】Fish Weight (OAE思想,思维)

题干&#xff1a; It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 ≤ …

php 字符串比较的规则,PHP字符串比较函数strcmp()与strcasecmp()的用法介绍

使用“”来判断。它和“”的区别&#xff0c;前者强调“identical(相同的&#xff0c;完全相同)”类型也要求一样&#xff1b;后者要求“equal(相等)”&#xff0c;值相同就可以了。或者使用strcmp来判断&#xff0c;但是这不能说明两个字符串是否相等。一般能用 !, 比较两个对…

【CodeForces - 140C】New Year Snowmen (贪心)

题干&#xff1a; As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergeys twins help him: theyve already made n sno…

php正文重复度,百度如何判断网页文章的重复度?两个页面相似度确认方法介绍...

在这个科技高度发达的时代&#xff0c;百度已经成为人们能获取消息的主要途径。但如今的百度&#xff0c;到处充斥着一些重复的内容&#xff0c;对用户的访问造成很大的困扰。因此&#xff0c;百度需要对网页重复进行判断&#xff0c;对重复的网页&#xff0c;只选取一些高质量…

【CodeForces - 892C 】Pride (数学,思维构造,gcd)

题干&#xff1a; You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor. What is…

php webshell编写,php webshell学习

一、环境kali 192.168.43.177开户apache /etc/init.d/apache2 start/var/www/html/目录下编辑php代码hackbarhttps://github.com/Mr-xn/hackbar2.1.3二、php基础输出函数:echo - 可以输出一个或多个字符串print - 只允许输出一个字符串&#xff0c;返回值总为 1提示&#xff1a…

【CodeForces - 27E】Number With The Given Amount Of Divisors (数论,数学,反素数)

题干&#xff1a; Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018. Input The first line of the input contains integer n (1 ≤ n ≤ 1000). Outp…

js php c语言for循环,小蚂蚁学习C语言(8)——C语言for循环

最近听到流程控制和循环了&#xff0c;感觉语言之间的语法很相似&#xff0c;不听吧&#xff0c;怕耽误某一个不同点或知识点&#xff0c;听吧&#xff0c;消耗很多时间&#xff0c;着实很纠结&#xff0c;莫非这需要传说中的空杯心态&#xff1f;循环 定义和分类定义&#…

【HDU - 1559】最大子矩阵 (二维前缀和裸题)

题干&#xff1a; 给你一个mn的整数矩阵&#xff0c;在上面找一个xy的子矩阵&#xff0c;使子矩阵中所有元素的和最大。 Input 输入数据的第一行为一个正整数T&#xff0c;表示有T组测试数据。每一组测试数据的第一行为四个正整数m,n,x,y&#xff08;0<m,n<1000 AND 0…

php _invoke 闭包,PHP新特性之闭包、匿名函数

闭包闭包是什么&#xff1f;1).闭包和匿名函数在PHP5.3中被引入。2).闭包是指在创建时封装函数周围状态的函数&#xff0c;即使闭包所在的环境不存在了&#xff0c;闭包封装的状态依然存在&#xff0c;这一点和Javascript的闭包特性很相似。3).匿名函数就是没有名称的函数&…