UVA11584-Partitioning by Palindromes(动态规划基础)

Problem UVA11584-Partitioning by Palindromes

Accept: 1326  Submit: 7151
Time Limit: 3000 mSec

Problem Description

Input

Input begins with the number n of test cases. Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.

 Output

For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.

 Sample Input

3

racecar

fastcar

aaadbccb

Sample Output

1

7

3

题解:思路很明显,dp[i]的含义是前i个字符组成的字符串所能划分成的最少回文串的个数,定义好这个状态就很简单了,dp[i]肯定是从dp[j]转移过来(j<i)并且需要j+1到i是回文串,此时

dp[i] = min(dp[i], dp[j] + 1),方程有了,边界也没啥困难的地方。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int maxn = 1000 + 10;
 6 const int INF = 0x3f3f3f3f;
 7
 8 char str[maxn];
 9
10 int is_palindromes[maxn][maxn];
11 int dp[maxn];
12
13 int Is_palindromes(int j, int i) {
14     if (j >= i) return 1;
15     if (is_palindromes[j][i] != -1) return is_palindromes[j][i];
16
17     if (str[i] == str[j]) {
18         return is_palindromes[j][i] = Is_palindromes(j + 1, i - 1);
19     }
20     else return is_palindromes[j][i] = 0;
21 }
22
23 int main()
24 {
25     //freopen("input.txt", "r", stdin);
26     int iCase;
27     scanf("%d", &iCase);
28     while (iCase--) {
29         scanf("%s", str + 1);
30         memset(is_palindromes, -1, sizeof(is_palindromes));
31         dp[0] = 0;
32         int len = strlen(str + 1);
33         for (int i = 1; i <= len; i++) {
34             dp[i] = i;
35             for (int j = 0; j < i; j++) {
36                 if (Is_palindromes(j + 1, i)) dp[i] = min(dp[i], dp[j] + 1);
37             }
38         }
39         printf("%d\n", dp[len]);
40     }
41     return 0;
42 }

原文地址:https://www.cnblogs.com/npugen/p/9746263.html

时间: 2024-10-24 23:21:38

UVA11584-Partitioning by Palindromes(动态规划基础)的相关文章

UVA-11584:Partitioning by Palindromes(基础DP)

今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比.总结.理解,但难度上降低了. We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not. A partition of a sequenc

【uva-11584】Partitioning by Palindromes(dp)

粗略的复杂度是L^3,长度最大是1000,,没敢做,之后发现其实这个复杂度的系数也不大,可以过,而且很快. dp[j] = dp[i - 1] + 1 (if(str[i] ~ str[j]为回文) 14327451 11584 Partitioning by Palindromes Accepted C++ 0.052 2014-10-09 09:33:17 #include<set> #include<map> #include<stack> #include<

UVA - 11584 Partitioning by Palindromes[序列DP]

UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a lis

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

nyist oj 36 最长公共子序列 (动态规划基础题)

最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列. 输入 第一行给出一个整数N(0<N<100)表示待测数据组数 接

UVa 11584 Partitioning by Palindromes

/*---UVa 11584 Partitioning by Palindromes --用dp[i]表示前i个字母划分成最小回文串的个数,则有dp[i]=min{dp[j]+1}s[j+1...i]是一个回文串,状态O(n)个 每次有O(n)个决策,而判断是否是回文串复杂度O(n),这样总的复杂度O(n^3).如果事先预处理标记一下s[i...j]是否构成 回文串,这样总的复杂度O(n^2).标记s[i...j]是否构成回文串,用vis[i][j](i<=j)来标记,if s[i]!=s[j]

UVa 11583 Partitioning by Palindromes

 Description Problem H: Partitioning by Palindromes We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not. A partition of a sequence of charact

UVA 11584 一 Partitioning by Palindromes

Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11584 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char a[1

Uva 11584 - Partitioning by Palindromes dp

Problem H: Partitioning by Palindromes We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not. A partition of a sequence of characters is a list o