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是切换,若现在是大写锁 ...
随机推荐
- 访问需要HTTP Basic Authentication认证的资源的各种开发语言的实现
什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧. 在你访问一个需要H ...
- Node js路由
/* 要为程序提供请求的 URL 和其他需要的 GET 及 POST 参数,随后程序需要根据这些数据来执行相应的代码. 因此,需要查看 HTTP 请求,从中提取出请求的 URL 以及 GET/POST ...
- php 生成短网址 代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- windows下apache+php安装
1.安装apache 通过exe安装,如果80端口被占用,修改httpd.conf中的Listen,然后再次用exe安装,选择repaire 2.安装php 解压php包,添加系统变量 path,加上 ...
- 【bzoj5008】方师傅的房子 计算几何
题目描述 给出一个凸多边形,多次询问某个点是否在这个凸多边形的内部,强制在线. 输入 第一行一个数n,接下来n行,每行两个整数x,y.输入按照逆时针顺序输入一个凸包. 接下来一个数m,最后有m行, ...
- Django 2.0 学习(11):Django setuptools
应用打包 当前状态的Python包与各种工具有点儿混乱,本结我们将学习使用setuptools来构建应用包.该工具是强烈推荐使用的打包工具,之后我们也会使用pip去安装和卸载它. Python打包指的 ...
- SVG总结小知识
SVG:可缩放矢量图形.全称是:Scalable Vector Graphics SVG使用 XML 格式定义图像. SVG是使用 XML 来描述(二维图形和绘图)程序的语言. SVG是W3C ...
- Python 源码剖析(四)【LIST对象】
四.LIST对象 1.PyListObject对象 2.PyListObject的创建与维护 3.PyListObject 对象缓冲池 4.Hack PyListObject 1.PyListObje ...
- Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)
这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: · 向里面加一个括号,可以在开头,在结尾,在两个括号之间加. · 对当前括号序列进 ...
- [六省联考2017]分手是祝愿 期望DP
表示每次看见期望的题就很懵逼... 但是这题感觉还是值得一做,有可借鉴之处 要是下面这段文字格式不一样的话(虽然好像的确不一样,我也不知道为什么,是直接从代码里面复制出来的,因为我一般都是习惯在代码里 ...