hdu 2577 How to Type
Posted njczy2010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2577 How to Type相关的知识,希望对你有一定的参考价值。
How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5452 Accepted Submission(s): 2436
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
Recommend
16464331 | 2016-03-07 10:38:54 | Accepted | 2577 | 0MS | 1564K | 1191 B | G++ | czy |
题解:
转自:http://blog.sina.com.cn/s/blog_892d87ff0100u1n5.html
解题报告:题意:输入以字符串,求最少需要敲多少下键盘才能完成输入!开两个数组open[N],close[N],分别记录灯亮时输入第i个字符所敲键盘的次数,灯灭时输入第i个字符时所敲键盘的次数!
一开始没想到,可以在大写锁定下,按Shift输入小写,唉,dp的意识还是不强,有待继续训练
转移方程:
1 if(judge(s[i]) == 1){ 2 open[i] = min(open[i - 1] + 1,close[i - 1] + 2); 3 close[i] = min(open[i - 1] + 2,close[i - 1] + 2); 4 } 5 else{ 6 open[i] = min(open[i - 1] + 2,close[i - 1] + 2); 7 close[i] = min(open[i - 1] + 2,close[i - 1] + 1); 8 }
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 #include <cctype> 7 #include <vector> 8 #include <cmath> 9 #include <map> 10 #include <queue> 11 12 #define ll long long 13 #define N 10005 14 #define eps 1e-8 15 16 using namespace std; 17 18 int T; 19 int ans; 20 char s[105]; 21 int l; 22 int open[105]; 23 int close[105]; 24 25 int judge(char c) 26 { 27 if(c >= ‘A‘ && c <= ‘Z‘) return 1; 28 return 0; 29 } 30 31 void solve() 32 { 33 int i; 34 i = 0; 35 open[i] = 1; 36 close[i] = 0; 37 for(i = 1;i <= l;i++){ 38 if(judge(s[i]) == 1){ 39 open[i] = min(open[i - 1] + 1,close[i - 1] + 2); 40 close[i] = min(open[i - 1] + 2,close[i - 1] + 2); 41 } 42 else{ 43 open[i] = min(open[i - 1] + 2,close[i - 1] + 2); 44 close[i] = min(open[i - 1] + 2,close[i - 1] + 1); 45 } 46 } 47 } 48 49 int main() 50 { 51 //freopen("in.txt","r",stdin); 52 scanf("%d",&T); 53 for(int ccnt=1;ccnt<=T;ccnt++){ 54 //while(scanf("%I64d%I64d",&a,&b)!=EOF){ 55 scanf("%s",s + 1); 56 l = strlen(s + 1); 57 solve(); 58 ans = min(open[l] + 1,close[l]); 59 printf("%d\n",ans); 60 } 61 return 0; 62 }
以上是关于hdu 2577 How to Type的主要内容,如果未能解决你的问题,请参考以下文章