zoukankan      html  css  js  c++  java
  • 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): 4616    Accepted Submission(s): 2084

    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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577

    题目大意:坑比题,给一些有大写和小写组成的字符串,问用键盘打出它们的最少按键次数
    按键规则:对于每个小写字母。加上shift可使其变成大写。注意shift不能够一直按着不放,还能够开大写锁Caps lock键,按完这键输入就变成大写,此时再按shift。则打出小写(mac的键盘貌似不是这样),注意每次大写锁,最后必须把它关上

    题目分析:知道题以后就非常easy了dp[i][0]和dp[i][1]分别表示到第i个字符大写锁关,开着时要按的最少次数,则有4种情况
    假设到第i个字符为小写且此时开着大写锁,则它前一个必定是大写,由于假设前一个是小写,当前也是小写。我显然不须要开大写锁
    dp[i][1] = dp[i - 1][1] + 2
    假设到第i个字符为小写且此时没开大写锁,直接打即可了
    dp[i][0] = dp[i - 1][0] + 1
    假设到第i个字符为大写且此时开着大写锁。则为前一个开着大写锁的情况加1和前一个没开大写锁的情况加2的最小值,表示开锁
    dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][0] + 2)
    假设到第i个字符为大写且此时没开大写锁。则为前一个没开大写锁的情况加2(按shift)和前一个开着大写锁的情况加2的最小值。表示关锁
    dp[i][0] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 2)

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int const MAX = 105;
    char s[MAX];
    int dp[MAX][2];
    
    bool judge(char ch)
    {
        if(ch >= 'A' && ch <= 'Z')
            return true;
        return false;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%s", s + 1);
            int len = strlen(s + 1);
            memset(dp,0, sizeof(dp));
            dp[0][1] =  1;
            for(int i = 1; i <= len; i++)
            {
                if(judge(s[i]))
                {
                    dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][0] + 2);
                    dp[i][0] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 2);
                }
                else
                {
                    dp[i][1] = dp[i - 1][1] + 2;
                    dp[i][0] = dp[i - 1][0] + 1;
                }
            }
            printf("%d
    ", min(dp[len][0], dp[len][1] + 1));
        }
    }



  • 相关阅读:
    ural 1818 Fair Fishermen
    2013ACM-ICPC杭州赛区全国邀请赛总结
    成为程序界的叶问,你需要打败这十个人
    在互联网时代,你是消费者还是创造者?
    别让好想法埋没:如何进行APP开发?
    从无到有:学习程序开发的四个步骤
    BAT三家互联网公司哪家更注重用户体验?
    Stack Overflow大揭密:哪一种程序员工资最高?
    web产品浏览器兼容性问题你有考虑到吗?
    响应式网页设计:互联网web产品RWD概念
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7096064.html
Copyright © 2011-2022 走看看