CROC-MBTU 2012, Elimination Round (ACM-ICPC) H DP题目
2012-11-21 20:54
351 查看
http://codeforces.com/contest/245/problem/H
题意:
给定一个字符串s (1 ≤ |s| ≤ 5000) 然后又q个询问(1 ≤ q ≤ 106) 每次询问包括两个数l,r 求l到r内回文串的个数。
思路:
自己对dp的感觉真是弱爆了,大牛们16分钟就能A出来的题目,自己想了好久还是没思路,最后看了别人的代码才AC的,弱爆了。
dp[i][j] = d[i][j -1] + dp[i + 1][j] - dp[i + 1][j - 1] - R[i][j] 这里R[i][j]表示子串s[i...j]是否是回文串,这里的处理太棒了,自己没能想到。还有就是转移方程也没想到真是弱爆了。还是要继续加油啊。。
题意:
给定一个字符串s (1 ≤ |s| ≤ 5000) 然后又q个询问(1 ≤ q ≤ 106) 每次询问包括两个数l,r 求l到r内回文串的个数。
思路:
自己对dp的感觉真是弱爆了,大牛们16分钟就能A出来的题目,自己想了好久还是没思路,最后看了别人的代码才AC的,弱爆了。
dp[i][j] = d[i][j -1] + dp[i + 1][j] - dp[i + 1][j - 1] - R[i][j] 这里R[i][j]表示子串s[i...j]是否是回文串,这里的处理太棒了,自己没能想到。还有就是转移方程也没想到真是弱爆了。还是要继续加油啊。。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll long long #define inf 0x7f7f7f7f #define MOD 1073741824 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 107 #define N 5007 using namespace std; //freopen("din.txt","r",stdin); int dp ,R ; char s ; int n; int main() { //freopen("din.txt","r",stdin); int q,l,r; int i,j; scanf("%s",s + 1); n = strlen(s + 1); CL(dp,0); CL(R,0); for (i = 1; i <= n; ++i) R[i][i] = R[i + 1][i] = dp[i][i] = 1; for (i = 2; i <= n; ++i)//从长度小的开始处理 { for (j = 1; j <= n; ++j) { int k = j + i - 1; R[j][k] = R[j + 1][k - 1]&&(s[j] == s[k]); dp[j][k] = dp[j + 1][k] + dp[j][k - 1] - dp[j + 1][k - 1] + R[j][k]; } } scanf("%d",&q); while (q--) { scanf("%d%d",&l,&r); printf("%d\n",dp[l][r]); } return 0; }
相关文章推荐
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) E YY题目
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) D题
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) D. Restoring Table
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) E. Mishap in Club
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CF CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) H. Queries for Number of Palindromes
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) 总结
- codeforces CROC-MBTU 2012, Elimination Round (ACM-ICPC) G题 && HDU4039
- Power Station(2012 acm-icpc 金华邀请赛 B题 )树型dp
- 浅谈ACM/ICPC的题目风格和近几年题目的发展
- ACM-ICPC Dhaka Regional 2012 题解
- hdu 4585 Shaolin 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- hdu 4276 The Ghost Blows Light(2012 ACM/ICPC Asia Regional Changchun Online )
- CROC-MBTU 2012, Elimination Round / 245E Mishap in Club (想法题)
- Gym 101205 (ACM-ICPC World Finals 2012)