当前位置: 首页 > news >正文

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 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».

题意:求一个区间内回文串的数量

思路:要利用一个判断l~r区间的回文数组来判断这个区间是否是回文串是则为1不是则为0 然后定义一个dp数组来记录l~r区间内回文串的数量

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int hw[5005][5005];
int dp[5005][5005];
char str[5005];
int main()
{
   scanf("%s",str+1);
   int len=strlen(str+1);
   for(int t=1;t<=len;t++)
   {
       hw[t][t]=1;
       hw[t][t-1]=1;
       dp[t][t]=1;
   }
   for(int d=1;d<=len;d++)
   {
       for(int l=1;l+d<=len;l++)
       {
           int r=l+d;
           if(str[l]==str[r]&&hw[l+1][r-1])
           {
               hw[l][r]=1;
        }
    }
   }
   for(int d=1;d<=len;d++)
   {
       for(int l=1;l+d<=len;l++)
       {
           int r=l+d;
           dp[l][r]=dp[l+1][r]+dp[l][r-1]-dp[l+1][r-1]+hw[l][r];
    }
   }
   int q;
   cin>>q;
   int l,r;
   while(q--)
   {
       scanf("%d%d",&l,&r);
       printf("%d\n",dp[l][r]);
   }
   return 0; 
}

 

转载于:https://www.cnblogs.com/Staceyacm/p/11240949.html

相关文章:

  • ASP.Net WebForm温故知新学习笔记:二、ViewState与UpdatePanel探秘
  • IDEA等全家桶设置Ctrl+滚轮调整字体大小
  • 怎样卸载外壳扩展的DLL?
  • 具有自动恢复功能的通知栏图标控件
  • Win7编程:在按钮中加入管理员权限运行
  • 反射概念
  • Office 2010 中的数字签名
  • OpenJ_Bailian - 2995-登山(两遍最长上升子序列+枚举顶点)
  • 今天还好
  • HDU - 1261-字串数 (排列组合+大数)
  • 【CSDN博客之星评选】我为什么坚持写博客
  • php io
  • 音视频学习之 - 基础概念
  • 注册“Oracle Provider for OLE DB”和创建链接服务器
  • SpringBoot Admin 使用指南
  • [笔记] php常见简单功能及函数
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 【347天】每日项目总结系列085(2018.01.18)
  • 07.Android之多媒体问题
  • 10个最佳ES6特性 ES7与ES8的特性
  • IP路由与转发
  • JavaScript类型识别
  • JS基础之数据类型、对象、原型、原型链、继承
  • Laravel 中的一个后期静态绑定
  • mac修复ab及siege安装
  • miaov-React 最佳入门
  • react-native 安卓真机环境搭建
  • Redis字符串类型内部编码剖析
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 扑朔迷离的属性和特性【彻底弄清】
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 详解移动APP与web APP的区别
  • 在Docker Swarm上部署Apache Storm:第1部分
  • UI设计初学者应该如何入门?
  • 函数计算新功能-----支持C#函数
  • ​卜东波研究员:高观点下的少儿计算思维
  • #NOIP 2014# day.2 T2 寻找道路
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (C#)获取字符编码的类
  • (done) 两个矩阵 “相似” 是什么意思?
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (一)Java算法:二分查找
  • (转)EOS中账户、钱包和密钥的关系
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • .axf 转化 .bin文件 的方法
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .net core 6 redis操作类
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET DataGridView数据绑定说明
  • .NET 常见的偏门问题
  • .Net 访问电子邮箱-LumiSoft.Net,好用