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
题目大意:
给你一个仅仅包括大写和小写字母的字符串,如今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;
}