LightOJ - 1044 Palindrome Partitioning(DP)
来源:互联网 发布:php mvc框架 编辑:程序博客网 时间:2024/05/18 03:58
题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小
解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分
接着枚举j,如果[i,j]回文,那么dp[i] = min(dp[i], dp[j - 1] + 1)
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int N = 1010;char str[N];int dp[N];int cas = 1;bool judge(int i, int j) { while (i <= j) { if (str[i] != str[j]) return false; i++; j--; } return true;}void solve() { scanf("%s", str); int len = strlen(str); for (int i = 0; i < len; i++) { dp[i] = INF; for (int j = 0; j <= i; j++) { if (judge(j, i)) { if (j == 0) dp[i] = 1; else dp[i] = min(dp[i], dp[j - 1] + 1); } } } printf("Case %d: %d\n", cas++, dp[len - 1]);}int main() { int test; scanf("%d", &test); while (test--) solve(); return 0;}
0 0
- lightoj 1044 Palindrome Partitioning(dp)
- LightOJ - 1044 Palindrome Partitioning(DP)
- LightOJ 1044 - Palindrome Partitioning(DP)
- LightOJ 1044 - Palindrome Partitioning【dp】
- lightoj 1044 - Palindrome Partitioning DP优化DP
- LightOJ 1044 - Palindrome Partitioning(dp)
- LightOJ 1044 - Palindrome Partitioning (区间dp)
- lightoj 1044 - Palindrome Partitioning
- lightoj 1044 - Palindrome Partitioning
- Light OJ 1044 Palindrome Partitioning (hash+DP)
- 1044 - Palindrome Partitioning(记忆化DP)
- 【DP】Palindrome Partitioning I
- 【DP】Palindrome Partitioning II
- DP Palindrome Partitioning II
- [leetcode][DP] Palindrome Partitioning II
- leetcode---palindrome-partitioning-ii---dp
- leetcode---palindrome-partitioning---dp, dfs
- leetcode Palindrome Partitioning II DP 深搜
- 【NOIP】10.28集训总结
- 这两天的总结
- 10.28NOIP模拟总结
- LayoutInflater的inflate方法
- Intellij idea Ultimate 14.* 密钥生成器
- LightOJ - 1044 Palindrome Partitioning(DP)
- 怎样在spring初始化完成后执行一些操作?
- C语言左值右值的个人见解
- 什么是事务
- js控制div展开和收回
- 飞凌2440开发板的led驱动和应用程序
- 求三个数的最大最小值
- 欢迎使用CSDN-markdown编辑器
- 使用函数找出指定范围内的完数