HDU 2577 How to Type (DP,经典)
题意:
打字游戏,求所按的最少次数。给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反。注意:在打完所有字后,如果cap键是开着的,要关它,也就是要算多一次。
思路:
DP,根据每个字符打完后cap键盘是开着的还是关着的,最后dp[最后一个字符][关着的]为答案。规模降低到1个字符,每次考虑增加一个字符,打这个字符有两种选择,从上一个字符打完后的cap键关/开的两种状态来按下此字符,按完此字符后考虑使cap键开着或者关掉。
dp[当前字符][关着的]= 可从两种途径而来(取最小即可):(1)dp[上一个字符][关着的] (2)dp[上一个字符][开着的]
dp[当前字符][开着的]= 同上。
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
using namespace std;
const int N=;
char str[N];
int dp1[N], dp2[N]; int cal(int len)
{ dp2[]=; //亮
for(int i=; i<=len; i++)
{
if( str[i]<='Z' ) //大写
{
dp1[i]=min(dp1[i-]+, dp2[i-]+);
dp2[i]=min(dp1[i-]+, dp2[i-]+);
}
else //小写
{ dp1[i]=min(dp1[i-]+, dp2[i-]+);
dp2[i]=min(dp1[i-]+, dp2[i-]+);
}
}
return min(dp1[len],dp2[len]+);
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
cin>>t;
while(t--)
{
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
scanf("%s",str+);
cout<<cal(strlen(str+))<<endl;
}
return ;
}
AC代码
HDU 2577 How to Type (DP,经典)的更多相关文章
- HDU 2577 How to Type DP也可以模拟
http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...
- hdu 2577 How to Type(dp)
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
- HDU 2577 How to Type (线性dp)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- hdu 2577 How to Type(DP)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 2577 How to Type【DP】
题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就 ...
- HDU 2577 How to Type (字符串处理)
题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...
- HDU 2577 How to Type (字符串处理)
题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...
- HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...
随机推荐
- 使用Varnish代替Squid做网站缓存加速器的详细解决方案----转载
[文章作者:张宴 本文版本:v1.2 最后修改:2008.01.02 转载请注明出处:http://blog.s135.com] 我曾经写过一篇文章──<初步试用Squid的替代产品──Varn ...
- ios下最简单的正则,RegexKitLite
ios下最简单的正则,RegexKitLite 1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中.备用地址:http://www.coco ...
- java 以及 vs 的快捷键
javactrl+shift+y 小写ctrl+shift+x 大写ctrl+shift+f 格式化代码 vsctrl+u 小写ctrl+shift+u 大写ctrl+k+f 格式化代码
- 2013 ACM/ICPC Asia Regional Online —— Warmup
1003 Rotation Lock Puzzle 找出每一圈中的最大值即可 代码如下: #include<iostream> #include<stdio.h> #inclu ...
- C语言一些常用内存分配函数
首先看个问题程序(这里用的是TC编译器): #include "stdlib.h" #include "stdio.h" void main() { in ...
- OSX Mavericks下使用Synergy进行多台主机通过wifi共享键鼠问题的解决方法
转帖: OSX 10.9 几天用下来还是遇到几处问题的:之前先是遇到了OSX Mavericks GM598无法从Appstore升级到完全正式版的问题,下载无反应,后来找到了解决方法,发在以下链接: ...
- Sold out
When will the writer see the play? 'The play may begin at any moment,'I said. 'It may have begun alr ...
- php 实现树形结构
<?phpclass Tree{ private $OriginalList; public $pk;//主键字段名 public $parentKey;//上级id字段名 public $ch ...
- PageLayoutControl的基本操作
整理了下对PageLayoutControl的基本功能操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- iOS LLDB调试器和断点调试
技巧一:运行时修改变量的值 你以前怎么验证是不是某个变量的值导致整段程序不能正常工作?修改代码中的变量的值,然后cmd+r重新启动app?现在你不需要这么做了,只需要设置一个断点,当程序在这进入调试模 ...