Problem H: Partitioning by Palindromes
We say a sequence of characters is a
A
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 begins with the number
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
题意: 将一段字符串划分为尽可能少的回文串, 输出最少能划分为多少个回文串.
解题思路:
代码:
O(n^3)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1005
const int INF = (1<<29);
int n;
int dp[MAX];
char str[MAX];
inline int min(int a, int b)
{
}
bool judge(int left, int right)
{
}
int main()
{
//
}
O(n^2)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1005
const int INF = (1<<29);
char str[MAX];
int dp[MAX];
bool flag[MAX][MAX]; //flag[i][j]: 长度为i, 从str[j]开始有一段回文串
inline int min(int a, int b)
{
}
void init(int len)
{
}
int main()
{
//
}