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;
}
   
时间: 2024-10-08 20:00:42

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

HDU 2577 How to Type(dp题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符串用键盘输入到电脑中,一开始的时候大写锁定是关闭的,并且要求结束的时候也是关闭的,然后让你求输入这些字符串最少需要按多少次键盘(包括Cap Lock键和Shift键) 一个典型的dp题,定义一个一维数组就够了,然后dp[i]的含义的输入到第i个字符时需要按键的最少次数.然后递推公式如下: dp[i]

HDU 2577 How to Type(模拟)

题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2577 题意   给你一个由大写字母和小写字母组成的字符串  模拟键盘输入的最少按键次数 直接模拟每个字符的输入  flag表示capslock的状态  1表示打开  0为关闭  开始是和输入完毕都是关闭的关闭的  用plu记录shift和capslock的按键次数 当接下来输入的字母有连续n个跟capslock状态不同时  分析可只  只有n=1时适合用shift键 如flag=1 n=1  输入

HDU 2577 How to Type (线性dp)

How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4616    Accepted Submission(s): 2084 Problem Description Pirates have finished developing the typing software. He called Cathy to tes

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): 5452    Accepted Submission(s): 2436 Problem Description Pirates have finished developing the typing software. He called Cathy to

HDU 2577 How to Type 动态规划

友情题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2577 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=110; char str[N]; int close[N],open[N]; int MIN(int a ,int b) { return a=a<b?a:b; } int main(

HDU 2577 How to Type (DP,经典)

题意:打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的,要关它,也就是要算多一次. 思路:DP,根据每个字符打完后cap键盘是开着的还是关着的,最后dp[最后一个字符][关着的]为答案.规模降低到1个字符,每次考虑增加一个字符,打这个字符有两种选择,从上一个字符打完后的cap键关/开的两种状态来按下此字符,按完此字符后考虑使cap键开着或者关掉. dp[当

HDU 2577 How to Type【DP】

题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就可以写出状态转移方程了,因为最后需要灯是灭的,所以最后在找最小值的时候,dp[len][1]需要加1 又一次看的题解===go--go 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #includ

hdu 2577 How to Type(dp)

题意: 输入一行字符串 只包含大小写字母 可以使用shift 和 caps 键 切换大小写 问最少按几次键 思路: if(str[i]>='A'&&str[i]<='Z') { dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2);//on dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2);//off } else { dp[i][0]=min(dp[i-1][0]+2,dp[i-1][1]+2); dp[i][1]=mi

hdu 4857 逃生 (拓扑排序+保证最小在前面)

逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 74    Accepted Submission(s): 13 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. 同时,社会是不平