How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 842 Accepted Submission(s): 364
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
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
题目大意:给你一个字符串,问要至少按多少次键盘才能打出这些字母,有大写和小写,可以按caps lock,也可以按shift。注意!!最后打完后,caps lock一定要是关灯的(小写)。
一道很好的DP题目!非常好!用on[]记住开灯的状态,用off[]记住关灯的状态,然后根据大小写字母来写状态转移方程!下面代码有详细注释。
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
char ch[105];
int on[105]; //打开大写
int off[105]; //关闭大写
int main()
{
int i, t, len;
scanf("%d", &t);
while(t--)
{
scanf("%s", ch);
len = strlen(ch);
off[0] = 0; //刚开始的没开灯
on[0] = 1; //开灯的要+1
for(i = 0; i < len; i++)
{
if(ch[i] >= 'a' && ch[i] <= 'z') //小写字母
{
//开:(开~~shift+type, 关~~type+开灯)
on[i+1] = min(on[i] + 2, off[i] + 2);
//关:(开~~lock+type, 关~~type)
off[i+1] = min(on[i] + 2, off[i] + 1);
}
else //大写字母
{
//开:(开~~type, 关~~开灯+type)
on[i+1] = min(on[i] + 1, off[i] + 2);
//关:(开~~lock+type, 关~~shift+type)
off[i+1] = min(on[i] + 2, off[i] + 2);
}
}
on[len]++;
printf("%d\n", min(on[len], off[len]));
}
return 0;
}
分享到:
相关推荐
HDU的一题........HDU DP动态规
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,...
HDU上DP大集合,里面包括题,题解,代码,对DP入门者很实用,对DP老手也是有很大的提高
动态规划DP题解 POJ HDU部分动态规划DP题解
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
hdu1001解题报告
HDU1059的代码
hdu 1574 passed sorce
hdu2101AC代码
HDU的ACM,非常的好 涉及了很多算法,例如二分匹配、博弈、组合、最小生成树、搜索、动态规划、贪心算法
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
自己做的HDU ACM已经AC的题目
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1166线段树代码
HDU最全ac代码
hdu动态规划算法集锦