UVa11584-Partitioning by Palindromes

http://uva.onlinejudge.org/external/115/11584.html

将所给的字符串,最少可以分成多少个回文串,dp[i]表示0~i之间最少可以分成的值,然后判断j~i(j <= i)之间是否是回文,如果是回文dp[i] = min(dp[i], dp[j - 1] + 1)

/*************************************************************************
 > File Name: UVa11584.cpp
 > Author: AcToy
 > Mail: ycsgldy@163.com 
 > Created Time: 2013年07月18日 星期四 10时02分17秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
typedef unsigned int u32;
typedef long long i64;
typedef unsigned long long u64;
typedef vector<int> IV;
typedef vector<bool> BV;
typedef pair<int,int> II;
typedef vector<II> IIV;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
const int INF = 0x7FFFFFFF;
const double eps = 1E-10;
const double PI = acos(-1);
int Case, dp[1010];
string str;
bool is_pal(int i, int j) {
	int mid = (j - i) / 2;
	for(int k = 0; k <= mid; ++k) 
		if(str[i + k] != str[j - k]) return 0;
	return 1;
}

int main()
{
	cin >> Case;
	while(Case--) {
		cin >> str;
		int len = str.length();
		dp[0] = 0;
		for(int i = 1; i <= len; ++i) {
			dp[i] = INF;
			for(int j = 1; j <= i; ++j) {
				if(is_pal(j - 1, i - 1)) {
					dp[i] = min(dp[i], dp[j - 1] + 1);
				}
			}
		}
		printf("%d\n", dp[len]);
	}
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值