zoukankan      html  css  js  c++  java
  • hdu 2577 How to Type(DP)

    题意:

    给一个字符串(有大写有小写),问最少需要按多少次【键盘上的键】才能写出来。

    开始前大写灯是关着的。结束后也必须保证大写灯是关的。

    例:

    Pirates  HDUacm  HDUACM

    8 8 8

    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

    思路:

    每打完一个字母大写灯都可能有两种状态。层次结构还是比较易看出来的。

    dp[i][0]:打完第i个字母大写灯是关闭状态所花最少次数。

    dp[i][1]:打完第i个字母大写灯是开启状态所花最少次数。

    代码:

    int dp_closed[105];
    int dp_open[105];
    
    
    int main(){
    
        int T;
        char str[105];
    
        cin>>T;
        while(T--){
            scanf("%s",str);
            int L=strlen(str);
    
            dp_open[0]=1;
            dp_closed[0]=0;
            rep(i,1,L-1){
                if(str[i-1]>='a' && str[i-1]<='z'){
                    dp_closed[i]=min( dp_closed[i-1]+1,dp_open[i-1]+2 );
                    dp_open[i]=min( dp_closed[i-1]+2,dp_open[i-1]+2 );
                }else{
                    dp_closed[i]=min( dp_closed[i-1]+2,dp_open[i-1]+2 );
                    dp_open[i]=min( dp_closed[i-1]+2,dp_open[i-1]+1 );
                }
            }
            if(str[L-1]>='a' && str[L-1]<='z'){
                dp_closed[L]=min( dp_open[L-1]+2,dp_closed[L-1]+1 );
            }else{
                dp_closed[L]=min( dp_open[L-1]+2,dp_closed[L-1]+2 );
            }
            printf("%d
    ",dp_closed[L]);
        }
    
        return 0;
    }
  • 相关阅读:
    Codeforces Round #592 (Div. 2)
    2019 China Collegiate Programming Contest Qinhuangdao Onsite
    2019CCPC 秦皇岛 E.Escape
    2018 Multi-University Training Contest 3
    AtCoder Regular Contest 098
    Educational Codeforces Round 74 (Rated for Div. 2)
    Codeforces Round #590 (Div. 3) F
    AtCoder Regular Contest 99
    [AH2017/HNOI2017] 单旋
    [CF1304F] Animal Observation
  • 原文地址:https://www.cnblogs.com/fish7/p/4330780.html
Copyright © 2011-2022 走看看