题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000。

分析:

1、dp[i]为字符0~i划分成的最小回文串的个数。

2、dp[j] = Min(dp[j], dp[i - 1] + 1),若i~j是回文串,则更新dp[j]。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
int dp[MAXN];
bool judge(int l, int r){
int len = r - l + 1;
for(int i = 0; i < len / 2; ++i)
if(s[l + i] != s[r - i]) return false;
return true;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(dp, INT_INF, sizeof dp);
scanf("%s", s + 1);
int len = strlen(s + 1);
dp[0] = 0;
for(int i = 1; i <= len; ++i){
for(int j = i; j <= len; ++j){
if(judge(i, j)){
dp[j] = Min(dp[j], dp[i - 1] + 1);
}
}
}
printf("%d\n", dp[len]);
}
return 0;
}

  

UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)的更多相关文章

  1. UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)

    d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...

  2. Uva 11584,划分成回文串

    题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...

  3. UVa 11584 划分成回文串

    https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=mi ...

  4. UVA 11584 Paritioning by Palindromes(动态规划 回文)

    题目大意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串.比如racecar本身就是回文串:fastcar只能分成7个单字母的回文串:aaadbccb最少可分成3个回文串:aaa. ...

  5. UVA11584 划分成回文串

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/B 紫书275 题意:输入一个字符,最少能划分几个回文串 分析 ...

  6. 随手练——Uva-11584 划分成回文串(区间DP)

    思路:dp[i]代表到第i位的最小值,枚举它的前几位,求出最小值. 转移方程:dp[ i ] = min(dp[ i ], dp[ j - 1 ] + 1 ) ; 本来觉得,代码加深部分可以提前bre ...

  7. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  8. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  9. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

随机推荐

  1. ng-校验重复并提示具体重复内容

    //校验其他等级模块是否存在"职业类别"完全一致的等级模块 var moreFlag=false; for(var i=0;i<$scope.djArr.length;i++ ...

  2. 前端学习笔记系列一:12 js中获取时间new date()的用法

    获取时间: 1  var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.get ...

  3. js动态添加li,你添加的li具有你绑定的事件

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <%@page i ...

  4. hibernate通过数据库表反向生成实体类

      步骤一: window-->open Perspective-->MyEclipse Java Persistence 进行了上面的 操作后会出现一个视图DB Brower:MyEcl ...

  5. Sweet Round 1题解

    感谢各位参赛者,所有的题解如下: T1 syx的奖励 这题明显是签到题了吧,随便猜猜结论就A掉了 先说怎么做吧,把所有的可走的数gcd起来,然后再与n求gcd 如果为1,则输出n,若不为1,则输出-1 ...

  6. 深入解读EOS源代码之——区块链内核

    EOS进入大众视野并且受到热议已经有一段时间了,各种热捧和争议过后,是时候让我们静下来搞清楚EOS到底是一个什么样的产品.本文从技术角度深入的分析EOS底层设计,从源代码入手,一层层揭开EOS区块链底 ...

  7. 实训41 S7通信 单向连接 基于DP网络通信

    连接的基本概念? 连接是指两个通信伙伴之间执行通信服务建立的逻辑链路,而不是指两个站之间用物理媒体(例如电缆)实现的连接. 连接相当于 通信伙伴之间 一条虚拟的"专线". 一条物理 ...

  8. MapReduce会自动忽略文件夹下的.开头的文件

    MapReduce会自动忽略文件夹下的.开头的文件,跳过这些文件的处理.

  9. other#apache-commons

    if you want to be a better javaer, you should  spent time on apache commons.

  10. Vue(九)---自定义指令(directive )

    1.无参数 自定义指令的方式:1. 使用Vue.directive 来自定义2. 第一个参数就是 指令名称 xart3. el 表示当前的html dom对象4. 在方法体内就可以通过 innerHT ...