hdu 简单DP 1009 How to Type

How to Type

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 36   Accepted Submission(s) : 19

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

Author

Dellenge

Source

HDU 2009-5 Programming Contest


#include 
using namespace std;
int open[1005],close[1005];//每一步结束都是开灯和每一步结束都是关灯要走的最小步数
int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        char a[105];
        a[0]='0';
        scanf("%s",a+1);
        memset(open,0,sizeof(open)),memset(close,0,sizeof(close));
        int l=strlen(a+1);
        open[0]=1;           //原始键盘是小写吗,open[0]=1,!!!!!!!!!
        for(int i=1;i<=l;i++)
        {
            if(a[i]>='a'&&a[i]<='z')//输入小写
            {
                close[i]=min(close[i-1]+1,open[i-1]+2);//关灯状态是由原关灯状态直接输小写或者原开灯状态下字母+关灯
                open[i]=min(close[i-1]+2,open[i-1]+2);//开灯状态是由原关灯状态字母+开灯或者原开灯状态下shift+字母
            }
            else   //输入大写
            {
                close[i]=min(close[i-1]+2,open[i-1]+2);//关灯状态是由原关灯状态shift+字母或者原开灯状态下字母+关灯
                open[i]=min(close[i-1]+2,open[i-1]+1);//开灯状态是由原关灯状态开灯+字母或者原开灯状态下直接输大写
            }
        }
        printf("%d\n",min(close[l],open[l]+1));//要么直接是关灯的最后一步或者开灯的最后一步把灯关了
    }
    return 0;
}

你可能感兴趣的:(hdu 简单DP 1009 How to Type)