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
分析:
多注意一些细节问题。
代码:
#include <stdio.h>
#include <string.h>
int isA(char c)
{
return c>='A'&&c<='Z';
}
int main()
{
int n,len,c;
int i,j;
char s[105];
int caplock;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
c=len=strlen(s);
caplock=0;
for(i=0; i<len; i++)
{
int t=isA(s[i]);
for(j=i+1; j<len; j++)
{
if(t!=isA(s[j]))break;
}
if(t==1&&caplock==0&&j-i<2)//只有一个大写字母,Cap键没有按下
c++;//按shift键
else if(t==1&&caplock==0&&j-i>=2)//有多于1个大写字母,Cap键没有按下
c++,caplock=1;//按下Cap键
else if(t==0&&caplock==1&&j-i<2&&j==len)//结尾有一个小写字母,Cap已经按下
c++,caplock=0;//按下Cap键
else if(t==0&&caplock==1&&j-i<2)//Cap已经按下,出现一个小写字母
c++;//按shift键
else if(t==0&&caplock==1&&j-i>=2)//Cap已经按下,出现多个小写字母
c++,caplock=0;//按下Cap键
i=j-1;
}
if(caplock)c++;
printf("%d\n",c);
}
}
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是切换,若现在是大写锁 ...
随机推荐
- TCP系列03—连接管理—2、TCP连接的同时打开和同时关闭
在前面的内容中我们介绍了TCP连接管理中最常见的三次握手方式和四次挥手的方式.但是有可能A和B两端同时执行主动打开并连接对方或者同时执行主动关闭连接(尽管发生这种情况的可能性比较低低),这个时候的流程 ...
- Scala快速入门-基础
HelloWorld 从HelloWorld开始,使用scala IDE编辑器. 新建scala project 新建scala object 编写HelloWorld run as scala ap ...
- 3ds Max学习日记(四)
下午去实验室见了师姐,人还挺好,给我安排了任务,和3ds max没有半毛钱关系. 附上今日的劳动成果: 板子(牌匾) 简约吊灯(看上去比较单调) 高脚杯(喝酒用的) 沙发(沙发) ...
- PAT L2-028 秀恩爱分得快
https://pintia.cn/problem-sets/994805046380707840/problems/994805054698012672 古人云:秀恩爱,分得快. 互联网上每天都有大 ...
- IDEA编译的JAR包运行出现“没有主清单属性”
运行编译好的包出现: 解决方案就是: 确保MANIFEST.MF文件在src/main/resources/META_INF/而不是src/main/java/META_INF/
- ipython matplotlib
matplotlib实际上是一套面向对象的绘图库,它所绘制的图表中的每个绘图元素,例如线条Line2D.文字Text.刻度等在内存中都有一个对象与之对应.为了方便快速绘图matplotlib通过pyp ...
- centos7 nginx端口转发出现502的其中一种原因
在排查了一系列可能的原因后仍无法解决,经资料查阅可能是SELinux造成,SELinux很强大但若配置不当也会造成很多组件无法正常使用,这里直接将其关闭: //打开配置文件 vi /etc/selin ...
- BZOJ 1010 玩具装箱(斜率优化DP)
dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...
- BZOJ4565 HAOI2016字符合并(区间dp+状压dp)
设f[i][j][k]为将i~j的字符最终合并成k的答案.转移时只考虑最后一个字符是由哪段后缀合成的.如果最后合成为一个字符特殊转移一下. 复杂度看起来是O(n32k),实际常数极小达到O(玄学). ...
- BZOJ3195:[JXOI2012]奇怪的道路——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3195 Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方 ...