UVa11584 -Partitioningby Palindromes
2016-09-27 19:29
295 查看
UVa11584 -Partitioningby Palindromes
题意:给定一个字符串,求该字符串最少能被分为几个回文子串。
分析:很明显的区间dp,问题规模为判断1到n这n个字符的字符串至少可以被划分为的回文子串个数,刚开始定义dp[i][j]为i到j这些子字符串至少可以划分为回文串的个数,那么dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j],但是转念一想,这样肯定会超时的,还要判断是否为回文串,就不用说了。
状态转移过多,时间复杂度承受不了,没办法,只能降低维度,定义dp[i]为1到i前i个字符至少被划分成回文串的个数。那会不会有问题呢?不会。因为根本没必要dp中间的子串,对结果无影响。dp[i] = min(dp[i],dp[j]+x),如果j+1到i为回文串的话,x为1,否则为INF,这样dp就行了。在之前预处理一遍区间是否为回文串,这同样可以用dp处理。.
增加维度是为了更准确地进行规划最优值,更细致地描述状态,但是如果维度过多,转移也多,时间复杂度也会变大,那么便可以降低维度,转换状态进行dp。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char s[1010];
int f[1010][1010], dp[1010];
int x, y, len;
void DP()
{
int flag = 1;
for (int i = x, j = y; i >= 1 && j <= len; i--, j++){
if (flag){
if (s[i] == s[j]) f[i][j] = 1;
else f[i][j] = INF, flag = 0;
}
else f[i][j] = INF;
}
}
int main()
{
dp[1] = 1;
int t;
scanf("%d", &t);
while (t--){
scanf("%s", s+1);
len = strlen(s+1);
f[len][len] = 1;
for (x = 1; x < len; x++)
for (y = x; y <= x+1; y++) DP();
for (int i = 2; i <= len; i++){
dp[i] = INF;
for (int j = 0; j < i; j++) dp[i] = min(dp[i], dp[j]+f[j+1][i]);
}
printf("%d\n", dp[len]);
}
return 0;
}
其实上面的dp还是可以优化的:减少没必要的转移,只有当j+1到i为回文串时才转移,而且预处理其实没必要,因为每个区间只判断了一次,并不会重复计算。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char s[1010];
int f[1010][1010], dp[1010];
int check(int x, int y)
{
for (int i = x, j = y; i < j; i++, j--)
if (s[i] != s[j]) return 0;
return 1;
}
int main()
{
dp[1] = 1;
int t;
scanf("%d", &t);
while (t--){
scanf("%s", s+1);
int len = strlen(s+1);
for (int i = 2; i <= len; i++){
dp[i] = i;
for (int j = 0; j < i; j++)
if (check(j+1, i)) dp[i] = min(dp[i], dp[j]+1);
}
printf("%d\n", dp[len]);
}
return 0;
}
题意:给定一个字符串,求该字符串最少能被分为几个回文子串。
分析:很明显的区间dp,问题规模为判断1到n这n个字符的字符串至少可以被划分为的回文子串个数,刚开始定义dp[i][j]为i到j这些子字符串至少可以划分为回文串的个数,那么dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j],但是转念一想,这样肯定会超时的,还要判断是否为回文串,就不用说了。
状态转移过多,时间复杂度承受不了,没办法,只能降低维度,定义dp[i]为1到i前i个字符至少被划分成回文串的个数。那会不会有问题呢?不会。因为根本没必要dp中间的子串,对结果无影响。dp[i] = min(dp[i],dp[j]+x),如果j+1到i为回文串的话,x为1,否则为INF,这样dp就行了。在之前预处理一遍区间是否为回文串,这同样可以用dp处理。.
增加维度是为了更准确地进行规划最优值,更细致地描述状态,但是如果维度过多,转移也多,时间复杂度也会变大,那么便可以降低维度,转换状态进行dp。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char s[1010];
int f[1010][1010], dp[1010];
int x, y, len;
void DP()
{
int flag = 1;
for (int i = x, j = y; i >= 1 && j <= len; i--, j++){
if (flag){
if (s[i] == s[j]) f[i][j] = 1;
else f[i][j] = INF, flag = 0;
}
else f[i][j] = INF;
}
}
int main()
{
dp[1] = 1;
int t;
scanf("%d", &t);
while (t--){
scanf("%s", s+1);
len = strlen(s+1);
f[len][len] = 1;
for (x = 1; x < len; x++)
for (y = x; y <= x+1; y++) DP();
for (int i = 2; i <= len; i++){
dp[i] = INF;
for (int j = 0; j < i; j++) dp[i] = min(dp[i], dp[j]+f[j+1][i]);
}
printf("%d\n", dp[len]);
}
return 0;
}
其实上面的dp还是可以优化的:减少没必要的转移,只有当j+1到i为回文串时才转移,而且预处理其实没必要,因为每个区间只判断了一次,并不会重复计算。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char s[1010];
int f[1010][1010], dp[1010];
int check(int x, int y)
{
for (int i = x, j = y; i < j; i++, j--)
if (s[i] != s[j]) return 0;
return 1;
}
int main()
{
dp[1] = 1;
int t;
scanf("%d", &t);
while (t--){
scanf("%s", s+1);
int len = strlen(s+1);
for (int i = 2; i <= len; i++){
dp[i] = i;
for (int j = 0; j < i; j++)
if (check(j+1, i)) dp[i] = min(dp[i], dp[j]+1);
}
printf("%d\n", dp[len]);
}
return 0;
}
相关文章推荐
- 【动态规划】[UVa11584]Partitioning by Palindromes
- Partitioning by Palindromes UVA - 11584
- 【线性结构上的动态规划】UVa 11584 - Partitioning by Palindromes
- Uva-11584-Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes 回文串dp
- UVA 11584 Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVa-11584 - Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes[序列DP]
- UVA11584 - Partitioning by Palindromes - 动态规划
- UVA11584[Partitioning by Palindromes] 动态规划
- UVA 11584 Partitioning by Palindromes
- uva 11584 Partitioning by Palindromes 线性dp
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes 回文串dp
- uva_11584_Partitioning by Palindromes( DP )
- UVa 11584 Partitioning by Palindromes
- Partitioning by Palindromes UVA - 11584
- UVa 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes