HDU 2577 How to Type DP也可以模拟
http://acm.hdu.edu.cn/showproblem.php?pid=2577
大意:
大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出按键次数。
大写字母可以通过caps lock 也可以用shift得到,小写也是。
但是起始状态和最后caps lock都是不锁定的。
求最少的按键次数。
思路:
不用DP的方法:
当有连续的大写或者小写用caps lock 键,否则用shift。
#include<cstdio>
#include<cstring>
const int MAXN=110;
int main()
{
int T;
scanf("%d",&T); while(T--)
{
char a[MAXN];
scanf("%s",a);
int len=strlen(a);
bool capslock=false;
int cnt=0;
for(int i=0;i<len;i++)
{
if(a[i] >= 'A' && a[i] <= 'Z')
{
if(capslock==false)
cnt++; //shift or capslock if(a[i+1]>= 'A' && a[i+1] <= 'Z')
capslock=true;
cnt++;
}
else
{
if(capslock==true)
cnt++; //shift or capslock
if(a[i+1]>= 'a' && a[i+1] <= 'z' ||a[i+1]=='\0') //a[i+1]=='\0'必须加,这也就是模拟wa原因
capslock=false;
cnt++;
}
}
if(capslock)
cnt++;
printf("%d\n",cnt); }
return 0;
}
DP的方法
设lock[i]为敲完第i个之后为锁定状态,而notlock[i]为敲完第i个之后为没有锁定状态
则分大小写讨论:
a[i]若为大写:
lock[i]=min(lock[i-1]+1,notlock[i-1]+2);
notlock[i]=min(lock[i-1]+2,notlock[i-1]+2);
否则:
lock[i]=min(lock[i-1]+2,notlock[i-1]+2);
notlock[i]=min(lock[i-1]+2,notlock[i-1]+1);
至于怎么来的,你敲一下键盘你就懂了
还有就是最后要变成没有锁定状态。。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
const int MAXN=110;
int main()
{
int T;
scanf("%d",&T); while(T--)
{
char a[MAXN];
int lock[MAXN]={0};
int notlock[MAXN]={0};
scanf("%s",a);
int len=strlen(a);
if(isupper(a[0]))
{
lock[0]=2;
notlock[0]=2;
}
else
{
lock[0]=2;
notlock[0]=1;
} for(int i=1;i<len;i++)
{
if(isupper(a[i]))
{
lock[i]=min(lock[i-1]+1,notlock[i-1]+2);
notlock[i]=min(lock[i-1]+2,notlock[i-1]+2);
}
else //小写
{
lock[i]=min(lock[i-1]+2,notlock[i-1]+2);
notlock[i]=min(lock[i-1]+2,notlock[i-1]+1);
}
} printf("%d\n",min(notlock[len-1],lock[len-1]+1)); //最后要变成没有锁定的 }
return 0;
}
HDU 2577 How to Type DP也可以模拟的更多相关文章
- 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,经典)
题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...
- 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 ...
- DP问题(1) : hdu 2577
题目转自hdu 2577,题目传送门 题目大意: 现给你n个区分大小写的字符串,大小写可用Caps Lock和shift切换(学过计算机的都知道) 但是有一点需要注意(shift是切换,若现在是大写锁 ...
随机推荐
- AMD规范(RequireJS)、CMD规范(SeaJS)、CommonJS(BravoJS)规范的辨析
首先,AMD,CMD,CommonJS都实现了文件模块化. 对于依赖的模块:AMD是提前执行:CMD是延迟执行: AMD是依赖前置,CMD是依赖就近: AMD官方解释:https://github.c ...
- WHU 1470 Join in tasks 水题
http://acm.whu.edu.cn/land/problem/detail?problem_id=1470 大概是给你一个队列,每次移动队头的数到队尾并减1,如果本身这个数为1就删去. 然后a ...
- 2019.05.08 《Linux驱动开发入门与实战》
第六章:字符设备 申请设备号---注册设备 1.字符设备的框架: 2.结构体,struct cdev: 3.字符设备的组成: 4.例子: 5.申请和释放设备号: 设备号和设备节点是什么关系.? 设备驱 ...
- 7.Maven之(七)pom.xml配置文件详解
转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...
- Impala是什么?
Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统虽然也提供了SQL语义,但由于Hive底层 ...
- ZOJ QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- 【Codeforces Round #452 (Div. 2) C】 Dividing the numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两 ...
- Opera mini for S60 custom server
Opera mini for S60 custom server 在线改服 http://yourshell.info/mo/mini/ 本人贫穷一族,一直在用S60V2,这种手机启动JAVA很占内存 ...
- cwRsync 同步时报错 STATUS_ACCESS_VIOLATION
cwRsync 同步时报错 STATUS_ACCESS_VIOLATION windows XP 执行 cwRsync 同步时报错: 2 [main] rsync 3044 _cygtls::h ...
- eclipse- log 打印跟输出到文件
1.在eclipse中打印log,经常使用的就是log.e(string,string) 代码中如下 @Override public boolean onTouchEvent(MotionEvent ...