light oj 1025 区间dp
来源:互联网 发布:mysql书籍推荐 知乎 编辑:程序博客网 时间:2024/05/17 01:02
题意:给一个字符串,问有多少种方法通过去除中的字符使之变成回文串。
思路:区间dp,dp[i][j]表示区间i~j的方案总数。
对于串str,两个for进行控制,第一个for从后往前,第二个for从i+1开始,到串的最后,如果str[i] == str[j],那么dp[i][j] += dp[i+1][j-1] + 1,因为如果去掉i+1~j-1之后,因为str[i] == str[j] 串还是回文的。如果加上str[i]和str[j],因为相等,那么i~j 也还是回文的。
如果不考虑str[i] 和str[j] ,那么dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]。
#include <stdio.h>#include <string.h>#pragma warning (disable :4996)const int Max = 66;long long dp[Max][Max];int main(){int T;char str[Max];scanf("%d ", &T);for (int t = 1; t <= T; t++){gets(str);int n = strlen(str);memset(dp, 0, sizeof(dp));for (int i = 0; str[i]; i++)dp[i][i] = 1;for (int i = n - 2; i >= 0; 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];if (str[i] == str[j])dp[i][j] += dp[i + 1][j - 1] + 1;}}printf("Case %d: %lld\n", t, dp[0][n - 1]);}return 0;}
0 0
- light oj 1025 区间dp
- Light oj 1025 (区间dp)
- light oj 1422,区间dp
- light oj 1422(区间dp)
- Light OJ 1031 区间dp
- 区间DP :Light OJ 1031
- Light OJ 1025 The Specials Menu(区间DP)
- Light OJ 1025 The Specials Menu (区间DP)
- Light oj 1422 Halloween Costumes(区间 dp)
- Light OJ 1422 - Halloween Costumes (区间DP)
- light oj 1031 区间dp+博弈
- Light oj 1422 Halloween Costumes 区间DP
- Light OJ 1422 Halloween Costumes(区间DP)
- light oj 1033 - Generating Palindromes (区间dp)
- Light OJ 1031 Easy Game (区间DP)
- light oj 1031(区间dp入门)
- Light OJ 1031 Easy Game 区间DP
- light oj 1031(区间dp+博弈)
- Template Method Pattern
- dup和dup2函数的不同和使用
- LeakCanary: 让内存泄露无所遁形
- android自定义view组件circleprogress阅读笔记
- isset()和unset()函数
- light oj 1025 区间dp
- Reachability 类检测网络状态
- 黑马程序员——字符流,字节流
- LeetCode(15) 3Sum
- poj-1094-Sorting It All Out-拓扑排序
- 开源库x264的编译
- SPRING依赖注入
- self和super网上的小题目
- 【C】队列报数问题