hdoj 2577 How to Type 【坑】

来源:互联网 发布:手机写歌词软件 编辑:程序博客网 时间:2024/06/03 13:53


How to Type

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4653    Accepted Submission(s): 2106


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
3PiratesHDUacmHDUACM
 

Sample Output
888
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 8The string "HDUACM", can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8
 

题意:用最少敲键盘的次数 来敲出给出的字符串

注意若用一个判断字符函数直接写的话,可能会出错。 用大写字母函数判断可以直接忽略字符串末尾的结束字符,若用小写字母函数判断不能忽略否则会WA。

干脆我直接设两个函数来写。

思路:从头开始枚举,用一个变量mark来标记是否按过cap键,按过mark = 1, 否则为0。

这里我用ans 表示结果。

一:碰到小写字母

1,若mark = 0,直接ans += 1;

2,若mark = 1,这时对于后一位不同的字符有不同的处理:(1) 若后面是大写字母,则小写字母只需shift键处理 ans += 2即可 。(2) 若后面是小写字母,需要清除cap键,则有
ans += 2, mark = 0  。 (3)若后面是'\0'即字符串结尾,那么最优做法就是先清除cap,再输入小写字母 则有ans += 2, mark = 0。

二:碰到大写字母

1,若mark = 1, 直接ans += 1;
2,若mark = 0,同上:(1)若后面是小写字母,则大写字母只需shift键处理 ans += 2 即可 。(2) 若后面是大写字母,只需按下cap键,则有ans += 2, mark = 1。 (3) 若后面是字符串结尾,那么最优做法就是直接shift键处理,则有ans += 2。


最后还要清除cap键。


AC代码:


#include <cstdio>#include <cstring>bool xiaoxie(char x){    return x >= 'a' && x <= 'z'; } bool daxie(char x){    return x >= 'A' && x <= 'Z';} int main(){    int t;    char str[110];    int ans;    int i, j;    scanf("%d", &t);    while(t--)    {        scanf("%s", str);        int l  = strlen(str);        ans = 0;        int mark = 0;//初始没有按过mark         for(i = 0; i < l; i++)        {            if(xiaoxie(str[i]))//小写字母             {                if(!mark)//没有按cap                 ans += 1;                else                {                    if(daxie(str[i+1]))//按过cap 但后面是大写                      ans += 2;//shift 处理                     else if(xiaoxie(str[i+1]))//后面是小写                     {                        mark = 0;//按回cap                        ans += 2;                     }                     else //字符串结尾                    {                    mark = 0;//最优做法 是直接清除cap 再输入小写字母                     ans += 2;                    }                }            }             else if(daxie(str[i]))//大写字母             {                if(mark)//按过cap                ans += 1;                  else//没按过cap                 {                    if(xiaoxie(str[i+1]))//下一项是小写                    ans += 2;//shift 处理                     else if(daxie(str[i+1]))//下一项是大写                     {                        mark = 1;//按cap                         ans += 2;                    }                     else //字符串结尾ans += 2; //shift处理                 }            }         }        if(mark) ans += 1;         printf("%d\n", ans);    }    return 0;} 


0 0
原创粉丝点击