How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4552 Accepted Submission(s): 2057
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 8HintThe 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+字母或者shift+字母
如果现在caps灯亮 下一个字母如果还是大写字母 就可以直接按字母键
如果下一个字母是小写 则必须要先按caps让caps灯灭
所以要根据caps灯亮灯灭来进行状态转移
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int dpon[200],dpoff[200];
char ch[200];
int main()
{
//fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%s",ch);
int len=strlen(ch);
MEM(dpon,0); MEM(dpoff,0);
dpon[0]=1;
for(int i=0;i<len;i++)
{
if(ch[i]>='A'&&ch[i]<='Z')
{
//如果输入前一个单词时灯是亮着的,1.只需按所要输入的单词即可,2.按shift+所要输入的字母;
dpon[i+1]=min(dpon[i]+1,dpoff[i]+2);
//如果输入前一个单词时灯是灭的,1.按Caps Lock之后再按所要输入的字母,2.按Shift+所要输入的字母
dpoff[i+1]=min(dpon[i]+2,dpoff[i]+2);
}
else
{
dpon[i+1]=min(dpon[i]+2,dpoff[i]+2);
dpoff[i+1]=min(dpon[i]+2,dpoff[i]+1);
}
}
dpon[len]+=1;//将caps灯按灭
int ans=min(dpon[len],dpoff[len]);
printf("%d\n",ans);
}
return 0;
}