Problem UVA11584-Partitioning by Palindromes

Accept: 1326  Submit: 7151
Time Limit: 3000 mSec

Problem Description

Input

Input begins with the number n of test cases. Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.

 Output

For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.
 

 Sample Input

3
racecar
fastcar
aaadbccb
 

Sample Output

1

7

3

题解:思路很明显,dp[i]的含义是前i个字符组成的字符串所能划分成的最少回文串的个数,定义好这个状态就很简单了,dp[i]肯定是从dp[j]转移过来(j<i)并且需要j+1到i是回文串,此时

dp[i] = min(dp[i], dp[j] + 1),方程有了,边界也没啥困难的地方。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;
const int INF = 0x3f3f3f3f; char str[maxn]; int is_palindromes[maxn][maxn];
int dp[maxn]; int Is_palindromes(int j, int i) {
if (j >= i) return ;
if (is_palindromes[j][i] != -) return is_palindromes[j][i]; if (str[i] == str[j]) {
return is_palindromes[j][i] = Is_palindromes(j + , i - );
}
else return is_palindromes[j][i] = ;
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%s", str + );
memset(is_palindromes, -, sizeof(is_palindromes));
dp[] = ;
int len = strlen(str + );
for (int i = ; i <= len; i++) {
dp[i] = i;
for (int j = ; j < i; j++) {
if (Is_palindromes(j + , i)) dp[i] = min(dp[i], dp[j] + );
}
}
printf("%d\n", dp[len]);
}
return ;
}

UVA11584-Partitioning by Palindromes(动态规划基础)的更多相关文章

  1. UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数

    题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...

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

    今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比.总结.理解,但难度上降低了. We say a sequence of characters is ...

  3. 【题解】UVA11584 Partitioning by Palindromes

    UVA11584 https://www.luogu.org/problemnew/show/UVA11584 暑假开始刷lrj紫/蓝书DP题 这几天做的一道 思路 预处理出所有的回文串是否存在 前提 ...

  4. UVa11584 - Partitioning by Palindromes(区间DP)

    题目大意 给定一个小写字母组成的字符串S,你的任务是划分成尽量少的回文串 题解 方程就是dp[j]=min(dp[i-1]+1)(i<=j,s[i..j]是回文串) 代码: #include&l ...

  5. UVA-11584 Partitioning by Palindromes (简单线性DP)

    题目大意:给一个全是小写字母的字符串,判断最少可分为几个回文子序列.如:“aaadbccb” 最少能分为 “aaa” “d” “bccb” 共三个回文子序列,又如 “aaa” 最少能分为 1 个回文子 ...

  6. uva11584 Partitioning by Palindromes

    题目大意: 给出一个字符串,把他划分成尽量少的回文串,问最少的回文串个数 /* 先预处理所有回文子串 dp[i]表示字符1~i划分成的最小回文串的个数 */ #include<iostream& ...

  7. 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 ...

  8. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  9. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

随机推荐

  1. 4. explain简介

    一.是什么 使用 explain 关键字可以模拟优化器执行SQl查询语句,从而知道 mysql 是如何处理你的sql语句的.分析你的查询语句或是表的结构的性能瓶颈. 二.能干嘛 表的读取顺序 数据读取 ...

  2. Eclipse中SVN插件的安装和配置(在线安装)

    公司项目中用到了svn来管理项目,然后需要在Eclipse中进行配置.网上参考了很多资料,离线安装的方式装上了,但是导入项目后报错,可能是离线安装包的问题.然后又采用了Eclipse在线安装的方式,总 ...

  3. 【Tomcat】上线部署tomcat。常用命令

    ps -ef | grep tomcat-web [查询tomact进程]kill -9 pid [结束tomcat进程]/opt/tomcat-web/bin/startup.sh [启动tomca ...

  4. Netty中的HttpObjectAggregator

    Http的Get,POST Get请求包括两个部分: request line(包括method,request uri,protocol version)) header 基本样式: GET /?n ...

  5. Java8 使用stream实现各种list操作

    利用java8新特性,可以用简洁高效的代码来实现一些数据处理. 定义1个Apple对象: public class Apple { private Integer id; private String ...

  6. 捕获未处理的Promise错误

    译者按: 通过监听unhandledrejection事件,可以捕获未处理的Promise错误. 原文: Tracking unhandled rejected Promises 译者: Fundeb ...

  7. 字符串方法之padStart和padEnd

    ECMAScript 2017 有两个新的字符串方法:padStart和padEnd;  很有用啊啊,不用写if判断啦!开心脸 padStart在字符串开始出填充,padStart(num,‘要填充的 ...

  8. check约束

    -- 删除表 drop table check_test; -- 不为空,不为null的值只能是0,1(不为空,值只能是0,1) create table check_test( default_fl ...

  9. vue从入门到进阶:计算属性computed与侦听器watch(三)

    计算属性computed 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example" ...

  10. phpcms导航菜单的写法

    PHP打印方法: {php print_r(变量);} <?php print_r(变量);?> 1. <div class="webnav"> {pc:g ...