Partitioning by Palindromes UVA - 11584 动态规划

题意是给定一个字符串,求最少把它分成多少份,使的每一份都是回文串。

错了好多遍,最后发现是贪心思路出了问题,一开始求出dp[i][j],dp[i][j]表示第i个到第j个这段子串是否是回文串,求出来后,我直接从1开始遍历,每次求最右边的,这种贪心思路是有问题的。

看了赛后别人的才知道可以同动态规划,dp1[i]表示从1到第i个最少分成多少份,知道这思路就可以直接写了。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <string>
 6 #include <queue>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #define ll long long
11 #define INF 0x3f3f3f3f
12 #define lowvit(x) x&(-x)
13 #define N 1010
14 #define M 110
15 using namespace std;
16 char str[N];
17 bool dp[N][N];
18 int dp1[N];
19 int main() {
20     int t;
21     cin>>t;
22     while(t--) {
23         memset(dp, 0, sizeof(dp));
24         memset(str, 0, sizeof(str));
25         memset(dp1, 0, sizeof(dp1));
26         cin>>str+1;
27         int len = strlen(str+1);
28         for(int i = 1; i <= len; i ++) {
29             dp[i][i] = 1;dp1[i] = INF;
30             if(i+1 <= len && str[i] == str[i+1]) dp[i][i+1] = 1;
31         }
32         for(int i = 3; i <= len; i ++) {
33             for(int j = 1; j <= len-i+1; j ++) {
34                 int r = j+i-1;
35                 if(dp[j+1][r-1] && str[j] == str[r]) dp[j][r] = 1;
36             }
37         }
38         dp1[0] = 0;
39         for(int i = 1; i <= len; i ++) {
40             for(int j = 1; j <= len; j ++) {
41                 if(dp[j][i]) {
42                     dp1[i] = min(dp1[i],dp1[j-1]+1);
43                 }
44             }
45         }
46         cout << dp1[len] << endl;
47     }
48     return 0;
49 }
时间: 2024-08-02 23:15:03

Partitioning by Palindromes UVA - 11584 动态规划的相关文章

Partitioning by Palindromes UVA - 11584

题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串. 题解:d(i)为字符0~i划分成的最小回文串的个数,则d[ i ]=min{ d[ j ]+1 |s[ j+1~ i ]是回文串 }. 注意要预处理,其次是怎么初始化...很重要 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define INF 1e8 6 u

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 <

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 - 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

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

区间DP UVA 11584 Partitioning by Palindromes

题目传送门 1 /* 2 题意:给一个字符串,划分成尽量少的回文串 3 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 4 如果s[j] 到 s[i]是回文串,那么可以从dp[j-1] + 1递推过来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath&g

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

UVa 11584 Partitioning by Palindromes(DP 最少对称串)

题意  判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串 令d[i]表示给定串的前i个字母至少可以分解为多少个对称串  那么对于j=1~i   若(i,j)是一个对称串  那么有  d[i]=min(d[i],d[j-1]+1)   然后就得到答案了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N

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