[CF245H] Queries for Number of Palindromes (容斥原理dp计数)
题目链接: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计数)的更多相关文章
- cf245H Queries for Number of Palindromes (manacher+dp)
首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...
- 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 ...
- CF245H Queries for Number of Palindromes(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
- Codeforces245H - Queries for Number of Palindromes(区间DP)
题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...
- 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] ...
- CF245H Queries for Number of Palindromes
题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
随机推荐
- php的特性
PHP的特性包括: 1. PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语法. 2. PHP可以比CGI或者Perl更快速的执行动态网页——动态页面方面,与其他的编程语言相比 ...
- ubuntu13.04 nfs配置
开发板和主机之间共享文件,常常需要配置nfs服务: 服务器端安装:root#:apt-get install nfs-common root#:apt-get install nfs-kernel-s ...
- HTML5 的data-* 自定义属性
HTML5增加了一项新功能是自定义数据属性,也就是 data-*自定义属性. 在HTML5中我们可以使用以data-为前缀来设置我们需要的自定义属性,来进行一些数据的存放. 当然高级浏览器下可通过脚本 ...
- Ubuntu中安装eclipse ,双击eclipse出现invalid configuration location问题
ubuntu invalid configuration location 标签: myeclipse for ubuntu ubuntu myeclipse ubuntu安装myecli ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- ubuntu14.04LTS编译MUDOS v22.2b14
附:MudOS v22.2b14下载连接 简单的编译步骤: (1)运行./build.MudOS (2)vim socket_efuns.c o 修改Ln:1198的代码为 ...
- 【Avalon】factory
(function(global, factory) { if (typeof module === "object" && typeof module.expor ...
- SVN提交碰到的问题奇怪的问题
svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了. 错误如下: 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.d ...
- Java和C/C++进行DES/AES密文传输(借鉴)
Java和C/C++进行DES/AES密文传输 声明:对于新手来说很难解决的一个问题,终于在非常煎熬之后找到这篇文章,所以借鉴过来.原文地址http://blog.sina.com.cn/s/blog ...
- Java-->Json解析网页数据
--> 官方解析jar包: 链接:http://pan.baidu.com/s/1pKDnXKv 密码:694d --> 离线Json格式检测工具: 链接:http://pan.baidu ...