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题,要使旅行的城市最多,关键是要选出一个城市作为开始,以这个城市作为开始的城市时, ...
随机推荐
- 【微收藏】来自Twitter的自动文字补齐jQuery插件 - Typeahead.js
没图没逼格 事发有因 该插件可以结合本地数据进行一些操作.推荐关注一下H5的几种数据存储的方式(localstorage与sessionstorage.IndexedDB.离线缓存manifest文件 ...
- EF—主键冲突解决办法
报错信息: 编辑代码: 解决办法: 在Controller中不要把实体直接传过去,而要根据id先查出来,然后把查出来的实体传递过去就OK了
- myeclipse自动import
不管包什么的 直接把代码全写出来 再按 ctrl + shift +o 这是自动导包的 前提是你写的代码是正确的
- base-css
html{ min-width: 320px;}body{ min-width: 320px; overflow-x:hidden }@media print { * { background: tr ...
- qt添加最小化和关闭按钮
int width = this->width();//获取界面的宽度 //构建最小化.最大化.关闭按钮 QToolButton *minButton = new QToolButton(thi ...
- Rhino Mock
mock interfaces, delegates and classes, including those with parameterized constructors. set expecta ...
- Android如何让真机显示debug log的调试信息
真机默认是不开启debug log调试功能的,以前我一直用模拟器,模拟器默认是开启debug log调试功能的,那么如何让真机开启呢? 我用华为Ascend P6为例: 1.进入拨号界面,输入*#*# ...
- Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析
上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种 ...
- springMVC实现防止重复提交
参考文档:http://my.oschina.net/mushui/blog/143397
- codevs3031 最富有的人
题目描述 Description 在你的面前有n堆金子,你只能取走其中的两堆,且总价值为这两堆金子的xor值,你想成为最富有的人,你就要有所选择. 输入描述 Input Description 第一行 ...