How to Type

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

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

题意就是每组给一个字符串,问你打出这个字符串须要敲多少次键盘要求打完后CapsLock保持关闭.(注意按一次Shift仅仅能输入一个字母,同一时候在CapsLock开启时使用Shift能输入小写字母).
作为菜鸟。下边是看大神的分析::: 
这题能够直接将连续的(大写和小写同样的)字母依据长度分为长度为1和长度为1以上的来处理,一遍遍历后得出结果(复杂度O(n)),当然也能够使用DP来做.
这里给出DP的做法.
对于每个字符,针对输入前后CapsLock开启关闭状态分类

若输入后CapsLock关闭
假如输入前关闭,则要么直接输入(小写字母),要么用Shift+当前字母输入(大写字母)
假如输入前开启,要么先输入当前字母再按CapsLock(当前字母是大写的),要么先按CapsLock再输入字母(当前字母小写),都是按两次
若输入后CapsLock开启
假如输入前开启,则要么直接输入(大写字母),要么用Shift+当前字母输入(小写字母)
假如输入前关闭,要么先输入当前字母再按CapsLock(当前字母是小写的),要么先按CapsLock再输入字母(当前字母大写),都是按两次
由此能够得出状态转移方程
我们用DP[i][0]来代表输入第i个字母后且CapsLock关闭的情况下须要的最少次数,用DP[i][1]来代表输入第i个字母后且CapsLock开启的情况下须要的最少次数,并定义函数int check(char ch),若ch为大写字母,则返回1,否则返回0,则有
dp[i][0]=min(dp[i-1][0]+check(s[i])+1,dp[i-1][1]+2)
dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2-check(s[i]))
特殊的,当i==0时
dp[0][0]=check(s[i])+1
dp[0][1]=2
多练,,,。2015,7,21

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[110];
int dp[110][2];
int check(char a)
{
if(a>='A'&&a<='Z')
return 1;
return 0;
}
int main()
{
int t,i,len;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
len=strlen(s);
for(i=0;i<len;i++)
{
if(i==0)
{
dp[i][0]=check(s[0])+1;
dp[i][1]=2;
}
else
{
dp[i][0]=min(dp[i-1][0]+check(s[i])+1,dp[i-1][1]+2);
dp[i][1]=min(dp[i-1][1]+2-check(s[i]),dp[i-1][0]+2);
}
}
printf("%d\n",dp[len-1][0]);
}
return 0;
}

hdu 2577 How to Type(DP)的更多相关文章

  1. HDU 2577 How to Type(dp题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...

  2. HDU 2577 How to Type (字符串处理)

    题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...

  3. HDU 1864 最大报销额(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1864 题目: 最大报销额 Time Limit: 1000/1000 MS (Java/Others) ...

  4. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

  5. HDU 4562 守护雅典娜(dp)

    守护雅典娜 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  6. HDU - 6199 gems gems gems (DP)

    有n(2e4)个宝石两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石.一旦不能再取宝石就结束.双方都希望自己拿到的宝石数比对方尽可能 ...

  7. HDU 2577 How to Type (线性dp)

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU - 6357 Hills And Valleys(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=6357 题意 给一个数值范围为0-9的a数组,可以选择翻转一个区间,问非严格最长上升子序列,以及翻转的区间. 分析 ...

  9. HDU 1069 Monkey and Banana (dp)

    题目链接 Problem Description A group of researchers are designing an experiment to test the IQ of a monk ...

随机推荐

  1. 使用Ant 和 Maven打包发布命令行程序(转载)

    From:https://www.linux178.com/Java/maven-release.html 用Java写了一个命令行的小程序,使用的Intellij IDE是IDEA13原来一直使用A ...

  2. [Redux-Observable && Unit Testing] Mocking an ajax request when testing epics

    Often in unit tests we are focussing on the logic involved in crafting a network request, & how ...

  3. hdu 1863 畅通project kruskal || prim

    简单最小生成树,畅通project.这三道题目都是练习最小生成树的. 注意一下推断是否有通路时,kruskal能够推断每一个点的祖先是否同样.prim能够推断每一个点是否都加进集合里面了,也就是说是否 ...

  4. 漫话C++之string字符串类的使用(有汇编分析)

    C++中并不提倡继续使用C风格的字符串,而是为字符串定义了专门的类,名为string. 使用前的准备工作 在使用string类型时,需要包含string头文件,且string位于std命名空间内: # ...

  5. 企业网管软件之SOLARWINDS实战-基于浏览器的网络流量监控

    本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  6. slice深拷贝数组

    var a = [1, 2, 3, 4] var b = a.slice(0) b[0] = 2 // a = [1, 2, 3, 4] // b = [2, 2, 3, 4]

  7. 不重新启动VMWare虚拟机加入虚拟磁盘的方法(上)

    近期因为业务须要在不重新启动系统的前提下对系统进行扩容,前提是该系统做过lvm.可是没有足够的物理卷(硬盘),所以引出了改文.本文共分为上下两部分.这是第一部分. 文件夹 加入磁盘 做LVM 加入硬盘 ...

  8. jquery ui 分页插件 传入后台的连个參数名

    參数名: page .rows page=int(request.form.get('page',1).encode('u8')) rows1=int(request.form.get('rows', ...

  9. 用c实现的各种排序的方法

    #include <stdio.h> void swap(int *a, int *b); void bubble_sort(int a[], int n); void select_so ...

  10. Solr 读数据流程

    Solr 读数据流程: 1.用户提供搜索关键词,也就是搜索语句,需要经过分词器处理以及语言处理. 2.对处理之后的关键词,搜索索引找出对应Document 即记录. 3.用户根据需要从找到的Docum ...