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是切换,若现在是大写锁 ...
随机推荐
- Python的time,datetime,string相互转换
#把datetime转成字符串 def datetime_toString(dt): return dt.strftime("%Y-%m-%d-%H") #把字符串转成dateti ...
- oracle 时间段内的所有行时间
获取一段时间内所有的小时 SELECT to_date('2013-07-01 12', 'yyyy-mm-dd hh24') + (ROWNUM - 1) / 24 sdate FROM dualC ...
- VS05错误:部署WEB文件失败
直接生成一个空项目就不存在这个问题了.
- C运行时库
原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 在开发window程序是经常会遇到编译好好的程序拿到另一台机器上面无法运行的情况,这 ...
- InnoDB,select为啥会阻塞insert?
MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一. 但是,如<InnoDB,5项最佳实践>所述,如果查询没有命中索引,也将退化为表锁. InnoDB的细粒度锁,是实现在索引记录 ...
- 青花瓷运用->下载历史版本App
1.软件准备 [必备]Charles4.0.1 下载密码: jfnk [不需要,配合Charles食用效果更佳]Paw2.3.1 下载密码: t3my 2.正式开始 2.1 打开Charles青花瓷 ...
- [SP8372-TSUM]Triple Sums
题面在这里 description 某\(B\)姓\(OJ\)权限题 给出\(n\)个正整数\(a[i]\),求\(i<j<k\)且\(S=a[i]+a[j]+a[k]\)的三元组\((i ...
- [BZOJ5339] [TJOI2018]教科书般的亵渎
题目链接 BZOJ题面. 洛谷题面. Solution 随便推一推,可以发现瓶颈在求\(\sum_{i=1}^n i^k\),关于这个可以看看拉格朗日插值法. 复杂度\(O(Tm^2)\). #inc ...
- bzoj 1103: [POI2007]大都市meg (dfs序)
dfs序,加个bit维护前缀和就行了 type arr=record toward,next:longint; end; const maxn=; var edge:..maxn]of arr; bi ...
- BZOJ2111:[ZJOI2010]排列计数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2111 https://www.luogu.org/problemnew/show/P2606#su ...