HDU 2577 How to Type

Problem 2577 : How to Type

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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

分析

题意:给一个由英文字母组成的字符串,计算出用键盘敲出这个字符串的最少按键次数,初始的状态Caps Lock键是关闭状态,是小写状态,要求最后输入完之后也需要把Caps Lock键置于关闭状态
这道题是一道动态规划,但可以用模拟方法简单做出;首先很容易发现,如果第X+i个字母(i=1,2,3,……;i < a;a>1)都与第X个字母的大小写不同,而到第(X+a)个字母与第X个字母的大小写相同:
当a=2时,用Shift键需要更少的按键操作次数
当a=3时,用Shift键或者Caps Lock键都需要4次按键操作
当a>3时,用Caps Lock键需要更少的按键操作次数
因此可以只判断a的大小来选择最少次数的操作(简单的说只需判断是否连续两个都是相同的大小写,相同时判断Caps Lock键的状态,不同时直接用Shift键)
但存在一种特殊情况:如果最后一个字母均为小写,而倒数第二个为大写时,且在倒数第二个字母是Caps Lock键为开启状态是,则最后一个字母的输入可以Caps Lock键+该字母,也可以Shift键+该字母,次数是相同的,但要求最后输入完之后也需要把Caps Lock键置于关闭状态,所以用第二种方法可以少一次操作次数

代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int time=0,e=1;///time用来记录按键次数,e用来存放Caps Lock键是否为关闭状态
        char a[110];
        scanf("%s",a);
        for(int i=0;i<strlen(a);i++)
        {
            if(a[i]>='A'&&a[i]<='Z')///第i个字母为大写
            {
                if(a[i+1]>='A'&&a[i+1]<='Z')///第i+1个字母仍为大写
                {
                    if(e==1){time++;e=0;}///Caps Lock键为关闭状态,按一次Caps Lock,将其变为开启
                }
                else if(e==1)time++;///第i+1个字母为小写,Caps Lock键为关闭状态,按一次Shift
                time++;///按下第i个字母
            }
            if(a[i]>='a'&&a[i]<='z')///第i个字母为小写
            {
                if(a[i+1]>='a'&&a[i+1]<='z'||i==strlen(a)-1)///第i+1个字母仍为小写且第i个字母不是最后一个
                {
                    if(e==0){time++;e=1;}///Caps Lock键为开启状态,按一次Caps Lock,将其变为关闭
                }
                else if(e==0)time++;///Caps Lock键为开启状态,1.第i+1个字母为大写,按一次Shift;2.第i个字母是最后一个,按一次Caps Lock
                time++;///按下第i个字母
            }
        }
        if(e==0)time++;
        printf("%d\n",time);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值