zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    EOF ---shell编程
    Linux错误代码含义
    linux连接sybase数据库-isql
    CH7-WEB开发(集成在一起)
    [转]ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)
    [转]ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)
    [转]ASP.NET MVC 入门8、ModelState与数据验证
    [转]ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    [转]ASP.NET MVC 入门6、TempData
    [转]ASP.NET MVC 入门5、View与ViewData
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6766763.html
Copyright © 2011-2022 走看看