HDU-2577 How To Type 解题报告

HDU - 2577

题目描述:

How To Type

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. No 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


题目大意

题目要计算输入一行只由大小写字母组成的字符串输入所需要的最短时间,其中值得注意的是
1. 若输入完成后大写锁定还开启,就要将其关闭
2. 若单个大写或小写字母出现,无需开启大写锁定
3. 在大写锁定开启时,输入小写字母不一定要关闭大写锁定
4. 在讨论最后一个字符时,要将讨论“下下个字符”的过程改为“判断是否为’\0’”的过程,如果没有这个步骤,你可能在类似HDUacM的例子上出错,因为M不需要开启大写锁定

解题方法

这是一道DP的题目,状态可以看作是输入字符串前 i (0 <= i <= n)个字符所需的最短时间。因此开一个数组来表示DP的过程,用个一个bool元素表示大写锁定是否开启。
· 边界条件:i == 0时,time[0] = 0。
· 转移方程:分如下情形:
1、大写锁定开启,下个字母是大写
2、大写锁定开启,下个字母是小写,下下个字母是大写
3、大写锁定开启,下个字母是小写,下下个字母是小写
4、大写锁定没开,下个字母是大写,下下个字母是大写
5、大写锁定没开,下个字母是大写,下下个字母是小写
6、大写锁定没开,下个字母是小写
在最后,判断大写锁定是否开启,若开启,则时间加一,关闭大写锁定

代码如下

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = 105;
int N;
char str[MAX];
int MinTime[MAX];
int main()
{
    cin >> N;
    MinTime[0] = 0;
    for(int i = 0; i < N; i ++)
    {
        cin >> str;
        unsigned long len = strlen(str);
        bool capslockOpen = false;
        for(int i = 1; i <= len; i ++)
        {
            if(capslockOpen == true)
            {
                if(str[i - 1] <= 'Z' && str[i - 1] >= 'A')
                    MinTime[i] = MinTime[i - 1] + 1;
                else
                {
                    if((str[i] <= 'z' && str[i] >= 'a') || str[i] == '\0')
                        capslockOpen = false;
                    MinTime[i] = MinTime[i - 1] + 2;
                }
            }
            else
            {
                if(str[i - 1] <= 'Z' && str[i - 1] >= 'A')
                {
                    if(str[i] <= 'Z' && str[i] >= 'A')
                        capslockOpen = true;
                    MinTime[i] = MinTime[i - 1] + 2;
                }
                else
                    MinTime[i] = MinTime[i - 1] + 1;
            }
        }
        if(capslockOpen == true)
            MinTime[len] += 1;
        cout << MinTime[len] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41480330/article/details/81669718