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][r-1]-dp[l+1][r-1]+flag[l][r], 如果是回文串flag[l][r]为1。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
string s;
int dp[][], flag[][];
int judge(int l, int r) {
if(~flag[l][r])
return flag[l][r];
if(l == r)
return flag[l][r] = ;
if(l == r- &&s[l] == s[r])
return flag[l][r] = ;
int tmpl = l, tmpr = r;
while(l<r) {
if(s[l]!=s[r])
return flag[tmpl][tmpr] = ;
return flag[tmpl][tmpr] = judge(l+, r-);
}
}
int dfs(int l, int r) {
if(~dp[l][r])
return dp[l][r];
if(l>r)
return dp[l][r] = ;
if(l == r)
return dp[l][r] = ;
dp[l][r] = dfs(l+, r)+dfs(l, r-)-dfs(l+, r-)+judge(l, r);
return dp[l][r];
}
int main()
{
mem1(dp);
mem1(flag);
cin>>s;
int n, a, b;
cin>>n;
dfs(, s.size()-);
for(int i = ; i<n; i++) {
scanf("%d%d", &a, &b);
printf("%d\n", dp[a-][b-]);
}
return ;
}
codeforces 245H . Queries for Number of Palindromes 区间dp的更多相关文章
- 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:区间dp
题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...
- 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 ...
随机推荐
- MOSS2010部署解决方案失败是可以尝试更新
update-spsolution -identity XXX.wsp -literalpath D:\SPS2010Setup\XXX.wsp -gacdeployment
- SQL Server 备份维护计划
1. 创建维护计划:SSMS -> 管理 -> 维护计划 -> 新建维护计划 2. 添加子计划(备份计划) a) 每30分钟:事务日志备份 每天:差异备份 每周:完整备份 b) ...
- Android开发问题汇总(持续更新)
在Android开发中,总会有一些很小的问题.由于我们的不仔细,很容易忽略掉,从而导致在该问题上花费了很多的时间,造成工作进度的延迟. 为此,在这里做一下记录,避免再次浪费许多时间在这些问题上. 1. ...
- Android studio mac版本快捷键
Mac下快捷键的符号所对应的按键 ⌥—> option|alt ⇧—>shift ⌃—>control ⌘—>command ⎋—>esc 注: 与F6/F7/F12等F ...
- HTMLのフォームの送信ボタンは、inputとbuttonでは機能的な違いがありますか?
(X)HTMLのフォームの送信ボタンは.inputとbuttonでは機能的な違いがありますか? <input type="submit" value="送信&quo ...
- 用CSS3绘制图形
参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...
- Yaf-Plus 我们只谈框架这件事
预热下,也算对自己的督促. Comming soon!
- OSG显示文字——自定义显示文字函数
#include <Windows.h> #include <osg/Geode> #include <osg/Geometry> #include <osg ...
- 深入GDI图像显示
摘 要:本文首先给出了一种结合了DIB和DDB两种位图优点的图像显示方法,其次对GDI函数的高级应用,如透明位图显示.图像旋转显示.图像镜像显示进行了研究. 关键词:GDI图像显示,特殊GDI函数的 ...
- 解决:未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0
开发环境:Microsoft Visual Studio 2013 MVC4.0 使用SignalR的过程中随着版本的升级,其它程序集也跟着升级,如Newtonsoft.Json,一般编译的时候,如果 ...