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



题目链接:

pid=2577">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\n", min(dp[len][0], dp[len][1] + 1));
}
}

HDU 2577 How to Type (线性dp)的更多相关文章

  1. HDU 2577 How to Type(dp题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...

  2. hdu 2577 How to Type(DP)

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU 2577 How to Type【DP】

    题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就 ...

  4. HDU 2577 How to Type DP也可以模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...

  5. hdu 2577 How to Type(dp)

    Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...

  6. HDU 2577 How to Type (DP,经典)

    题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...

  7. HDU 2577 分情况多维DP

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 2709 Sumsets 经典简单线性dp

    Sumsets Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. HDU 3455 Leap Frog(线性DP)

    Problem Description Jack and Jill play a game called "Leap Frog" in which they alternate t ...

随机推荐

  1. 【转】Flash AS3.0 中的自定义事件

    原文 http://www.cnblogs.com/acpp/archive/2010/10/19/1855670.html package { import flash.events.Event; ...

  2. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

  3. hdoj-1593-find a way to escape【数学题】

    find a way to escape Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  4. 调用C#版gdal库的一个注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在编译完C#版gdal库(x86平台)下,写了一个C#的控制台测试程序,出现下面的错误: 解决办法是将工程的目标平台设 ...

  5. JQ 实施编辑 (clone()复制行||双击编辑)

    //代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  6. Linux 内建命令和系统命令

    shell内建命令是指bash(或其它版本)工具集中的命令.一般都会有一个与之同名的系统命令,比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他们行为大体相仿.当在bash中键入 ...

  7. C# Unable to load DLL 'WzCanDll.dll':找不到指定的模块

    一.打开app无法加载DLL 我用C++编写的DLL,然后用C#写的界面APP,在自己的电脑上打开没有问题,放在其它电脑上就出现无法加载DLL库的问题,一连接APP就会出现问题,如下图所示: 二.解决 ...

  8. 自己动手写SSO(单点登录)

    SSO在我们的应用中非常常见,例如我们在OA系统登录了,我们就可以直接进入采购系统,不需要再登录了,这样使我们非常方便.现在网上也有很多实现方法,于是乎我也想写一个看看.我主要用到的是cookie的机 ...

  9. HDU——T 3336 Count the string

    http://acm.hdu.edu.cn/showproblem.php?pid=3336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  10. 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

    [144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...