How to Type(详解)

How to Type

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

建立两个数组,用来记录写入第i个字母时需要敲击的键盘次数,一个表示小写状态,一个表示大写状态,每次读入一个字母的通过min()函数,来进行判定。
注意按下Caps lock键后,默认是大写,想变成小写可以按着shift键,来读入小写或者再按一次Caps lock
具体操作看下面代码。
简单dp题
翻译就自己翻译吧
下面附上代码和注释

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int small[150];
int big[150];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(small,0,sizeof(small));
		memset(big,0,sizeof(big));
		small[150]; 
		char s[150];
		small[0]=0;//小写状态 ,默认始终为小写(就是不按其他键的情况下,按出来的是小写) 
		big[0]=1;//大写状态,一开始按了caps lock所以所以算上1,而不是0,默认始终为大写。(就是不按其他键,按出来的是大写) 
		scanf("%s",s+1);//+1是因为从s[1]开始,为了不占0的位置,也是为了方便下面的循环 
		int len=strlen(s+1); //长度 
		for(int i=1;s[i];i++)
		{
			if(s[i]>='a'&&s[i]<='z') //读入一个小写字母 
			{
				small[i]=min(small[i-1]+1,big[i-1]+2);//对于小写状态直接+1就行(因为不需要任何操作),所以+1 
				                            //对于大写状态,因为一开始按下了Caps,所以要按下CapS来把大写转换成小写,所以+2 
				big[i]=min(small[i-1]+2,big[i-1]+2); //对于小写状态先读入小写字母,再按下Caps来保持大写状态,所以+2。
				                           //对于大写状态,因为一开始按下了Caps,所以要按下shift来把大写转换成小写 ,所以+2 
			}
			else if(s[i]>='A'&&s[i]<='Z') //读入一个大写字母 
			{
				small[i]=min(small[i-1]+2,big[i-1]+2); //对于小写状态,就是按下shift+对应字母键,所以+2。
				                        //对于大写状态,先写入对应大写字母,再按下Caps取消大写状态 ,所以+2 
				big[i]=min(small[i-1]+2,big[i-1]+1); //对于小写状态,就是先按Caps键,转换成大写状态,再按对应字母,所以+2。 
				                          //对于大写状态,直接写入对应大写字母即可,所以+1 
			}
		}
		printf("%d\n",min(small[len],big[len]+1));//对于大写状态要再按一次Caps所以+1 
	}
	return 0;
 } 

如果你有任何建议或者批评和补充,请留言指出,不胜感激。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值