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;
    }
  • 相关阅读:
    Python 元组
    Python 字典
    Python 列表
    Python 数字(函数)
    Python 序列操作符与函数(字符串)
    JavaScript使用IEEE 标准进行二进制浮点运算,产生莫名错误
    网站用户体验要点(翻译理论)
    去掉checkbox边框的方法
    WPF数据模板样式选择器
    JS中年份问题的纠结!
  • 原文地址:https://www.cnblogs.com/fish7/p/4330780.html
Copyright © 2011-2022 走看看