题目链接:http://codeforces.com/problemset/problem/245/H

题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串的个数。

容斥原理: dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];

if( str[i]==str[j] 并且 str[i+1..j-1]是回文串 ) dp[i][j]++;

代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <queue>
#include <stack>
#include <string>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ size()
#define ST begin()
#define ED end()
#define CLR clear()
#define ZERO(x) memset((x),0,sizeof(x))
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const double EPS = 1e-; const int MAX_N = ;
char str[MAX_N];
int dp[MAX_N][MAX_N];
int q; bool IsPalindromes(int i,int j){
if(i>=j) return true;
return dp[i][j] == dp[i+][j]+dp[i][j-]-dp[i+][j-]+;
} int main() {
scanf("%s",str+);
int length = strlen(str+);
// printf("length: %d\n",length);
for(int i=;i<=length;i++) dp[i][i] = ; for(int len = ; len<=length; len++) {
for(int i=;i<=length;i++) {
int j = i+len-;
if( j>length ) continue;
dp[i][j] = dp[i][j-]+dp[i+][j]-dp[i+][j-];
if( str[i]==str[j] ) {
if( IsPalindromes(i+,j-) ) {
dp[i][j] ++ ;
}
}
}
} // for(int i=1;i<=length;i++) {
// for(int j=1;j<=length;j++) {
// printf("%d ",dp[i][j]);
// }
// puts("");
// } scanf("%d",&q); while( q-- ) {
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
} return ;
}

[CF245H] Queries for Number of Palindromes (容斥原理dp计数)的更多相关文章

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

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

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

  3. CF245H Queries for Number of Palindromes(回文树)

    题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...

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

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

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

  6. CF245H Queries for Number of Palindromes

    题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...

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

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

  8. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  9. Queries for Number of Palindromes (区间DP)

    Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...

随机推荐

  1. Windows下gvim配置

    Windows下gvim配置原作地:http://hi.baidu.com/leemoncc/blog/item/a6be15cf40d7ab31b600c806.html 0.准备软件及插件. (a ...

  2. qpython3 读取安卓lastpass Cookies

    之前我的博客写了python读取windows chrome Cookies,沿着同样的思路,这次本来想尝试读取安卓chrome Cookies, 但是可能是chrome的sqlite3版本比较高读取 ...

  3. python3 reqeuests给OSC全站用户刷积分

    严格来讲是给在OSC上贡献过内容的OSC用户刷积分. OSC很多操作都需要消耗积分,很多人给OSC贡献了内容, 但是却没有人点赞,所以有些人在OSC混了很多年也没有积分. 本文中使用到的工具有gifc ...

  4. Python的平凡之路(8)

    (本文是对平凡之路(7)的补充等) 一.动态导入模块 import importlib __import__('import_lib.metaclass') #这是解释器自己内部用的 #importl ...

  5. 【RobotFramework自动化测试】RFS常用脚本

    读取后台数据文件:Import Variables | ${CURDIR}/\ABC.py 定位页面:Wait Until Keyword Succeeds | 5s | 500ms | select ...

  6. web.xml在Servlet3.0中的新增元素

    metadata-complete: 当属性为true时,该Web应用将不会加载注解配置的Web组件(如Servlet.Filter.Listener) 当属性为false时,将加载注解配置的Web组 ...

  7. Mybatis开篇以及配置教程

    MyBatis来源~: MyBatis本是apache的一个开源的项目,原来称为iBatis,2010年这个项目由apache softwarefoundation迁移到了google code,并改 ...

  8. inline-block

    在CSS中,块级对象元素会单独占一行显示,多个block元素会各自新起一行,并且可以设置width,height属性:而内联对象元素前后不会产生换行,一系列inline元素都在一行内显示,直到该行排满 ...

  9. 传智博客.NET培训第13季 Ajax教程(共十三季) 学习资源

    http://wangpengnimei.ctfile.com/u/1235801/47146 传智博客.NET培训第13季 Ajax教程(十三季).rar 19

  10. 传智播客JavaWeb day06-jstl

    一.jsp标签(sun公司提供的) 二.EL表达式 三.jstl (javaserver pages standard tag library) 1.为什么要有jstl jsp标签太弱,el表达式功能 ...