题目链接

给一个字符串, 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的更多相关文章

  1. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

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

  3. Codeforces 245H Queries for Number of Palindromes:区间dp

    题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...

  4. Codeforces 245H Queries for Number of Palindromes

    http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...

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

  6. Codeforces245H - Queries for Number of Palindromes(区间DP)

    题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...

  7. codeforces H. Queries for Number of Palindromes(区间dp)

    题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...

  8. [CF245H] Queries for Number of Palindromes (容斥原理dp计数)

    题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...

  9. cf245H Queries for Number of Palindromes (manacher+dp)

    首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...

随机推荐

  1. javascript高级知识点——实例类型

    代码信息来自于http://ejohn.org/apps/learn/. 分析一下对象的结构 function Ninja(){} var ninja = new Ninja(); console.l ...

  2. mac nodejs&npm 安装

    https://www.baidu.com/link?url=Ekv7EzWuMOXjIqFL_ewddWzdahU7jMAsWY4gOGOjMtC&ie=UTF-8&wd=nodej ...

  3. 多线程11_张孝祥 java5的线程锁技术

    本例子因为两个线程公用同线程中,使用同一个对象,实现了他们公用一把锁,实现了同一个方法的互斥. package locks; /** *会被打乱的效果 */ public class LockTest ...

  4. Android开发问题汇总(持续更新)

    在Android开发中,总会有一些很小的问题.由于我们的不仔细,很容易忽略掉,从而导致在该问题上花费了很多的时间,造成工作进度的延迟. 为此,在这里做一下记录,避免再次浪费许多时间在这些问题上. 1. ...

  5. HTTP填坑

    HTTP知识填坑 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...

  6. Web 前端知识点

  7. nodejs 下载网页及相关资源文件

    功能其实很见简单,通过 phantomjs.exe 采集 url 加载的资源,通过子进程的方式,启动nodejs 加载所有的资源,对于css的资源,匹配css内容,下载里面的url资源 当然功能还是很 ...

  8. javascript 多行字符串

    javascript 字符串多行 平时一般使用 字符串+,或者[].join('')的方式 同事推荐了这样的形式 ExceptionDivHtml="<div class='gameI ...

  9. linux----定义命令别名

    1.定义命令别名的语法: alias nickName='command'#用于定义. unalias  nickName#用于撤消一个别名的定义. 如:alias cls='clear' 2.应该要 ...

  10. CDC不同模式在ODI体现系列之二 异步模式

    CDC不同模式在ODI体现系列之二 异步模式 2 异步模式需要在数据库中做一些准备工作: 改数据为归档并启用logminer: SQL> shutdown immediate 数据库已经关闭. ...