CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
Times:5000ms;
Memory limit:262144 kB
给定字符串S(|S|<=5000),下标由1开始。然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间[L,R]里有多少个回文串。
请想出两种或者以上的方法。
------------------------分界线--------------------------
方法1:区间DP。
容斥一下,dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+ok[i][j],(ok表示是否是个回文串)。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int dp[maxn][maxn],ok[maxn][maxn],N,Q;
char c[maxn];
void DP()
{
for(int i=N;i>=;i--)
for(int j=i;j<=N;j++){
if(j-i==) ok[i][j]=,dp[i][j]=;
else if(j-i==) dp[i][j]=ok[i][j]=dp[i][i]+dp[j][j]+(c[i]==c[j]?:);
else {
ok[i][j]=ok[i+][j-]&(c[i]==c[j]);
dp[i][j]=dp[i+][j]+dp[i][j-]-dp[i+][j-]+ok[i][j];
}
}
}
int main()
{
scanf("%s",c+);
N=strlen(c+);
DP();
scanf("%d",&Q);
while(Q--){
int x,y; scanf("%d%d",&x,&y);
printf("%d\n",dp[x][y]);
}
return ;
}
方法2:
马拉车:每个为起点,然后预处理得到每个为起点的回文串个数前缀和。 为了训练,就先不给带代码了。
方法3:回文树,这个我还不会。
方法4:暴力,开始我以为暴力得到回文串,然后差分得到前缀和,但是前缀和涉及到了交叉问题可能会导致出错。所以,目前还不行。(不过别人说可以,那再想想)
CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)的更多相关文章
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- 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
http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...
- Codeforces 245H Queries for Number of Palindromes:区间dp
题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...
- Queries for Number of Palindromes(求任意子列的回文数)
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
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...
- codeforces H. Queries for Number of Palindromes(区间dp)
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...
- 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(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
随机推荐
- mac 获得进程信息的方法
NSProcessInfo可以获得当前进程的信息.获得所有活动进程信息可以尝试使用下面的方法. 进程的信息可以通过ps命令得到也可以通过sysctl方法得到. 但是我总是不能获取进程的流量信息,关于这 ...
- (10) android控件-date
1.TimePicker <TimePicker android:id="@+id/timePicker4" android:layout_width="wrap_ ...
- centos6安装概述
1.1.选择安装类型:[Install or upgrade an existing system]安装或升级现有系统 1.2.介质校验:[Skip]跳过介质校验,校验时间较长 1.3.语言选择:[E ...
- C语言qsort
C/C++中有一个快速排序的标准库函数 qsort ,在stdlib.h 中声明,其原型为: void qsort(void *base, int nelem, unsigned int width, ...
- Win7 VNC远程连接Centos桌面
一,安装Linux桌面: yum -y groupinstall Desktop yum -y groupinstall "X Window System" yum -y grou ...
- 一致性hash算法在memcached中的使用
一.概述 1.我们的memcacheclient(这里我看的spymemcache的源代码).使用了一致性hash算法ketama进行数据存储节点的选择.与常规的hash算法思路不同.仅仅是对我们要存 ...
- DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1
DevExpress2011控件教程)编辑控件(comboBox,AspxCheckBox) 范例1 AspxCheckBox 是一个检查编辑控件去展示特殊条件是否关闭或者打开.它一般会展示Yes/N ...
- opencms 安装出现以下的问题:Your 'max_allowed_packet' variable is set to less than 16777216 Byte (16MB).
一.问题 在安装opencms是会出现例如以下错误: MySQL system variable 'max_allowed_packet' is set to 1048576 Byte (1MB). ...
- net spy memcached 使用demo
package memcached; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.mem ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...