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的更多相关文章

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

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

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

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

  4. Codeforces 245H Queries for Number of Palindromes

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

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

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

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

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

  7. Queries for Number of Palindromes(求任意子列的回文数)

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

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

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

  9. 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. Scala 深入浅出实战经典 第76讲:模式匹配下的赋值语句

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

  2. linux 下 取进程占用内存(MEM)最高的前10个进程

    # linux 下 取进程占用 cpu 最高的前10个进程ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head # linux 下 取进程占用内存 ...

  3. 原创教程:SpagoBI4.2汉化及配置Mysql数据库教程

    SpagoBI4.2汉化及配置Mysql数据库教程 商务智能套件SpagoBI提供一个基于J2EE的框架用于管理BI对象如报表.OLAP分析.仪表盘.记分卡以及数据挖掘模型等的开源BI产品.它提供的B ...

  4. 英語版Windows Server 2012 R2を日本語化する手順

    [スタート]ボタンを押し.[Control Panel]を起動 [Clock, Language and Region]の下の[Add a Language]をクリック [Add a Language ...

  5. IIS 日志文件分析

    先安装下文参考资料中的log parser studio 然后就可以针对日志文件进行sql语句的查询了. 各页面访问量排行 ) FROM '[LOGFILEPATH]' where cs-uri-st ...

  6. RPM 包下载 GCC 4.8安装

    http://ftp.scientificlinux.org/linux/scientific/ http://www.rpmfind.net/linux/rpm2html/search.php?qu ...

  7. git Please move or remove them before you can merge. 错误解决方案

    git pull 时 往往会遇到各种各样的问题 ,下面是常遇到的一种状况 Updating 7c9e086..936acacerror: The following untracked working ...

  8. PHP 常见语法 集合

    1.die()与exit()的真正区别 die 为 exit 的别名, 执行过程 将释放内存,停止代码执行 echo "begin exec <br/>"; show( ...

  9. 读书笔记_Effective_C++_条款四十七:请使用trait classes来表示类型信息

    这一条款主要来讨论模板中迭代器的属性iterator_category,它可以通过类似于vector<int>::iterator::iterator_category的方式来取得. 到这 ...

  10. oracle 11g 如何创建、修改、删除list-list组合分区

    Oracle11g在分区方面做了很大的提高,不但新增了4种复合分区类型,还增加了虚拟列分区.系统分区.INTERVAL分区等功能. 9i开始,Oracle就包括了2种复合分区,RANGE-HASH和R ...