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的子序列使得子序列中的 ...
随机推荐
- HDU 2843 I Will Win(概率题?,怨念颇深,简单)
题目 真不想说什么,,,这神题真讨厌,,,多校的.. //又是一道神题... #include<stdio.h> #include<string.h> //最大公约数 int ...
- POJ 1469
#include<iostream> #include<stdio.h> #include <string.h> #include <vector> # ...
- Xamarin for Visual Studio 3.11.666 稳定版 破解补丁 Version 3
前提概要 1.全新安装请参考 安装 Xamarin for Visual Studio. 2.本次补丁包含: ① Xamarin for Visual Studio 3.11.666 ② Xamari ...
- WCF分布式开发步步为赢(8):使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据
数据集(DataSet).数据表(DataTable).集合(Collection)概念是.NET FrameWork里提供数据类型,在应用程序编程过程中会经常使用其来作为数据的载体,属于ADO.NE ...
- Lines演示程序
#include "stdafx.h"#include "d3d9.h"#include "d3dx9.h" #pragma comment ...
- 国内银行CNAPS CODE 查询 苹果开发者,应用内购,需要填写税务相关信息必须的
https://e.czbank.com/CORPORBANK/QYUK http://weekend.blog.163.com/blog/static/746895820127961346724/
- How to say "no"?
How to say "no"?7招教你如何拒绝别人 Do you have a hard time saying no to others? Do you say “y ...
- Android核心分析之二十八Android GDI之Surface&Canvas
Surface&Canvas Canvas为在画布的意思.Android上层的作图几乎都通过Canvas实例来完成,其实Canvas更多是一种接口的包装.drawPaints ,drawPoi ...
- 多线程包:java.util.concurrent,
Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent,
- iOS CoreMotion框架(传感器)
我们知道iOS的应用真的太多了,很多应用让我们惊叹不已!!!很多意想不到的应用! 比如: 1.电子罗盘指南针之类的应用-让我们知道方向. 2.运动类型软件-让我们知道我们跑步多少公里. 3.社交软件中 ...