hdu 2577 How to Type(DP)

来源:互联网 发布:淘宝产品怎么推广 编辑:程序博客网 时间:2024/05/21 06:20

How to Type

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4658    Accepted Submission(s): 2110


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
3PiratesHDUacmHDUACM
 

Sample Output
888
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

题意就是每组给一个字符串,问你打出这个字符串需要敲多少次键盘要求打完后CapsLock保持关闭.(注意按一次Shift只能输入一个字母,同时在CapsLock开启时使用Shift能输入小写字母).作为菜鸟,下边是看大神的分析::: 这题可以直接将连续的(大小写相同的)字母根据长度分为长度为1和长度为1以上的来处理,一遍遍历后得出结果(复杂度O(n)),当然也可以使用DP来做.这里给出DP的做法.对于每一个字符,针对输入前后CapsLock开启关闭状态分类若输入后CapsLock关闭假如输入前关闭,则要么直接输入(小写字母),要么用Shift+当前字母输入(大写字母)假如输入前开启,要么先输入当前字母再按CapsLock(当前字母是大写的),要么先按CapsLock再输入字母(当前字母小写),都是按两次若输入后CapsLock开启假如输入前开启,则要么直接输入(大写字母),要么用Shift+当前字母输入(小写字母)假如输入前关闭,要么先输入当前字母再按CapsLock(当前字母是小写的),要么先按CapsLock再输入字母(当前字母大写),都是按两次由此可以得出状态转移方程我们用DP[i][0]来代表输入第i个字母后且CapsLock关闭的情况下需要的最少次数,用DP[i][1]来代表输入第i个字母后且CapsLock开启的情况下需要的最少次数,并定义函数int check(char ch),若ch为大写字母,则返回1,否则返回0,则有dp[i][0]=min(dp[i-1][0]+check(s[i])+1,dp[i-1][1]+2)dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2-check(s[i]))特殊的,当i==0时dp[0][0]=check(s[i])+1dp[0][1]=2多练,,,,2015,7,21

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char s[110];int dp[110][2];int check(char a){if(a>='A'&&a<='Z')return 1;return 0;}int main(){int t,i,len;scanf("%d",&t);while(t--){scanf("%s",s);len=strlen(s);for(i=0;i<len;i++){if(i==0){dp[i][0]=check(s[0])+1;dp[i][1]=2;}else{dp[i][0]=min(dp[i-1][0]+check(s[i])+1,dp[i-1][1]+2);dp[i][1]=min(dp[i-1][1]+2-check(s[i]),dp[i-1][0]+2);}}printf("%d\n",dp[len-1][0]);}return 0;}



0 0
原创粉丝点击