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

<div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>
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
</div>
题意:给定一行字符串求敲出该串的最小步数。大小写切换及小写字母变大写字母符合正常规则。要求敲完字符串后大写字母指示灯必须关着
解题思路:定义一个两个二维数组变量f[i][0],f[i][1]。f[i][0]表示大写的指示灯为关着状态。f[i][1]表示大写指示灯为开着状态。对每个字母求两种状态步数最小即可
#include<stdio.h>
#include<string.h>
int p[200][2];
int mi(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int  n,i,j,m;
    char lmx[105];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        scanf("%s",lmx);
        m=strlen(lmx);
        memset(p,0,sizeof(p));
        p[0][1]=1;
        for(i=0;i<strlen(lmx);i++)
        {
            j=i+1;
            if(lmx[i]>='a'&&lmx[i]<='z')//当前字母为小写时
            {
                p[j][0]=mi(p[j-1][0]+1,p[j-1][1]+2);
                p[j][1]=mi(p[j-1][0]+2,p[j-1][1]+2);
            }
            else//当前字母为大写时
            {
                p[j][0]=mi(p[j-1][0]+2,p[j-1][1]+2);
                p[j][1]=mi(p[j-1][1]+1,p[j-1][0]+2);
            }
        }
        p[m][1]++;//关掉大写指示灯
        int temp=p[m][0]<p[m][1]?p[m][0]:p[m][1];
        printf("%d\n",temp);
    }
    return 0;
}
posted @ 2013-05-28 17:56  forevermemory  阅读(347)  评论(0编辑  收藏  举报