uva 11584(动态规划起步第四天 线性DP)
2015-02-17 15:16
399 查看
题目很容易,找到状态DP[i] 表示前 i 个字符中最少的划分,那么转移
DP[i] = min {DP[j] + 1 | s[j +1]...s[i] 为 palindromes}
DP[i] = min {DP[j] + 1 | s[j +1]...s[i] 为 palindromes}
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define REP(i,N) for (int i = 0;i < (N);i++) #define REP_1(i,N) for (int i = 1;i < (N);i++) #define REP_2(i,be,en) for (int i = (be);i < (en);i++) #define DWN(i,N) for (int i = N;i >= 0;i--) #define INF 0x3f3f3f3f #define MAXN 1010 using namespace std; char str[MAXN]; int dp[MAXN]; bool is_palindromes(int i,int j) { if (i >= j) return 1; if (str[i] == str[j]) return is_palindromes(i + 1,j - 1); else return 0; } int main () { int T; freopen("1.txt","r",stdin); cin >> T; while (T--) { scanf("%s",str + 1); int len = strlen(str + 1); REP(i,len + 1) { dp[i] = i; REP(j,i) { if (is_palindromes(j + 1,i)) { dp[i] = min(dp[j] + 1,dp[i]); } } } cout << dp[len] << endl; } }
相关文章推荐
- uva 11400 (动态规划起步第四天 线性DP)
- Uva 11584 线性DP
- uva 11584 Partitioning by Palindromes 线性dp
- uva11584 划分成回文串 线性dp
- Partitioning by Palindromes UVA - 11584(线性dp)
- UVA-11584 Partitioning by Palindromes (简单线性DP)
- uva 10003(动态规划起步第四天 矩阵连乘)
- uva 11584 Partitioning by Palindromes 线性dp
- 【动态规划】【树形DP】[UVa 10859]Placing Lampposts
- uva 12563(动态规划起步第三天 01背包变形)
- UVA 11584 Partitioning by Palindromes (回文DP,4级)
- UVa11584 - Partitioning by Palindromes(dp)
- Uva1625 线性DP
- 【算法笔记】动态规划 线性DP
- uva 11584 字符串 dp
- UVa11584 - Partitioning by Palindromes(区间DP)
- uva 11584 - Partitioning by Palindromes(DP)
- uva 437 ( 动态规划起步第二天 DAG)
- UVa - 11854 - Partitioning by Palindromes (线性动态规划)
- UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数