HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577
解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符串用键盘输入到电脑中,一开始的时候大写锁定是关闭的,并且要求结束的时候也是关闭的,然后让你求输入这些字符串最少需要按多少次键盘(包括Cap Lock键和Shift键)
一个典型的dp题,定义一个一维数组就够了,然后dp[i]的含义的输入到第i个字符时需要按键的最少次数。然后递推公式如下:
dp[i] = min(dp[i],dp[j-1] + judge1(j,i));
dp[i] = min(dp[i],dp[j-1] + judge2(j,i));
两种输入方式分别是从第j个字符到第i个字符打开大写锁定来输入,另一种方式是关闭大写锁定来输入第j到i的字符。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn = ;
char str[maxn];
int dp[maxn];
int judge1(int x,int y)
{
int sum = ;
for(int i = x;i <= y;++i)
if(str[i] >= 'A' && str[i] <= 'Z')
sum++;
else if(str[i] >= 'a' && str[i] <= 'z')
sum += ;
return sum;
}
int judge2(int x,int y)
{
int sum = ;
for(int i = x;i <= y;++i)
if(str[i] >= 'A' && str[i] <= 'Z')
sum += ;
else if(str[i] >= 'a' && str[i] <= 'z')
sum++;
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str+);
int len = strlen(str+);
memset(dp,0x3f,sizeof(dp));
dp[] = ; //使0号为0,后面要-1,不用做特殊处理
for(int i = ;i <= len;++i)
for(int j = ;j <= i;++j)
{
dp[i] = min(dp[i],dp[j-] + judge1(j,i));
dp[i] = min(dp[i],dp[j-] + judge2(j,i));
}
printf("%d\n",dp[len]);
}
return ;
}
HDU 2577 How to Type(dp题)的更多相关文章
- HDU 2577 How to Type DP也可以模拟
http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...
- hdu 2577 How to Type(dp)
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
- HDU 2577 How to Type (DP,经典)
题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...
- HDU 2577 How to Type (线性dp)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 2577 How to Type(DP)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 2577 How to Type【DP】
题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就 ...
- HDU 2577 How to Type (字符串处理)
题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...
- HDU 2577 How to Type (字符串处理)
题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...
- HDU 1422 重温世界杯 DP题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1422 解题报告:DP题,要使旅行的城市最多,关键是要选出一个城市作为开始,以这个城市作为开始的城市时, ...
随机推荐
- unity3d 依赖关系获取预制件任意资源
前段时间策划们想知道UI预制件中使用了哪些音效 N多预制件.N多音效!! 如果纯人工整理的话这还不累成狗? 累成狗不说,还容易出错 所以获取音频剪辑小工具就诞生了,将策划从死亡边缘拉了回来 我们先看一 ...
- Web API 基于ASP.NET Identity的Basic Authentication
今天给大家分享在Web API下,如何利用ASP.NET Identity实现基本认证(Basic Authentication),在博客园子搜索了一圈Web API的基本认证,基本都是做的Forms ...
- 第六章 prototype和constructor
首先我们看下面一段代码(第六章 01.htm) function myfun() //定义一个函数myfun { }; console.log(typeof (myfun.prototype)); c ...
- webstorm调试Node的时候配置
点击Edit Configurations的这个的配置:(不能点击是因为目前你选中的不是项目)
- LINQ找出重复和不重复的元素及linq OrderBy 方法 两个字段同时排序有关问题
//重复元素:3,4,5 //不重复元素:1,8,9 , , , , , , , , , , }; //不重复元素 var unique = arr.GroupBy(i => i) .Where ...
- [BZOJ 3038]上帝造题的7分钟2(树状数组)
分析:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3038 这题看起来没办法做……但是注意到1e12只要开方8次就能到1……所以直接暴力就行 ...
- Bootstrap3.0学习第二十轮(JavaScript插件——滚动监听)
详情请查看 http://aehyok.com/Blog/Detail/26.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:h ...
- JSP页面的中文乱码
jsp页面显示中文乱码: jsp页面的编码方式有两个地方需要设置: <%@ page language="java" import="java.util. ...
- WebService学习过程中的心得和问题
1.发布一个WebService 2.调用第三方提供的WebService服务
- Qt模型/视图框架----简单的例子
#include<qapplication.h> #include<qfilesystemmodel.h> #include<qtreeview.h> #inclu ...