codeforces D. Palindrome pairs 动态规划
2012-04-25 01:34
197 查看
刚开始作道题 我只是想到一个 n四次方的算法 不敢打啊!!!后来和磊哥讨论讨论 得到一个 n二次方的算法 还可以 就是
计算出一i为尾节点的回文个数dp1[i],计算出以i为头节点的个数dp2[i],这样只要做相乘就可以啦 这个事n方,
之前处理dp1[i],dp2[i]也是n方的算法 详细看代码吧!!!!
计算出一i为尾节点的回文个数dp1[i],计算出以i为头节点的个数dp2[i],这样只要做相乘就可以啦 这个事n方,
之前处理dp1[i],dp2[i]也是n方的算法 详细看代码吧!!!!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; char a[2005]; long long dps[2005],dpe[2005]; int main() { while(~scanf("%s",&a)) { int len=strlen(a); for(int i=0;i<=len;i++) { dps[i]=dpe[i]=1; } for(int i=0;i<len;i++)//偶数回文 { for(int j=0;j<(len/2+1);j++) { if((i-j)<0||(i+j+1)>=len) break; if(a[i-j]==a[i+1+j]) { dps[i-j]++; dpe[i+1+j]++; } else break; } } for(int i=1;i<len;i++) //奇数回文 { for(int j=1;j<(len/2+1);j++) { if((i-j)<0||(i+j)>=len) break; if(a[i-j]==a[i+j]) { dps[i-j]++; dpe[i+j]++; } else break; } } long long ans=0; for(int i=0;i<len;i++) { long long sum=0; for(int j=i+1;j<len;j++) sum+=dps[j]; ans+=sum*dpe[i]; } cout<<ans<<endl; } return 0; }
相关文章推荐
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
- Codeforces - Educational Codeforces Round 14B - s-palindrome(模拟)
- Educational Codeforces Round 26 A B C 三道水题 - - D 动态规划
- Codeforces Round #411 (Div. 2) B. 3-palindrome(思维题)
- 动态规划训练10 [Coloring Brackets CodeForces - 149D]
- Codeforces Beta Round #7 D. Palindrome Degree hash
- Codeforces Round #186 (Div. 2) D. Ilya and Roads(区间类动态规划)
- CodeForces Manthan 2011 D. Optical Experiment(动态规划)
- codeforces Educational Codeforces Round 2 C Make Palindrome
- Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划★ ★
- CodeForces #179(295A|295B|295C)|动态规划|最短路径|前缀和
- Educational Codeforces Round 14 B. s-palindrome 水题
- Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
- Codeforces Round #363 (Div. 2) C. Vacations(DP 动态规划)
- codeforces D.MADMAX 动态规划、记忆化搜索
- Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划
- 【动态规划】【滚动数组】Educational Codeforces Round 26 D. Round Subset
- Educational Codeforces Round 10 C. Foe Pairs —— 后缀和
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本