Problem Description
Pirates have finished developing the typing software. He called Cathy to test his typing software. She is good at thinking. After testing for several days, she finds that if she types a string by some ways, she will type the key at least. But she has a bad habit that if the caps lock is on, she must turn off it, after she finishes typing. Now she wants to know the smallest times of typing the key to finish typing a string.
 
Input
The first line is an integer t (t<=100), which is the number of test case in the input file. For each test case, there is only one string which consists of lowercase letter and upper case letter. The length of the string is at most 100.
 
Output
For each test case, you must output the smallest times of typing the key to finish typing this string.
 
Sample Input
3
Pirates
HDUacm
HDUACM
 
Sample Output
8
8
8
由于这里有大写锁定开关两种状态 所以需要两个数组a b来进行dp
动态规划的问题实际上就是对多个子问题的优化处理问题 在考虑优化时 应该从一个子问题到下一个子问题的过程中找出所有可能的情况 并进行优化
 
#include<stdio.h>
#include<iostream>
#include<string.h>
int min(int x,int y)
{
 if(x>y) return y;
 else return x;
}
int f(char x)//1为大写
{
 if(x>='A'&&x<='Z') return 1;
 else return 0;
}
using namespace std;
int main()
{
    char s[130];
 int n,len,i,j,a[130];//开
 int b[130],t,mine;//关 a,b需要保证 其状态的变化量
 scanf("%d",&n);
 while(n--)
 {
  memset(a,0,sizeof(a));
  memset(b,0,sizeof(b));
  cin>>s;
  a[0]=1;
  len=strlen(s);
  for(i=0;i<len;i++)
  {
    if(f(s[i])==1)//da
    {
          a[i+1]=min(a[i]+1,b[i]+2);//开灯的话直接输入 关灯的话开灯在输入
    b[i+1]=min(a[i]+2,b[i]+2);// 开灯的话先输入再关 关灯的话按shitf在输入(这里可以开灯输入在关灯 但由于是找最少的次数 所以忽略)     
    } 
    else//xiao
    {
     a[i+1]=min(a[i]+2,b[i]+2);//开灯的话 按SHIFT输入 关灯的话 先输入在开灯(保证a数组的开灯状态)
     b[i+1]=min(a[i]+2,b[i]+1);//开灯的话  关灯再输入   关灯的话 直接输入
    }
  }
  a[len]++;// 最后不要忘了关灯
  mine=min(a[len],b[len]);
  cout<<mine<<endl;
 }
 return 0;
}

 

杭电2577 多数组dp问题的更多相关文章

  1. 杭电1466------简单的dp

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1466 #include<iostream> #include<cstdio> # ...

  2. 最少拦截系统(杭电1257)(DP)+(贪心)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 杭电hdu 2089 数位dp

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...

  4. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  7. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  8. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  9. 杭电 1272 POJ 1308 小希的迷宫

    这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...

随机推荐

  1. 新零售下的 AI智能货柜

    公司有个智能货柜,通过微信扫码开门,拿货,自动扣款,挺智能的.还不错.研究一下原理,网上查了一下. 文章简介: 目前新零售风刮的蛮大,笔者进入该领域近一年,负责过无人便利店.智能货柜.智慧商超等产品, ...

  2. windows中kill端口为8080的进程(或子进程)

    1.netstat -aon|findstr "8080" 查出8080端口被9436进程占用 2.查看PID对应的进程 tasklist|findstr "9436&q ...

  3. java(集合框架)(转)

    前言 集合①只能存放对象,存放基本类型会自动转成对应的对象②可以存放不同类型的对象(如果不使用泛型的话),且不限数量③集合中存放的只是对象的引用 集合详解 集合-1.png 集合-2.png   It ...

  4. Syntax error: "(" unexpected shell里面的报错解决

    author:headsen  chen date : 2019-08-08  11:11:38 notice : 个人原创 Ubuntu上运行shell脚本总是报下面这个错误,在centos下面或者 ...

  5. pytorch设置多GPU运行的方法

    1.DataParallel layers (multi-GPU, distributed) 1)DataParallel CLASS torch.nn.DataParallel(module, de ...

  6. WPF 问题 PresentationCore.dll!System.Windows.Media.Composition.DUCE.Channel.SyncFlush() 分析

    错误信息: 没有足够的内存继续执行程序 在 System.Windows.Media.Composition.DUCE.Channel.SyncFlush() 在 System.Windows.Int ...

  7. 一些有用的github收藏(持续更新中...)

    1.facebook的c++开源库folly(Facebook open source library)介绍 https://github.com/facebook/folly 2.pprint 一个 ...

  8. gitlab 配置到jenkins

    直接把公匙配到了gitlab,然后填入jenkins https://blog.csdn.net/zhufengyan521521/article/details/81219193  配置全局凭据 h ...

  9. CNN中计算量FLOPs的计算

    1.FLOPs的概念:全称是floating point operations per second,意指每秒浮点运算次数,即用来衡量硬件的计算性能:在CNN中用来指浮点运算次数: 2.计算过程: 如 ...

  10. 【k8s 硬盘监控】prometheus grafana

    设置监控哪块盘: https://www.bountysource.com/issues/50160777-disk-space-usage-depcited-in-grafana-correct h ...