HDU2577How to Type

本文探讨了一种算法,用于计算完成输入字符串所需的最小按键次数,包括考虑大小写转换及Caps Lock键的状态。通过对不同情况的细致分析,提供了一种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
据说是一道DP题,但不用DP,通过分析情况,也能计算答案,先选定第i个元素,然后判断i之后的元素大小写是否和i一样,直到遇到不一样的元素,然后做差,若是两个及以上就要用capsblocks,但是当最后一个元素是小写,则需要直接按capsblocks,当全部元素循环结束,要判断capsblocks是否是开启状态,若是,必须要关上,
(┬_┬)一开始忽略了小写也可以用shift来写=.=
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 110
char c[maxn];
int flag;
using namespace std;

//只记得大写可以用shift
//不要忽略小写也可以=.=

int capital(char c1)
{
    if(c1 >= 'A' && c1 <= 'Z')
        return 1;
    return 0;
}
int main()
{
    int t;
    while(~scanf("%d", &t))
    {
        int f = 1;
        while(t--)
        {
            if(f)
            {
                getchar();
                f = 0;
            }
            //memset(f1, 0, sizeof(f1));
            scanf("%s", c);
            int len = strlen(c);
            int num = len;//每个字母都要按一下
            int t;
            flag = 0;
            int i, j;
            for(i = 0; i < len; i++)
            {
                t = capital(c[i]);
                for(j = i + 1; j < len; j++)
                {
                    if(t != capital(c[j]))
                        break;
                        //判断同为大写或小写的长度
                }
                //元素是大写,并且没有用capslock,只有i元素是大写=.=所以要按下shift;
                if(t && !flag && j - i < 2)
                {
                    num += 1;
                }
                //元素是大写,i后面含有至少一个是大写,所以锁定capslock
                else if(t && !flag && j - i >= 2)
                {
                    num += 1;
                    flag = 1;
                }
                //元素是小写,但是现在锁定着capsblock,且元素个数为1,所以要按下shift;
                //但假如i是最后一个元素,那么按下的就因该是capsblocks
                else if(!t && flag && j - i < 2 && i == len - 1)
                {
                    num += 1;
                    flag = 0;
                }
                else if(!t && flag && j - i < 2)
                {
                    num += 1;
                }
                //元素是小写,但锁定着capsblock,且元素个数大于等于2,那么再按下capsblock
                else if(!t && flag && j - i >= 2)
                {
                    num += 1;
                    flag = 0;
                }
                //在第j的元素时,与前面元素大小写不一样,但是要i++,所以i = j - 1;
                i = j - 1;
            }
            if(flag) num += 1;
            printf("%d\n", num);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值