uva 11584 Partitioning by Palindromes 线性dp

14 篇文章 0 订阅
6 篇文章 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值