Codeforces 245H Queries for Number of Palindromes:区间dp
题目链接:http://codeforces.com/problemset/problem/245/H
题意:
给你一个字符串s。
然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的个数。
题解:
表示状态:
dp[x][y] = numbers
表示区间[x,y]中的回文子串个数。
找出答案:
每次询问:ans = dp[x][y]
如何转移:
dp[x][y] = dp[x][y-1] + dp[x+1][y] - dp[x+1][y-1] + pal[x][y]
用到了容斥原理。
pal[x][y]表示子串s[x to y]是否是一个回文串(是为1,不是为0)。
其中pal[x][y]又有递推式:
pal[x][y] = ( s[x]==s[y] && (x+1==y || pal[x+1][y-1]) )
记忆化搜索就好啦。
边界条件:
if(x>y) dp[x][y] = 0
if(x==y) dp[x][y] = 1
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 5005 using namespace std; int t;
int dp[MAX_N][MAX_N];
int pal[MAX_N][MAX_N];
char s[MAX_N]; int is_pal(int x,int y)
{
if(s[x]!=s[y]) return ;
if(x+==y) return ;
if(pal[x+][y-]!=-) return pal[x+][y-];
return pal[x+][y-]=is_pal(x+,y-);
} int dfs(int x,int y)
{
if(dp[x][y]!=-) return dp[x][y];
if(x>y) return ;
if(x==y) return dp[x][y]=pal[x][y]=;
dp[x][y]=dfs(x,y-)+dfs(x+,y)-dfs(x+,y-);
pal[x][y]=(s[x]==s[y] && (x+==y || pal[x+][y-]));
return dp[x][y]=dp[x][y]+pal[x][y];
} int main()
{
scanf("%s%d",s+,&t);
memset(dp,-,sizeof(dp));
int x,y;
while(t--)
{
scanf("%d%d",&x,&y);
printf("%d\n",dfs(x,y));
}
}
Codeforces 245H 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] ...
- 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 ...
- Codeforces 245H Queries for Number of Palindromes
http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...
- 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 ...
- codeforces H. Queries for Number of Palindromes(区间dp)
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...
- [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 ...
随机推荐
- redhat 网络配置
1. 查看网络 ifconfig 网卡名字(eth0.wlan0) ifconfig -a //查看所有网卡配置 2. 网卡打开\关闭 ifconfig eth0 down ifconfig eth0 ...
- 【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip
直接上代码: __author__ = 'zc' import unittest class demoSkipTest(unittest.TestCase): a = 70 b = 50 print( ...
- Android中的常见通信机制和Linux中的通信机制
Handler Handler是Android系统中的一种消息传递机制,起作用是应对多线程场景.将A进程的消息传递给B线程,实现异步消息处理.很多情况是将工作线程中需要更新UI的操作消息传递给UI主线 ...
- lamda表达式在EF中的应用
1.条件查询 _dbContext.TBEntity.Where(p=>p.ID=ID) 2.排序 升序 _dbContext.TBEntity.Where(p=>p.ID=ID).Or ...
- vue实践---vue配合express实现请求数据mock
mock数据是前端比较常见的技术,这里介绍下vue配合express 实现请求数据mock. 第一步: 安装 express : npm install express -D 第二步: 简历需要mo ...
- 由浅到深理解ROS(2)
ROS文件系统 用户可以直接参看官网:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念:Packa ...
- 【原创】Hibernate自动生成(1)
本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...
- Protobuf 语法 - 史上最简教程
Protobuf 语法简明教程 疯狂创客圈 死磕Netty 亿级流量架构系列之12 [博客园 总入口 ] 在protobuf中,协议是由一系列的消息组成的.因此最重要的就是定义通信时使用到的消息格式. ...
- HDU 4513 吉哥系列故事――完美队形II(Manacher)
题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...
- SAP号码段
[转http://blog.csdn.net/wengyupeng/article/details/8513527] 1.通用Tcode:SNRO 常用维护特定Number range Tcode: ...