HDU 2577 How to Type (线性dp)
How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4616 Accepted Submission(s): 2084
Problem Description
ways, she will type the key at least. But she has a bad habit that if the caps lock is on, she must turn off it, after she finishes typing. Now she wants to know the smallest times of typing the key to finish typing a string.
Input
of the string is at most 100.
Output
Sample Input
3
Pirates
HDUacm
HDUACM
Sample Output
8
8
8HintThe string “Pirates”, can type this way, Shift, p, i, r, a, t, e, s, the answer is 8.
The string “HDUacm”, can type this way, Caps lock, h, d, u, Caps lock, a, c, m, the answer is 8
The string "HDUACM", can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8
Author
Source
题目链接:
pid=2577">http://acm.hdu.edu.cn/showproblem.php?pid=2577
题目大意:坑比题,给一些有大写和小写组成的字符串,问用键盘打出它们的最少按键次数
按键规则:对于每个小写字母。加上shift可使其变成大写。注意shift不能够一直按着不放,还能够开大写锁Caps lock键,按完这键输入就变成大写,此时再按shift。则打出小写(mac的键盘貌似不是这样),注意每次大写锁,最后必须把它关上
题目分析:知道题以后就非常easy了dp[i][0]和dp[i][1]分别表示到第i个字符大写锁关,开着时要按的最少次数,则有4种情况
假设到第i个字符为小写且此时开着大写锁,则它前一个必定是大写,由于假设前一个是小写,当前也是小写。我显然不须要开大写锁
dp[i][1] = dp[i - 1][1] + 2
假设到第i个字符为小写且此时没开大写锁,直接打即可了
dp[i][0] = dp[i - 1][0] + 1
假设到第i个字符为大写且此时开着大写锁。则为前一个开着大写锁的情况加1和前一个没开大写锁的情况加2的最小值,表示开锁
dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][0] + 2)
假设到第i个字符为大写且此时没开大写锁。则为前一个没开大写锁的情况加2(按shift)和前一个开着大写锁的情况加2的最小值。表示关锁
dp[i][0] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 2)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 105;
char s[MAX];
int dp[MAX][2]; bool judge(char ch)
{
if(ch >= 'A' && ch <= 'Z')
return true;
return false;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s + 1);
int len = strlen(s + 1);
memset(dp,0, sizeof(dp));
dp[0][1] = 1;
for(int i = 1; i <= len; i++)
{
if(judge(s[i]))
{
dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][0] + 2);
dp[i][0] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 2);
}
else
{
dp[i][1] = dp[i - 1][1] + 2;
dp[i][0] = dp[i - 1][0] + 1;
}
}
printf("%d\n", min(dp[len][0], dp[len][1] + 1));
}
}
HDU 2577 How to Type (线性dp)的更多相关文章
- HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- 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 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 分情况多维DP
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2709 Sumsets 经典简单线性dp
Sumsets Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 3455 Leap Frog(线性DP)
Problem Description Jack and Jill play a game called "Leap Frog" in which they alternate t ...
随机推荐
- file---探测给定文件的类型
file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程. 语法 file(选项)(参数) 选项 -b:列出辨识结果时,不显示文件名称: -c:详细显示 ...
- python code 1_username registration & login
This tiny program consists of 2 parts - registration and login. In the part of registration, the key ...
- 紫书 习题 10-21 UVa 1649 (组合数)
C(n, k) = m, 固定k,枚举k 这里用到了组合数的一个性质 当k固定的时候,C(2 * k, k) 最小 C(m, k)最大(对于这道题而言是这样,因为大于m 就最终答案不可能为m了) 所以 ...
- Linux中 ps aux 命令
$ ps aux USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND root 11 100.0 0.0 0 16 ?? RL 4Dec09 ...
- androidclient和站点数据交互的实现(基于Http协议获取数据方法)
androidclient一般不直接訪问站点数据库,而是像浏览器一样发送get或者post请求.然后站点返回client能理解的数据格式,client解析这些数据.显示在界面上.经常使用的数据格式是x ...
- 空间矢量数据(.shp文件)之JAVA操作
Shape文件由ESRI开发.一个ESRI(Environmental Systems Research Institute)的shape文件包含一个主文件,一个索引文件,和一个dBASE表. 当中主 ...
- WIN10 10招
还有不到两个月的时间,7 月 29 日 Windows 10 就将正式公布,在此之前已经有不少的用户已经使用上了 Windows 10 的预览版.对于那些苦等 Windows 10 的用户来说,幸福非 ...
- POJ 2378 树形DP??
思路: 找到每个节点的儿子size的最大值 和n-当前节点size 取个Max //By SiriusRen #include <cstdio> #include <cstring& ...
- echo---打印变量或输出字符串
cho命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下e ...
- Python datetime time 等时间 日期 之间的计算和相互转化
from datetime import datetime, date, timedelta, timezone from time import time, ctime, localtime, st ...