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是切换,若现在是大写锁 ...
 
随机推荐
- TCP系列02—连接管理—1、三次握手与四次挥手
			
一.TCP连接管理概述 正如我们在之前所说TCP是一个面向连接的通信协议,因此在进行数据传输前一般需要先建立连接(TFO除外),因此我们首先来介绍TCP的连接管理. 通常一次完整的TCP数据传输一般包 ...
 - pycharm开启代码智能提示和报错提示
			
天呐,经历了一大波周折,终于把提示给弄好了,加入没有提示的话,pycharm就是一个空格了,没有什么作用,对我来说,真的是很困难的事情,所以无论如何都要去把这个智能提示给搞好了. 先讲讲我的经历吧.我 ...
 - Zookeeper实现分布式集群监控
			
Zookeeepr实现分布式集群监控 Zookeeper中节点有两种:临时节点和永久节点 从类型上看节点又可以分为四种节点类型:PERSIST,PERSIST_SEQUENTIAL,EPHEMERAL ...
 - Delphi中正常窗口的实现
			
摘要: 在Delphi的VCL库中,为了使用以及实现的方便,应用对象Application创建了一个用来处理消息响应的隐藏窗口.而正是这个窗口,使得用VCL开发出来的程序存在着与其他窗口不能正常排列平 ...
 - HSF原理
			
HSF(High-speed Service Framework),高速服务框架,是阿里系主要采用的服务框架,其目的是作为桥梁联通不同的业务系统,解耦系统之间的实现依赖.其高速体现在底层的非阻塞I/O ...
 - asp.net MVC 导出查询结果到Excel
			
首先在View视图中有一表单form,导出按钮<input class="btn export" type="button" value="导出 ...
 - ::before和::after 常见的用法
			
.lizi:after{ content: "I'M after"; /*插入字符串*/ content: "attr(id)"; /*插入当前元素属性*/ ...
 - 【EF】EF实现大批量数据库插入操作
			
通过参考http://www.itnose.net/news/171/6306259里面介绍的方法,我们可以在程序的解决方案的引用中选择NuGet管理包中添加 该扩展方法提供BulkInsert() ...
 - Xcode开发技巧之code snippets(代码片段)
			
一.什么是代码片段 当在Xcode中输入dowhile并回车后,Xcode会出现下图所示的提示代码: 这就是代码片段,目的是使程序员以最快的速度输入常用的代码片段,提高编程效率.该功能是从Xcode4 ...
 - BZOJ5322:[JXOI2018]排序问题——题解
			
https://www.lydsy.com/JudgeOnline/problem.php?id=5322 https://loj.ac/problem/2543 <-可以看数据,要没有这数据我 ...