感觉这道dp题还是有点技巧的,此题设置了两个数组:open[]和close[],分别用来记录capslock一直开启状态和一直关闭状态下的最少输入次数。此时只要判断字母的大小写,选用最优子结构即可。状态转移方程为:

str[i]是小写字母:

open[i]=min(open[i-1]+2,close[i-1]+2);
close[i]=min(close[i-1]+1,open[i-1]+2);

str[i]是大写字母:

open[i]=min(open[i-1]+1,close[i-1]+2);
close[i]=min(close[i-1]+2,open[i-1]+2);

#include"iostream"
#include"stdio.h"
#include"string.h"
#include"ctype.h"
#define mx 1000
using namespace std;
char str[mx];
int open[mx],close[mx];
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int i,j,t;
cin>>t;
getchar();
while(t--)
{
memset(open,,sizeof(open));
memset(close,,sizeof(close));
cin>>str;
if(islower(str[])) {open[]=;close[]=;}
else {open[]=;close[]=;}
for(i=;str[i]!='\0';i++)
{
if(islower(str[i]))
{
open[i]=min(open[i-]+,close[i-]+);
close[i]=min(close[i-]+,open[i-]+);
}
else
{
open[i]=min(open[i-]+,close[i-]+);
close[i]=min(close[i-]+,open[i-]+);
}
}
cout<<close[i-]<<endl;
}
return ;
}

hdu How to Type的更多相关文章

  1. HDU 2577---How to Type

    HDU  2577 Description Pirates have finished developing the typing software. He called Cathy to test ...

  2. HDU 3586 二分答案+树形DP判定

    HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

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

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

  4. HDU 2577 How to Type(dp题)

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

  5. hdu 2577 How to Type(dp)

    Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...

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

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

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

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

  8. HDU 2577 How to Type DP也可以模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...

  9. hdu 2577 How to Type(DP)

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

随机推荐

  1. linux ldconfig

    http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...

  2. putty mtputty 设置utf8编码

    2013年10月30日 10:02:36 先load你指定的ip 然后选择左侧目录中的windows->translation 再在右侧选择utf-8编码 选中后,点击左侧目录中的session ...

  3. DP:Space Elevator(POJ 2392)

    太空电梯 题目大意:一群牛想造电梯到太空,电梯都是由一个一个块组成的,每一种块不能超过这个类型的高度,且每一种块都有各自的高度,有固定数量,问最高能造多高. 这题就是1742的翻版,对ai排个序就可以 ...

  4. Ubuntu下查看linux版本,内核版本,系统位数,gcc版本

    1. 查看linux版本  sunny@ubuntu:~$cat /etc/issueUbuntu 11.04 \n \l 2. 查看内核版本1) sunny@ubuntu:~$ cat /proc/ ...

  5. Linux下的ip命令,除了ifconfig还有很多

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  6. 二、JavaScript语言--JS基础--JavaScript入门篇

    1.如何插入JS 使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要成对出现,并把JavaScript代码写在<scri ...

  7. 查看nginx版本号

    # ./nginx -v Tengine version: Tengine/ (nginx/)

  8. Linq学习笔记---Linq to Sql之where

    http://kb.cnblogs.com/page/42465/ Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断 ...

  9. MyISAM InnoDB 区别

    MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...

  10. 亿条数据在PHP中实现Mysql数据库分表100张

    当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度.笔者做了一个简单的尝试,1亿条数据,分100张表.具体实现过程如下: 首先创建100张表: $i=0; while($i<=9 ...