码迷,mamicode.com
首页 > 其他好文 > 详细

UVA11584---Partitioning by Palindromes(dp)

时间:2015-05-21 22:45:42      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:dp

We say a sequence of characters is a
palindrome if it is the same written
forwards and backwards. For exam-
ple, ‘
racecar
’ is a palindrome, but

fastcar
’ is not.
A
partition
of a sequence of char-
acters is a list of one or more disjoint
non-empty groups of consecutive char-
acters whose concatenation yields the
initial sequence. For example, (‘
race
’,

car
’) is a partition of ‘
racecar
’ into
two groups.
Given a sequence of characters, we
can always create a partition of these
characters such that each group in the
partition is a palindrome! Given this
observation it is natural to ask: what
is the minimum number of groups
needed for a given string such that ev-
ery group is a palindrome?
For example:
?

racecar
’ is already a palin-
drome, therefore it can be par-
titioned into one group.
?

fastcar
’ does not contain any
non-trivial palindromes, so it
must be partitioned as (‘
f
’, ‘
a
’,

s
’, ‘
t
’, ‘
c
’, ‘
a
’, ‘
r
’).
?

aaadbccb
’ can be partitioned as
(‘
aaa
’, ‘
d
’, ‘
bccb
’).
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.
Universidad de Valladolid OJ:
11584 – Partitioning by Palindromes
2/2
Sample Input
3
racecar
fastcar
aaadbccb
Sample Output
1
7
3

O(n2)预处理出(i,j)是否为回文串
然后dp[i] 表示把0-i字符划分成最少的回文串的数目

/*************************************************************************
    > File Name: uva11584.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月21日 星期四 18时27分01秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 1010;
int dp[N];
bool ok[N][N];
string str;

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        cin >> str;
        memset(dp, inf, sizeof(dp));
        memset(ok, 0, sizeof(ok));
        int n = str.length();
        for (int i = 0; i < n; ++i) {
            ok[i][i] = 1;
        }
        for (int i = n - 2; i >= 0; --i) {
            if (str[i] == str[i + 1]) {
                ok[i][i + 1] = 1;
            }
            for (int j = i + 2; j < n; ++j) {
                if (ok[i + 1][j - 1] && str[i] == str[j]) {
                    ok[i][j] = 1;
                }
            }
        }
        dp[0] = 1;
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j <= i; ++j) {
                if (ok[j][i]) {
                    if (!j) {
                        dp[i] = 1;
                    }
                    else {
                        dp[i] = min(dp[i], dp[j - 1] + 1);
                    }
                }
            }
        }
        printf("%d\n", dp[n - 1]);
    }
    return 0;
}

UVA11584---Partitioning by Palindromes(dp)

标签:dp

原文地址:http://blog.csdn.net/guard_mine/article/details/45895469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!