首页 > 代码库 > HDU2577_How to Type
HDU2577_How to Type
How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3773 Accepted Submission(s): 1730
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
题目大意:给你一个只包含大小写字母的字符串,现在Pirates要从键盘上输出它,按CapsLk可开
启关闭大小写指示灯。按Shift可转换将输入字母的大小写而不改变CapsLk的开关状态。Pirates有
一个坏习惯,如果输入的时候CapsLk是开着的,那么输入结束后必须把它关闭。问,输入一个字符
串,最小的按键数目是多少
思路:用两个数组dpa和dpb分别来表示CapsLk开启状态和关闭状态的最小操作数。
如果将要输入的字母是小写字母:
dpa[i+1] = min(dpa[i]+1,dpb[i]+2); 不开灯的情况下直接按字母,开灯情况则先关灯再按字母
dpb[i+1] = min(dpa[i]+2,dpb[i]+2); 不开灯的情况下按字母再开灯,开灯情况下Shift+按字母,
最后保持开灯状态
如果将要输入的字母是大写字母:
dpa[i+1] = min(dpa[i]+2,dpb[i]+2); 不开灯的情况下按Shift+字母,开灯情况下则按字母再关灯
dpb[i+1] = min(dpa[i]+2,dpb[i]+1); 不开灯的情况下开灯按字母,开灯情况下直接按字母
最后dpb[len]++,要把灯关掉。
初始化为dpa[0] = 0,dpb[0] = 1,因为dpb状态刚开始要开灯。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dpa[110],dpb[110]; char a[110]; int main() { int T; scanf("%d",&T); while(T--) { getchar(); scanf("%s",a); memset(dpa,0,sizeof(dpa)); memset(dpb,0,sizeof(dpb)); dpa[0] = 0;//dpa为关灯状态最小操作数 dpb[0] = 1;//dpb为开灯状态最小操作数 int len = strlen(a); for(int i = 0; i < len; i++) { if(a[i]>='a' && a[i]<='z') { dpa[i+1] = min(dpa[i]+1,dpb[i]+2); dpb[i+1] = min(dpa[i]+2,dpb[i]+2); } else { dpa[i+1] = min(dpa[i]+2,dpb[i]+2); dpb[i+1] = min(dpa[i]+2,dpb[i]+1); } } dpb[len]++; int ans = min(dpa[len],dpb[len]); printf("%d\n",ans); } return 0; }
HDU2577_How to Type
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。