hdu How to Type
感觉这道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的更多相关文章
- HDU 2577---How to Type
HDU 2577 Description Pirates have finished developing the typing software. He called Cathy to test ...
- HDU 3586 二分答案+树形DP判定
HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- 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)
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
- 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 2577 How to Type DP也可以模拟
http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...
- hdu 2577 How to Type(DP)
How to Type Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Java for LeetCode 066 Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- codeforces 476B.Dreamoon and WiFi 解题报告
题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 '+' 和 '-' 组成,而 ...
- springJDBC一对多关系,以及Java递归,jsp递归的实现
maven编译,springMVC+spring+springJDBC框架. 要实现的功能是一个文件夹下,可能显示n个文件夹,每个文件夹下又可能显示n个文件夹.... 前台效果:
- c++流的读写
std::istream input_stream;//这是一个文件流,想把它写入文件 思路是,先将input_stream流读入一个char* buffer; 然后用std::ofstream将bu ...
- win32_11gR2_database安装教程
- 利用phpexcel把excel导入数据库和数据库导出excel实现
<?php ); ini_set(,,,date(,date(,,,date(,,,date(,date(,,,date() ->setCellValue();); $objPHP ...
- hdu 3466 排序01背包
也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...
- C语言输出格式总结
转自:http://www.cnblogs.com/scbzljstudy/archive/2011/02/28/1966887.html 1 一般格式 printf(格式控制,输出表列) ...
- Effective C++笔记:构造/析构/赋值运算
条款05:了解C++默默编写并调用哪些函数 默认构造函数.拷贝构造函数.拷贝赋值函数.析构函数构成了一个类的脊梁,只有良好的处理这些函数的定义才能保证类的设计良好性. 当我们没有人为的定义上面的几个函 ...