HDOJ题目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): 4467    Accepted Submission(s): 2034

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

Author

Dellenge

Source

HDU 2009-5 Programming Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  1158 3008 1227 1422 1300

两个状态,大写锁定开,大写锁定关。然后推后一个状态取最小值即可。

因为这个最后她还要把大写锁定给关了,那就需要最后再加一取下大小。

ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define min(a,b) (a>b?b:a)
char s[110];
int dp[2][110];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s);
		int len=strlen(s),i;
		if(isupper(s[0]))
		{
			dp[0][0]=2;
			dp[1][0]=2;
		}
		else
		{
			dp[0][0]=1;
			dp[1][0]=2;
		}
		for(i=1;i<len;i++)
		{
			if(isupper(s[i]))
			{
				dp[0][i]=min(dp[1][i-1]+2,dp[0][i-1]+2);
				dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+1);
			}
			else
			{
				dp[0][i]=min(dp[1][i-1]+2,dp[0][i-1]+1);
				dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+2);
			}
		}
		printf("%d\n",min(dp[0][len-1],dp[1][len-1]+1));
	}
}
时间: 2024-08-08 00:40:39

HDOJ题目2577 How to Type(dp)的相关文章

HDU 2577 How to Type (DP,经典)

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

HDOJ 题目4714 Tree2cycle(树形DP)

Tree2cycle Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1730    Accepted Submission(s): 401 Problem Description A tree with N nodes and N-1 edges is given. To connect or disconnect one ed

HDOJ 题目3555 Bomb(数位DP)

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 10419    Accepted Submission(s): 3673 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorist

HDOJ题目2089 不要62(数位DP)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25391    Accepted Submission(s): 8788 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就

HDOJ 题目4833 Best Financing(DP)

Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 496    Accepted Submission(s): 155 Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在

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]

[HDOJ - 5282] Senior&#39;s String 【DP】

题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = max(f[i - 1][j], f[i][j - 1]); if (x[i] == y[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); 然后再设置一个状态 g[i][j], 表示 x 串的前 i 个字符中,有多少个长为 f[i][j] 的子序列同时也

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  输入

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1