dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes
Problem's Link: http://codeforces.com/problemset/problem/245/H
Mean:
给你一个字符串,然后q个询问:从i到j这段字符串中存在多少个回文串。
analyse:
dp[i][j]表示i~j这段的回文串数。
首先判断i~j是否为回文,是则dp[i][j]=1,否则dp[i][j]=0;
那么dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i+1[j]-dp[i+1][j-1],从后往前推;
注意判断dp[i][j]是否是回文也需要从后往前推,否则超时。
Time complexity: O(n*n)
Source code:
// Memory Time
// 1347K 0MS
// by : crazyacking
// 2015-03-31-16.17
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 5005
#define LL long long
using namespace std;
char str[MAXN];
int dp[MAXN][MAXN]; bool judge(int sta,int en)
{
for(int i=sta,j=en;i<j;++i,--j)
{
if(str[i]!=str[j])
return false;
}
return true;
}
int main()
{
gets(str);
int len=strlen(str);
memset(dp,,sizeof dp);
for(int i=;i<len;++i)
{
if(str[i]==str[i+])
dp[i][i+]=;
dp[i][i]=;
}
for(int i=len-;i>=;--i)
{
for(int j=i;j<len;++j)
{
if(dp[i+][j-]==&&str[i]==str[j])
dp[i][j]=;
}
}
for(int i=len-;i>=;--i)
{
for(int j=i;j<len;++j)
{
dp[i][j]=dp[i][j]+dp[i][j-]+dp[i+][j]-dp[i+][j-];
}
}
int q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",dp[x-][y-]);
}
return ;
}
dp --- Codeforces 245H :Queries for Number of Palindromes的更多相关文章
- 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 区间dp
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...
- Codeforces 245H Queries for Number of Palindromes
http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...
- codeforces H. Queries for Number of Palindromes(区间dp)
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 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 ...
随机推荐
- 1、Orchard商城开发——开发需求
需要开发的功能: 1.商品详情,可添加商品属性,如颜色,尺寸等. 2.商品类别,可显示该类别下的所有商品,可按品牌.颜色.尺寸等检索,并可按价格.销量等排序游览. 3.商品游览记录,收藏商品,加入购物 ...
- Goldengate进程的拆分与合并
Goldengate的拆分与合并分类: ORACLE GoldenGate 2013-10-10 15:22 721人阅读 评论(0) 收藏 举报在使用Goldengate作为复制解决方案时,随着负载 ...
- 提示Can't load package:dclite70.bpl解决方法
64位系统安装Delphi7提示Can’t load package:dclite70.bpl 2015年04月05日 ⁄ 杂谈 ⁄ 共 392字 ⁄ 字号 小 中 大 ⁄ 暂无评论 ⁄ 阅读 1,7 ...
- 301 redirect Domain Name using global.asax
void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.ToSt ...
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- ORA-12571: TNS:packet writer failure
ORA-12571:TNS:包复写器失效 2007-05-28 22:04 ORA-12571:TNS:包复写器失效 近日一直在做Oracle数据库的搭建(入门),在搭建过程中遇到“ORA-12571 ...
- 谈一下OOP的乱用现象
很久很久以前写了两篇设计模式乱用的文章,最近心血来潮,突然想写篇OOP乱用. 最近在移植一个旧项目,接手过程很多嘈想吐,开一篇谈一下OOP的乱用. 大多数公司用MVC是为了解耦合,但是这套代码的MVC ...
- SAP的运输功能(转)
SAP的运输功能(transportation) transportation大体有三个作用: 1.运输计划transportation planning 概念:对outbound delivery ...
- tengine + mysql + nginx + php
tengine + mysql + nginx + php 1.配置防火墙vim /etc/sysconfig/iptables # 允许80端口通过防火墙-A INPUT -m state --st ...
- python 字符串相加
我们通过操作符号+来进行字符串的相加,不过建议还是用其他的方式来进行字符串的拼接,这样效率高点. 原因:在循环连接字符串的时候,他每次连接一次,就要重新开辟空间,然后把字符串连接起来,再放入新的空间, ...