码迷,mamicode.com
首页 > 其他好文 > 详细

【uva-11584】Partitioning by Palindromes(dp)

时间:2014-10-09 20:14:17      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   2014   

 粗略的复杂度是L^3,长度最大是1000,,没敢做,之后发现其实这个复杂度的系数也不大,可以过,而且很快。

dp[j] = dp[i - 1] + 1 (if(str[i] ~ str[j]为回文)

14327451 11584 Partitioning by Palindromes Accepted C++ 0.052 2014-10-09 09:33:17
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
const int maxn = 1111;
char str[maxn];
int   dp[maxn];
bool is_part(int i,int j){
    while(i < j){
        if(str[i] != str[j])
            return false;
        i ++;
        j --;
    }
    return true;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",str);
        int L = strlen(str);
        for(int i = 0 ; i <= L; i ++) dp[i] = i + 1;
        for(int j = 0 ; j < L; j ++) // L
            for(int i = 0 ; i <= j ; i ++){ // L
                if(is_part(i,j)){ // L
                    if(i - 1 >= 0)
                        dp[j] = min(dp[j],dp[i - 1] + 1);
                    else
                        dp[j] = 1;
                }
            }
        printf("%d\n",dp[L - 1]);
    }
    return 0;
}

【uva-11584】Partitioning by Palindromes(dp)

标签:style   http   color   io   os   ar   for   sp   2014   

原文地址:http://blog.csdn.net/u013451221/article/details/39934631

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!