首页 > 代码库 > hdu 2577 How to Type 如何保证英文输入状态下,可以按最小次数来完成输入

hdu 2577 How to Type 如何保证英文输入状态下,可以按最小次数来完成输入

How to Type

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3704    Accepted Submission(s): 1697


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
 

问:打出那段字母一共要按几下键盘,shift 按后只能输入一个字母。开始和最后CAPS都要保持关闭状态

思路:如果有两个连续的字母是大写或者小写的话,就可以按一次CAPS,因为就算只有两个,这和按shift 是等价的,都是一共四次;如果后面还有连续的话,CAPS就更节省按键次数了;所以贪心,凡是两个以上的连续串都改变CAPS,单独一个的话,选择按shift来改变大小写;但是最后一个字母要注意下,如果最后一个字母是小写,而当前CAPS是开启的,那要选择关闭CAPS;这样最后不用多按一下来关闭CAPS;

#include<stdio.h>
#include<string.h>
int main()
{
	int t,sum,ans,da,i;
	char a[2050];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",a);
		sum=ans=0;
		da=0;
		for(i=0;a[i]!=0;i++)
		{
			if(a[i]>='A'&&a[i]<='Z'&&a[i+1]>='A'&&a[i+1]<='Z'&&da==0)//如果连续两个大写 就转成大写的
			{
				da=1;
				ans++;
			}
			if(a[i]>='a'&&a[i]<='z'&&(a[i+1]>='a'&&a[i+1]<='z'||a[i+1]==0)&&da==1)//连续两个小写的就转成大写的 
				//注意:如果已经是最后一个的话,就要转换成小写,这样最后可以少一步转换回小写的操作;
			{
				da=0;
				ans++;
			}
			if(a[i]>='A'&&a[i]<='Z')
			{
				if(da==0)
					ans+=2;
				else
					ans++;
			}
			else if(a[i]>='a'&&a[i]<='z')
			{
				if(da==0)
					ans++;
				else
					ans+=2;
			}
		}
		if(da)
		{
			ans++; 
		}
		printf("%d\n",ans);
	}
	return 0;
}
   









hdu 2577 How to Type 如何保证英文输入状态下,可以按最小次数来完成输入