uva11584

将课本上所述方法实现即可,代码如下:

/*
 * Author:  Bingo
 * Created Time:  2015/1/25 23:49:49
 * File Name: uva11584.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
const int maxlen=1050;
int d[maxlen];
int s[maxlen][maxlen];
string st;
int work(){
    int len=st.size();
    memset(s,0,sizeof(s));
    for (int i=0;i<len;i++){
        int p,q;
        s[i][i]=1;
        //ji
        p=i-1;q=i+1;
        while (p>=0&&q<len&&st[p]==st[q]){
            s[p][q]=1;
            p--;q++;
        }
        //odd_left
        p=i-1;q=i;
        while (p>=0&&q<len&&st[p]==st[q]){
            s[p][q]=1;
            p--;q++;
        }
        //odd_right
        p=i;q=i+1;
        while (p>=0&&q<len&&st[p]==st[q]){
            s[p][q]=1;
            p--;q++;
        }
    }
}//s[i][j]是否为回文串
int main()
{
    int T;
    cin>>T;
    //string st;
    while (T--){
        cin>>st;
        work();
        int len=st.size();
        for (int i=0;i<len;i++)
            d[i]=100000;
        for (int i=0;i<len;i++){
            int j=0;
            if (s[0][i]) d[i]=1;
            else
            for (j=0;j<i;j++){
                if (s[j+1][i]) d[i]=min(d[i],d[j]+1);
            }
        }
        cout<<d[len-1]<<endl;
    }
    return 0;
}

时间: 2024-10-11 22:17:23

uva11584的相关文章

uva11584(暴力DP)

题意: 给出一个串,把它切成尽量少的串,使每个串都是回文; 思路: dp[i]表示前i个字符最少能划分成几个串; 枚举j 如果j 到 i是一个回文; 那么dp[i] = dp[j - 1] +1:找出最小值: AC: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 1005; char str[N]; int dp[N]; int main

【uva-11584】Partitioning by Palindromes(dp)

粗略的复杂度是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<

例题9-7 划分成回文串 UVa11584

1.题目描述:点击打开链接 2.解题思路:本题要求划分回文串,且个数尽可能的少.可以用动态规划解决.先提前判断i~j是否构成回文串,时间复杂度是O(N^2),然后定义d(i)表示0~i-1划分成的回文串的最小个数.则状态转移方程为: d(i)=min(d(i),d(j)+1)(s[j...i]是回文串) 上式中,d(i)的初始值是i,这样每次判断只需要O(1)的时间,总时间复杂度是O(N^2).当然,判断回文串的过程可以和状态转移相结合,细节请参考第二份代码. 3.代码: #define _CR

UVA-11584:Partitioning by Palindromes(基础DP)

今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比.总结.理解,但难度上降低了. We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not. A partition of a sequenc

UVA11584---Partitioning by Palindromes(dp)

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For exam- ple, ' racecar ' is a palindrome, but ' fastcar ' is not. A partition of a sequence of char- acters is a list of one or more disjoint non-empt

UVa 11584 划分成回文串

https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=min{d[i],d[j]+1},当然前提是j+1~i是回文串,我们可以预处理计算出所有的回文串,这样转移时就比较方便. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<s

UVA - 11584 Partitioning by Palindromes[序列DP]

UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a lis