http://codeforces.com/contest/245/problem/H

题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000)

思路:设定pd[i][j]代表i~j这部分是不是一个回文串,这个可以n^2预处理

然后设定f[i][j]代表i~j区间有多少个回文串,由于满足区间加减,因此有

f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+pd[i][j]

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int pd[][],f[][],n;
char s[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void init(){
for (int i=;i<=n;i++)
pd[i][i]=;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
if (i==j-&&s[i]==s[j]) pd[i][j]=;
else
if (s[i]==s[j]) pd[i][j]|=pd[i+][j-];
}
for (int i=;i<=n;i++)
f[i][i]=;
for (int i=;i<n;i++)
if (s[i]==s[i+])
f[i][i+]=;
else
f[i][i+]=;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
f[i][j]=f[i+][j]+f[i][j-]-f[i+][j-]+pd[i][j];
}
}
int main(){
scanf("%s",s+);
n=strlen(s+);
init();
int T=read();
while (T--){
int l=read(),r=read();
printf("%d\n",f[l][r]);
}
return ;
}

Codeforces 245H Queries for Number of Palindromes的更多相关文章

  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 区间dp

    题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. LeetCode_Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  2. 俄罗斯人开发的等待控件TfgActivityDialog

    http://blog.csdn.net/star1010/article/details/28674173

  3. 查询Sqlserver 表结构信息 SQL

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = ...

  4. Android Map新用法:MapFragment应用

    MapView ,MapActivity 这种的局限在于,必须要继承MapActivity,否则无法使用MapView,但是,MapFragment 这种的局限在于,必须要安装Google Play ...

  5. MySQL所有函数及操作符

    参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...

  6. cf493B Vasya and Wrestling

    B. Vasya and Wrestling time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. phpcms:八、show.html

    标题:{$title}来源:{$copyfrom}评论:<a href="#comment_iframe" id="comment">0</a ...

  8. warning: the `gets' function is dangerous and should not be used.(转)

    今天在LINUX下编译C程序时,出现了:warning: the `gets' function is dangerous and should not be used. 这个warning. 百度之 ...

  9. Entify Framewrok - Join的使用方法

    问题:有2个表,使用id相连,如何用Join语法将其连接起来? 如下代码 List<tblAssociation> assoList = dataContext.tblAssociatio ...

  10. Javascript 文件操作(整理版)

    Javascript 文件操作 一.功能实现核心:FileSystemObject 对象 其实,要在Javascript中实现文件操作功能,主要就是依靠FileSystemobject对象.在详细介绍 ...