hdu2577——How to Type

How to Type

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3920    Accepted Submission(s): 1800

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:  2870 2830 2845 1058 2571

DP题,设dp[i][0]表示当打印第i个字符时大写未锁定,dp[i][1]表示打印第i个字符时大写锁定

如果第i个字符是大写字母

dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + 2;

dp[i][1] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 1);

如果第i个字符是小写字母

dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1] + 2);

dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + 2;

初始化dp[0][0] = 0,dp[0][1] = 1;

最后输出min(dp[n][0], dp[n][1] + 1)

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
int dp[120][2];
char str[120];

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%s", str);
		int n = strlen(str);
		memset (dp, inf, sizeof(dp));
		dp[0][0] = 0;
		dp[0][1] = 1;
		for (int i = 1; i <= n; i++)
		{
			if (str[i - 1] >= 'A' && str[i - 1] <= 'Z')
			{
				dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + 2;
				dp[i][1] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 1);
			}
			else
			{
				dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1] + 2);
				dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + 2;
			}
		}
		printf("%d\n", min(dp[n][0], dp[n][1] + 1));
	}
	return 0;
}
时间: 2024-11-05 19:44:47

hdu2577——How to Type的相关文章

HDU-2577 How to Type DP

字符输入问题,刚开始没有考虑到shift+字符输入和当前大小写锁定状态相反状态字符只需要两步的问题. 状态转移方程:dp1[i]表示输入完第i个字符后为大写状态需要的步数 dp2[i]表示输入完第i个字符后为小写状态所需要的步数 如果第i个字符为小写: dp1[i]=min(dp1[i-1]+2,dp2[i-1]+2); dp2[i]=min(dp1[i-1]+2,dp2[i-1]+1); 如果第i个字符为大写: dp1[i]=min(dp1[i-1]+1,dp2[i-1]+2): dp2[i]

HDU2577 How to Type【DP】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=2577 题目大意: 给你一个仅仅包括大写和小写字母的字符串,如今Pirates要从键盘上输出它.按CapsLk可开 启关闭大写和小写指示灯. 按Shift可转换将输入字母的大写和小写而不改变CapsLk的开关状态.Pirates有 一个坏习惯,假设输入的时候CapsLk是开着的,那么输入结束后必须把它关闭. 问,输入一个字符 串,最小的按键数目是多少 思路: 用两个数组dpa和dpb分别来表示Ca

[HDU2577]How to Type(DP)

题目链接 题意 给一个大小写字符串,求最少敲击字符串次数,最开始和最后要求shift都是down的.如日常,大小写转换可以ctrl+z或者shift保持 up/down. 题解 两个dp数组,一个表示当前shift状态是up的最小转换次数,一个表示当前shift状态是down的最小转换次数.最后做处理和比较,再加上字符数即可. 代码 import java.util.Scanner; public class Main { public static void main(String args[

HDU2577 How to Type

题目链接 一道DP问题 设dp[i][j]为当前状态下的最小花费,j=1代表Caps Lock打开,j=0代表Caps Lock关闭 #include<bits/stdc++.h> using namespace std; int dp[105][2]; int main() { int T; scanf("%d",&T); while(T--) { string str; cin>>str; int len=str.length(); memset(d

jquery_ui

/*! jQuery UI - v1.10.4 - 2014-01-17 * http://jqueryui.com * Includes: jquery.ui.core.js, jquery.ui.widget.js, jquery.ui.mouse.js, jquery.ui.position.js, jquery.ui.accordion.js, jquery.ui.autocomplete.js, jquery.ui.button.js, jquery.ui.datepicker.js,

苹果APP更新上架被拒的另一种理由(Safety - Objectionable Content)

这两个星期,本来想和大伙分享:写IT连创业系列运营篇,但时间飞过,仍只是写了开头,一直很忙,没能完往下写.今天就动手写点其它内容,哈哈,免的和小伙伴太陌生???前几天IT恋更新了下版本(主要是解决远程APNS通知的问题了) http://p.baidu.com/itopic/main/qlog?qid=d7156162633336383561662600&type=questionlog http://p.baidu.com/itopic/main/qlog?qid=11166162633962

移动端效果之ScrollList

写在前面 列表一直是展示数据的一个重要方式,在手机端的列表展示又和PC端展示不同,毕竟手机端主要靠滑.之前手机端之前一直使用的 ,但是 本身其实有很多兼容性 ,想改动一下需求也很不容易,可以看我之前写的这一文章 "IScroll那些事--内容不足时下拉刷新" (这里并不是说 不好,里面对手机.浏览器 http://p.baidu.com/itopic/main/qlog?qid=ba6b6162636431633336382700&type=questionlog http:/

【原创】Unity3D跨平台动态库编译---记kcp基于CMake的各平台构建实践

C/CPP库在windows.Linux.Mac.android.iOS.windows phone等各平台动态库插件编译打包,供Unity3D使用.这里通过可靠UDP网络库kcp在各个平台上的打包来实践一下,含完整构建过程和构建项目,很容易参考这里的步骤举一反三实践到自己的项目当作. http://p.baidu.com/itopic/main/qlog?qid=94126162633861353663642800&type=questionloghttp://p.baidu.com/itop

Description Resource Path Location Type The superclass &quot;javax.servlet.http.HttpServlet&quot; was not foun

一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /easyBuy/src/main/webapp line 1 JSP Problem 经过查找原因,原来是因为忘记设置server