• HDU 2577 How to Type (DP)


    How to Type

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


    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
     
    Recommend
    lcy
     
    这道题目很有意思,很考思维能力,但是这个状态是不好想到的
    分为两种:
    1、open[i]表示到第i个字母的时候输入敲击键盘多少次,此时大写灯是开着的
    2、close[i]表示到第i个字母的时候输入敲击键盘多少次,此时大写灯是关着的
     
    那么转移方程:

    if('a'<=str[i] && str[i]<='z')//处理小写字母
    {
      open[i]=min(open[i-1]+2,close[i-1]+2);

      //到输入第i个小写字母的时候此时要求灯是开着的,如果输入前一个字母的时候大写灯是开着的,则要按Shift再输入字母。

      //如果输入前一个字母大写灯是关着的,则输入字母再打开大写灯。

      close[i]=min(open[i-1]+2,close[i-1]+1);
    }

    else//处理大写字母
    {
    open[i]=min(open[i-1]+1,close[i-1]+2);
    close[i]=min(open[i-1]+2,close[i-1]+2);
    }

    初始化的时候要注意

    open[0]=1; 因为一开始大写灯是关着的

    close[0]=0;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<stdlib.h>
    #include<queue>
    #include<stack>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=100+5;
    const int INF=0x3f3f3f3f;
    const double EPS=1e-9;
    int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};
    int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
    int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    int kase,len,open[MAXN],close[MAXN];
    char str[MAXN];
    int main()
    {
        scanf("%d",&kase);
        while(kase--)
        {
            scanf("%s",str+1);
            memset(open,0,sizeof(open));
            memset(close,0,sizeof(close));
            open[0]=1;
            int i;
            for(i=1;str[i];i++)
            {
                if('a'<=str[i] && str[i]<='z')//小写
                {
                    open[i]=min(open[i-1]+2,close[i-1]+2);
                    close[i]=min(open[i-1]+2,close[i-1]+1);
                }
                else//大写
                {
                    open[i]=min(open[i-1]+1,close[i-1]+2);
                    close[i]=min(open[i-1]+2,close[i-1]+2);
                }
            }
            printf("%d
    ",min(open[i-1]+1,close[i-1]));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    USACO 4.1 Fence Rails
    POJ 1742
    LA 2031
    uva 10564
    poj 3686
    LA 3350
    asp.net MVC 3多语言方案--再次写, 配源码
    使用Log4net记录日志
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    为什么要使用反射机制
  • 原文地址:https://www.cnblogs.com/clliff/p/4248298.html
Copyright © 2020-2023  润新知