Codeforces245H - Queries for Number of Palindromes(区间DP)
题目大意
给定一个字符串s,q个查询,每次查询返回s[l…r]含有的回文子串个数(题目地址)
题解
和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数
用dp[i][j]表示,s[i..j]含有的回文子串个数,则dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+flag[i][j](如果s[i..j]是回文子串则flag[i][j]=1,否则为0)
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 5005
int dp[MAXN][MAXN];
int flag[MAXN][MAXN],n;
char s[MAXN];
void solve(int l,int r)
{
while(l>=1&&r<=n)
{
if(s[l]==s[r])
{
flag[l][r]=1;
l--,r++;
}
else
break;
}
}
int main()
{
while(scanf("%s",s+1)!=EOF)
{
n=strlen(s+1);
memset(dp,0,sizeof(dp));
memset(flag,0,sizeof(flag));
for(int i=1; i<=n; i++)
{
solve(i,i);
solve(i,i+1);
}
for(int i=1; i<=n; i++)
dp[i][i]=1;
for(int i=n; i>=1; i--)
for(int j=i+1; j<=n; j++)
dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+flag[i][j];
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
}
}
return 0;
}
Codeforces245H - Queries for Number of Palindromes(区间DP)的更多相关文章
- 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] ...
- 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 (容斥原理dp计数)
题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...
- cf245H Queries for Number of Palindromes (manacher+dp)
首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
- 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 ...
- 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\)的范围很小,但是 ...
随机推荐
- WPF中的一些常用类型转换
1.string和Color的转换: //string转Color (Color)ColorConverter.ConvertFromString((string)str); //Color转stri ...
- vim emmet配置
http://nerd-is.in/2013-12/learning-vim-again-1-install-vundle/ http://nerd-is.in/2013-12/learn-vim-a ...
- UIScrollView解决touchesBegan等方法不能触发的解方案
新建一个类继承自UIScrollView 并重写下面的方法 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [su ...
- sencha architect/sencha touch , to prevent breakpoint lost when you debug
add this to your loader config: Ext.Loader.setConfig({ disableCaching: false }); or : click 'applica ...
- uitableviewcell 自适应大小 参考
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ...
- Jquery.Validate验证CheckBoxList,RadioButtonList,DropDownList是否选中
http://blog.csdn.net/fox123871/article/details/8108030 <script type="text/javascript"&g ...
- POJ2225+BFS
简单的BFS 1a /* 从起点到终点 */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...
- JavaSE replaceAll 方法
private String srcStr = "index\\.php\\?action=";//要替换的原字符串 private String destStr = " ...
- [杂题]CSUOJ1274Balls and Boxes
题目链接 题意:中文题 题意不多赘述 值得注意的是n<m 不必考虑n==m的情况 (m是盒子个数, n是每次选取的盒子个数, 不要弄反了!) 这题一看就是同余方程 每次选取n个盒子放球 也就是说 ...
- Altium Designer 特定网络取消 remove loops
在使用Altium Designer时,在PCB Editor 里面可以设定是否需要Automatically remove Loops,但是这个设置是全局的,在设计时难免会遇到对大部分网络是需要删除 ...