天天看點

hdoj 2577 How to Type 【坑】How to Type



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

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

   
    
        

題意:用最少敲鍵盤的次數 來敲出給出的字元串

注意若用一個判斷字元函數直接寫的話,可能會出錯。 用大寫字母函數判斷可以直接忽略字元串末尾的結束字元,若用小寫字母函數判斷不能忽略否則會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;
}