NYOJ练习题 Splits the string (简单动态规划)
Splits the string
- 描述
-
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A partition of a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concatenation yields the initial sequence. For example, ('race', 'car') is a partition of 'racecar' into two groups.Given a sequence of characters, we can always create a partition of these characters such that each group in the partition is a palindrome! Given this observation it is natural to ask: what is the minimum number of groups needed for a given string such that every group is a palindrome?For example:'racecar' is already a palindrome, therefore it can be partitioned into one group.'fastcar' does not contain any non-trivial palindromes, so it must be partitioned as ('f', 'a', 's', 't', 'c', 'a', 'r').'aaadbccb' can be partitioned as ('aaa', 'd', 'bccb').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.
- 输入
- Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.
- 输出
- For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.
- 样例输入
-
racecar fastcar aaadbccb
- 样例输出
-
1 7 3
题意:给定一个字符串,求最少需要几个回文子串组成此字符串。
解题思路:用dp[i] 记录从0到当前i位置这一段最少由几个回文子串组成,动态转移方程:dp[i] = min(dp[i],dp[j-1]+1);
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char str[1010]; int dp[1010]; bool judge(int x,int y) //判断是不是回文串 {while(x <= y){if(str[x] != str[y])return false;x++;y--;}return true; } int main() {int len, i, j;while(gets(str) != NULL){len = strlen(str);for(i = 0; i < len; i++){dp[i] = i + 1; //假设前面的都不能组成回文串for(j = 0; j <= i; j++)if(str[j] == str[i] && judge(j,i))dp[i] = min(dp[i], dp[j-1]+1);}printf("%d\n",dp[len-1]);}return 0; }
NYOJ练习题 Splits the string (简单动态规划)相关推荐
- 数据结构练习题――Hero In Maze 简单版
数据结构练习题――Hero In Maze 简单版 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交:306 ...
- String简单介绍
Java–String简单介绍 一.String是什么? String 是Java中的字符串类,属于引用类型,底层是char数组. 字符串 是有一个或多个字符组成,用" "双引号包 ...
- 关于简单动态规划(Dynamic Programming)的总结
Instructions 综上所述(好像没有上)我的DP真的垃圾的一批... 动态规划是用来避免重复计算状态导致效率低的情况,实现动规有记忆化搜索和填表两种方法,但记忆化搜索不能优化空间,所以常用的是 ...
- 51nod 1270 数组的最大代价 思路:简单动态规划
这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...
- 字符串上的简单动态规划
因为数据结构快学串了,以前又做过一些字符串dp的题,今天突然就想把它们写在一起吧. 直接开始 问题1:给两个字符串,求最长公共子串 问题2:给两个字符串,求最长公共子序列 问题3:给一个字符串,求最长 ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alpha ...
- C简单动态规划——爬数塔
问题描述: 小明在某个角落发现了一座由数字组成的斜塔,他I想到塔顶去看看.小明可以从底层任意一个数字出发逐层爬上去,每次可以爬至上一层数字上或者上一层左边相邻的数字上(第1列只能爬至正上方上一层的数字 ...
- 338. Counting Bits_比特位计数_简单动态规划
https://leetcode.com/problems/counting-bits/ 这是初步了解动态规划后做的第一道题,体验还不错... 看完题目要求后,写出前10个数的二进制数,发现了以下规律 ...
- NYOJ练习题 下三角矩形 (模拟)
下三角矩阵 时间限制:1000 ms | 内存限制:65535 KB 描述 给定一个由0和1组成的矩阵.只允许交换相邻的两行,要把矩阵转化成下三角矩阵(主对角线上方的元素都是0),最少需要交换几次 ...
最新文章
- 一起学spring--我的第一个Spring程序,简单粗暴易懂
- 【深度学习的数学】用神经网络进行图像分类时,为什么输出层的神经单元数量要跟分类数相同?可以采用二进制的表示方式么?
- 软件项目管理0824:标书中疫情条款
- “Imperceptible,Robust,and Targeted Adversaria lExamples for Automatic Speech Recognition”
- (转载)FPGA基础知识------PS/2基础知识
- 安卓分辨率_免费的安卓群控1数字云免费安卓群控系统
- 漫画:混乱的标记语言XHTML2/HTML5
- linux如何安装交叉工具链,Linux:制作mipsel 交叉工具链 (更新版)
- Facebook 洗白?欲打造以隐私为中心的社交平台!
- 支付宝返回参数说明(转)
- C++学习记录一——VS2019报错“C2011 ”tagPOINT“:“struct“类型重定义
- WIFI无线协议802.11a/b/g/n/ac的演变以及区别
- 【编解码】从零开始写H264解码器(2) NALU
- html怎么填充单元格颜色,PPT表格单元格怎么填充颜色 PPT填充表格单元格颜色的详细教程...
- CATIA二次开发—参数那点事
- 猪猪猫Windows7 X86旗舰中文装机版V0911
- CentOS 7:设置静态/动态ip地址
- 大数字加法(C语言实现)
- vscode配置python环境以及使用json文件配置默认解释器、代码自动保存、pydesigner、kite
- 如何给135编辑器推文添加附件