Uva11584 Partitioning by Palindromes

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 of one or more disjoint non-empty groups of consecutive characters 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 every group is a palindrome? For example: • ‘racecar’ is already a palindrome, therefore it can be partitioned 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. Sample Input 3 racecar fastcar aaadbccb Sample Output 1 7 3

https://odzkskevi.qnssl.com/5cc02c8be522c16812da27d55b5790ce?v=1508140214

【题解】

预处理ok[i][j]表示i到j是否组成回文串,枚举中间点即可

注意偶数回文串的情况

dp[i]表示前i个数的最小划分,转移即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 #define max(a, b) ((a) > (b) ? (a) : (b))
 8 #define min(a, b) ((a) < (b) ? (a) : (b))
 9 inline void swap(int &a, int &b)
10 {
11     int tmp = a;a = b;b = tmp;
12 }
13 inline void read(int &x)
14 {
15     x = 0;char ch = getchar(), c = ch;
16     while(ch < '0' || ch > '9')c = ch, ch = getchar();
17     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
18     if(c == '-')x = -x;
19 }
20 
21 const int INF = 0x3f3f3f3f;
22 const int MAXN = 1000 + 10;
23 
24 int t,n,dp[MAXN], ok[MAXN][MAXN];
25 char s[MAXN];
26 
27 int main()
28 {
29     read(t);
30     for(;t;--t)
31     {
32         memset(ok, 0, sizeof(ok));
33         scanf("%s", s + 1);
34         n = strlen(s + 1);
35         for(register int i = 1;i <= n;++ i)
36         {
37             int j = i;
38             int tmp = i;
39             while(s[tmp] == s[j] && tmp >= 1 && j <= n)
40             {
41                 ok[tmp][j] = 1;
42                 -- tmp, ++ j;
43             }
44             tmp = i;
45             j = tmp + 1;
46             while(s[tmp] == s[j] && tmp >= 1 && j <= n)
47             {
48                 ok[tmp][j] = 1;
49                 -- tmp, ++ j;
50             }
51         } 
52         for(register int i = 1;i <= n;++ i)
53         {
54             dp[i] = INF;
55             for(register int j = 1;j <= i;++ j)
56                 if(ok[j][i]) dp[i] = min(dp[i], dp[j - 1] + 1);
57         }
58         printf("%d
", dp[n]);
59     }
60     return 0;
61 }
Uva11584
原文地址:https://www.cnblogs.com/huibixiaoxing/p/7691127.html