How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4074 Accepted Submission(s): 1858
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
lcy
这道题目很有意思,很考思维能力,但是这个状态是不好想到的
分为两种:
1、open[i]表示到第i个字母的时候输入敲击键盘多少次,此时大写灯是开着的
2、close[i]表示到第i个字母的时候输入敲击键盘多少次,此时大写灯是关着的
那么转移方程:
if('a'<=str[i] && str[i]<='z')//处理小写字母
{
open[i]=min(open[i-1]+2,close[i-1]+2);
//到输入第i个小写字母的时候此时要求灯是开着的,如果输入前一个字母的时候大写灯是开着的,则要按Shift再输入字母。
//如果输入前一个字母大写灯是关着的,则输入字母再打开大写灯。
close[i]=min(open[i-1]+2,close[i-1]+1);
}
else//处理大写字母
{
open[i]=min(open[i-1]+1,close[i-1]+2);
close[i]=min(open[i-1]+2,close[i-1]+2);
}
初始化的时候要注意
open[0]=1; 因为一开始大写灯是关着的
close[0]=0;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=100+5; const int INF=0x3f3f3f3f; const double EPS=1e-9; int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int kase,len,open[MAXN],close[MAXN]; char str[MAXN]; int main() { scanf("%d",&kase); while(kase--) { scanf("%s",str+1); memset(open,0,sizeof(open)); memset(close,0,sizeof(close)); open[0]=1; int i; for(i=1;str[i];i++) { if('a'<=str[i] && str[i]<='z')//小写 { open[i]=min(open[i-1]+2,close[i-1]+2); close[i]=min(open[i-1]+2,close[i-1]+1); } else//大写 { open[i]=min(open[i-1]+1,close[i-1]+2); close[i]=min(open[i-1]+2,close[i-1]+2); } } printf("%d\n",min(open[i-1]+1,close[i-1])); } return 0; }