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. springMVC_03注解完成hello案例

    1.导入jar包 commons-logging-1.1.1.jar jackson-annotations-2.5.4.jar jackson-core-2.5.4.jar jackson-data ...

  2. (6)Jquery1.8.3快速入门_过滤选择器

    一.Jquery的基本过滤选择器: 基本过滤选择器: 1. :first 选取第一个元素 2. :last 选取最后一个元素 3.:not(selector) 去除所有与给定的选择器匹配的元素 4.: ...

  3. Netty实战七之EventLoop和线程模型

    简单地说,线程模型指定了操作系统.编程语言.框架或者应用程序的上下文中的线程管理的关键方面.Netty的线程模型强大但又易用,并且和Netty的一贯宗旨一样,旨在简化你的应用程序代码,同时最大限度地提 ...

  4. JavaWeb学习日记----XML的解析

    XML的解析简介: 在学习JavaScript时,我们用的DOM来解析HEML文档,根据HTML的层级结构在内存中分配一个树形结构,把HTML的标签啊,属性啊和文本之类的都封装成对象. 比如:docu ...

  5. ssh多台主机实现互相认证

    一.主机情况 如下图所示,集群一共11台机器.编辑每台主机的hosts文件,添加如下内容,方便统一管理. 10.202.62.60 hadoop60 10.202.62.61 hadoop61 10. ...

  6. 支持MPI的hdf5库的编译

    作者:朱金灿 来源:http://blog.csdn.net/clever101 因为最近要研究并行I/O,据说hdf5文件格式可以支持并行I/O,深度学习框架Caffe用的是hdf格式,所以决定把h ...

  7. Tars 负载均衡

    // 传入主控地址,在 db_tars t_registry_info 表中 Communicator communicator = CommunicatorFactory.getInstance() ...

  8. python爬虫学习记录——各种软件/库的安装

    Ubuntu18.04安装python3-pip 1.apt-get update更新源 2,ubuntu18.04默认安装了python3,但是pip没有安装,安装命令:apt install py ...

  9. c++屏蔽Win10系统快捷键

    很久之前实现的功能,也是参考其他人的实现,时间太久,具体参考哪里已经记不得了. 这里不仅能屏蔽一般的快捷键,还可以屏蔽ctrl+atl+del. ; HHOOK keyHook = NULL; HHO ...

  10. 智能POS删除文件和数据库操作步骤

    1. 2. 3. 4.winbox:日志:winboxcash:数据库:winboxcyb:其他文件: 5.删除以上三个文件夹