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
",c);
    }
}
原文地址:https://www.cnblogs.com/cmmdc/p/6766763.html