感觉这道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. DP:Cheapest Palindrome(POJ 3280)

    价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...

  2. CodeForces - 424B (贪心算法)

    Megacity Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  3. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  4. java 执行command

    StringBuffer buf = new StringBuffer(1000); try { Process pos = Runtime.getRuntime().exec("sh &q ...

  5. ubuntu 14.04安装mysql server & mysql client

    $ sudo apt-get install mysql-server

  6. hadoop配置文件加载顺序(转)

    原文  http://www.cnblogs.com/wolfblogs/p/4147485.html 用了一段时间的hadoop,现在回来看看源码发现别有一番味道,温故而知新,还真是这样的 在使用h ...

  7. InnoDB引擎的索引和存储结构

    在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的.而MySql数据库提供了多种存储引擎.用户可以根据不同的需求为数据表选择不同的存储引擎,用户也可以根据 ...

  8. Java Hour7

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为6 Hour,请各位不吝赐教. Hour7 Jav ...

  9. adb shell 命令详解(转)

    adb介绍 SDK的Tools文件夹下包含着Android模拟器操作的重要命令adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中方面 ...

  10. C++的那些事:类的拷贝控制

    1,什么是类的拷贝控制 当我们定义一个类的时候,为了让我们定义的类类型像内置类型(char,int,double等)一样好用,我们通常需要考下面几件事: Q1:用这个类的对象去初始化另一个同类型的对象 ...