首页 > 其他 > 详细

uva 11584 Partitioning by Palindromes 线性dp

时间:2015-06-03 23:35:29      阅读:427      评论:0      收藏:0      [点我收藏+]
// uva 11584 Partitioning by Palindromes 线性dp
//
// 题目意思是将一个字符串划分成尽量少的回文串
//
// f[i]表示前i个字符能化成最少的回文串的数目
//
// f[i] = min(f[i],f[j-1] + 1(j到i是回文串))
//
// 这道题还是挺简单的,继续练

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 1008;
char s[maxn];
int f[maxn];
bool vis[maxn][maxn];
int n;

bool ok(int x,int y){
	while(x<y){
		if (s[x]==s[y]){
			x++;
			y--;
		}else {
			return false;
		}
	}
	return true;
}

void init(){
	memset(vis,0,sizeof(vis));
	scanf("%s",s+1);
	n = strlen(s+1);
	f[0]=0;
	for (int i=1;i<=n;i++)
		f[i] = i;

	for (int i=1;i<=n;i++)
		for (int j=i;j<=n;j++){
			if (ok(i,j)){
				vis[i][j]=1;
			}
		}

	for (int i=1;i<=n;i++)
		for (int j=1;j<=i;j++){
			if (vis[j][i]){
				f[i] = min(f[i],f[j-1]+1);
			}
		}
	printf("%d\n",f[n]);
}

int main() {
	int t;
	//freopen("G:\\Code\\1.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		init();
	}
	return 0;
}

uva 11584 Partitioning by Palindromes 线性dp

原文:http://blog.csdn.net/timelimite/article/details/46351639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!