HDU 2577 How to Type (字符串处理)
Problem Description
Pirates have finished developing the typing software. He called Cathy to test his typing software. She is good at thinking. After testing for several days, she finds that if she types a string by some 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
The first line is an integer t (t<=100), which is the number of test case in the input file. For each test case, there is only one string which consists of lowercase letter and upper case letter. The length of the string is at most 100.
Output
For each test case, you must output the smallest times of typing the key to finish typing this string.
Sample Input
3
Pirates
HDUacm
HDUACM
Sample Output
8
8
8
Hint
The 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
题意:
给定一串字母,问最少需要按多少次键盘将这些字母打印到屏幕上,键盘最开始的时候是小写锁定的,最后也要回归到小写锁定。
分析:
在小(大)写锁定下输入小(大)写字母肯定不需要进行考虑了,关键就是在小(大)写锁定下输入大(小)写字母,如果连续的只有一个的话,就用Shift键只需要多按一次,如果两个或者两个以上的话,就转换大小写锁定。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ch[109];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf(" %s",ch);
int k=strlen(ch);
int i=0;
int ans=0;
int flag=0;///Caps lock键有没有被按下,0表示小写锁定,1表示大写锁定
while(i<k)
{
if(flag==0&&ch[i]>='a'&&ch[i]<='z')///小写锁定的情况下是小写字母
{
i++;
ans++;
}
if(ch[i]>='A'&&ch[i]<='Z'&&flag==1)///大写锁定的情况下是大写字母
{
i++;
ans++;
}
if(flag==0&&ch[i]>='A'&&ch[i]<='Z')///小写锁定的情况下是大写字母
{
if(i+1<k)
{
if(ch[i+1]>='A'&&ch[i+1]<='Z')///下一个还是大写,就可以换成大写锁定
{
flag=1;
ans+=2;
}
else///否则直接用Shift键就行
{
ans+=2;
}
}
else///已经是最后一个字母了,肯定就是用shift键
ans+=2;
i++;
}
if(flag==1&&ch[i]>='a'&&ch[i]<='z')///大写锁定的情况下是小写字母
{
if(i+1<k)
{
if(ch[i+1]>='a'&&ch[i+1]<='z')///下一个还是小写,就可以换成小写锁定
{
flag=0;
ans+=2;
}
else///下一个字母大写的话,用shift键
{
ans+=2;
}
}
else///已经是左后一个字母了
{
ans+=2;
flag=0;///肯定就回归小写锁定了
}
i++;
}
}
if(flag==1)///最后害的看一下是不是大写锁定
ans+=1;
printf("%d\n",ans);
}
return 0;
}
HDU 2577 How to Type (字符串处理)的更多相关文章
- HDU 2577 How to Type (字符串处理)
题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...
- 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题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- HDU 2577 How to Type DP也可以模拟
http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...
- 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)
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
- HDU 2577 How to Type (DP,经典)
题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...
- DP问题(1) : hdu 2577
题目转自hdu 2577,题目传送门 题目大意: 现给你n个区分大小写的字符串,大小写可用Caps Lock和shift切换(学过计算机的都知道) 但是有一点需要注意(shift是切换,若现在是大写锁 ...
随机推荐
- lintcode-6-合并排序数组
合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果其中一个 ...
- 百度安卓sdk开发
一 key问题 1 在百度地图api控制台申请key的流程主要用到了app包,开发工具的开发sha1和发布sha1值,这2个值的获取就非常关键了. 一般来说我们都是在windows上开发安卓,使用an ...
- 提升MyEclipse运行速度
修改MyEclipse.ini文件中的,将-vmargs后面的参数修改为 -Xms256m -Xmx768m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:Max ...
- 自定义JS Map 函数
// 自定义JS Map 函数 function Map() { var map = function (key, value) {//键值对 this.key = key; this.value = ...
- delphi7中 OnDrawColumnCell 事件怎么用
你问的这个事件应该是dbgrid控件中的吧?这个事件是在grid控件载入数据的时候触发的,至于你这个“怎么用”波及的范围太大了,呵呵!不知道如何说起!另外还是发一段相关的代码吧,这也是我之前提过问题, ...
- InnoDB,select为啥会阻塞insert?
MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一. 但是,如<InnoDB,5项最佳实践>所述,如果查询没有命中索引,也将退化为表锁. InnoDB的细粒度锁,是实现在索引记录 ...
- SSE:服务器发送事件,使用长链接进行通讯 基础学习
HTML5中新加了EventSounce对象,实现即时推送功能,可以从下面连接中学习, http://www.kwstu.com/ArticleView/kwstu_20140829064746093 ...
- 【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- html的head内标签
ctrl+?:自动注释 ctrl+/: 注释多行,再按一次,取消注释的多行. 一,*********本地测试的方法:1-找到文件路径,直接浏览器打开:2-pycharm打开测试. 二,模板的解释: ...
- 【题解】51nod 1203JZPLCM问题
这题好强强啊,貌似是集训队原题?集训队原题当中值域是1e9的范围,这样各种乱搞是妥妥的不能过了,只能写正解的离线+树状数组维护前缀积. 最开始我写了几种乱搞做法,包括莫队和线段树做法.其中表现比较优秀 ...