UVA 11584 Partitioning by Palindromes 划分成回文串(DP + 预处理)
来源:互联网 发布:网络借贷平台 风险 编辑:程序博客网 时间:2024/06/06 16:43
大大体题意:
给你n个字符串,求出能把这个字符串划分成最少几个回文串?
思路:
很简单的dp,做了好几遍了,今天才优化到n^2的复杂度= =!
令dp[i],表示从位置1到位置i 最少划分的回文串数!
那么直接二重循环,如果j~i是回文串的化,那么dp[i] = min(dp[i],dp[j-1]+1);
判断j~i是否回文直接预处理即可!
方法时枚举回文串的中心,分奇数偶数讨论以下, 当两段字符不相同跳出循环,否则记录!
以下代码是10ms代码,还待优化!
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1000 + 10;char s[maxn];int dp[maxn];bool g[maxn][maxn];int main(){ int T; scanf("%d",&T); while(T--){ memset(g,0,sizeof g); memset(dp,0x3f3f3f3f,sizeof dp); dp[0] = 0; scanf("%s",s+1); int len = strlen(s+1); for (int i = 1; i <= len; ++i){ g[i][i] = 1; for (int j = 1; ; ++j){ if (i-j < 1 || i+j > len)break; if (s[i-j] == s[i+j])g[i-j][i+j] = 1; else break; } if (s[i] == s[i+1]){ g[i][i+1] = 1; for (int j = 1; ; ++j){ if (i-j < 1 || i+1+j > len)break; if (s[i-j] != s[i+1+j])break; g[i-j][i+1+j] = 1; } } } for (int i = 1; i <= len; ++i){ for (int j = 1; j <= i; ++j){ if (g[j][i]) dp[i] = min(dp[j-1] + 1,dp[i]); } } printf("%d\n",dp[len]); } return 0;}
0 0
- UVA 11584 Partitioning by Palindromes 划分成回文串(DP + 预处理)
- uva 11584 Partitioning by Palindromes 回文+dp
- UVa 11584 - Partitioning by Palindromes 回文串dp
- UVA 11584 Partitioning by Palindromes 回文串dp
- uva 11584Partitioning by Palindromes dp(最少回文串划分)
- UVA 11584 Partitioning by Palindromes (回文DP,4级)
- UVA-11584-Partitioning by Palindromes(区间DP 最小回文串数量)
- uva 11584 - Partitioning by Palindromes (dp)
- 【uva-11584】Partitioning by Palindromes(dp)
- UVA 11584-Partitioning by Palindromes(DP)
- UVa 11584 - Partitioning by Palindromes(DP)
- UVa 11584:Partitioning by Palindromes(DP)
- Partitioning by Palindromes(回文串-->字符串DP)
- uva 11584 - Partitioning by Palindromes(简单dp)
- UVA 11584 - Partitioning by Palindromes(基础dp)
- uva 11584 - Partitioning by Palindromes(DP)
- UVA 11584 - Partitioning by Palindromes DP
- uva 11584 - Partitioning by Palindromes(dp)
- 比较字典序的模版函数
- Uri详解
- 优雅编程之这样设计通用程序,你就“正常”了(二十七)
- firefox console不能粘js贴解决
- java同步代码块问题(售票员)
- UVA 11584 Partitioning by Palindromes 划分成回文串(DP + 预处理)
- [LeetCode]--21. Merge Two Sorted Lists
- 使用RPM包安装PBS Pro
- 用C++编写老鼠迷宫,可以用方向键控制
- 迭代器基本原理
- UVA 11520 Fill the Squre(水题)
- 线性表之链表
- web框架之Spring-MVC环境搭建
- tq210-uboot nfs启动TQ210自带的linux系统