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 using namespace std;
 7
 8 const int maxn=1005;
 9
10 int len;
11 int dp[maxn];
12 char s[maxn];
13 bool vis[maxn][maxn];
14
15 void init(){
16     memset(vis,false,sizeof(vis));
17     for(int i=0;i<len;i++){
18         int k1=i,k2=i;
19         while(0<=k1&&k2<len&&s[k1]==s[k2]) { vis[k1][k2]=true; k1--,k2++; }
20         k1=i,k2=i+1;
21         while(0<=k1&&k2<len&&s[k1]==s[k2]) { vis[k1][k2]=true; k1--,k2++; }
22     }
23 }
24
25 void solve()
26 {   init();
27     for(int i=1;i<len;i++){
28         if(vis[0][i]) dp[i]=1;
29         for(int j=0;j<i;j++)
30             if(vis[j+1][i]) dp[i]=min(dp[i],dp[j]+1);
31     }
32     int ans=dp[len-1];
33     cout<<ans<<endl;
34
35 }
36
37 int main()
38 {   int kase;
39     cin>>kase;
40     while(kase--){
41         scanf("%s",s);
42         len=strlen(s);
43         for(int i=0;i<len;i++) dp[i]=i+1;
44
45         solve();
46     }
47     return 0;
48 }
时间: 2024-11-14 12:32:33

Partitioning by Palindromes UVA - 11584的相关文章

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 <

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