题目链接

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

Hint

The string “Pirates”, can type this way, Shift, p, i, r, a, t, e, s, the answer is 8.

The string “HDUacm”, can type this way, Caps lock, h, d, u, Caps lock, a, c, m, the answer is 8

The string "HDUACM", can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8

题意:

给定一串字母,问最少需要按多少次键盘将这些字母打印到屏幕上,键盘最开始的时候是小写锁定的,最后也要回归到小写锁定。

分析:

在小(大)写锁定下输入小(大)写字母肯定不需要进行考虑了,关键就是在小(大)写锁定下输入大(小)写字母,如果连续的只有一个的话,就用Shift键只需要多按一次,如果两个或者两个以上的话,就转换大小写锁定。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ch[109];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf(" %s",ch);
int k=strlen(ch);
int i=0;
int ans=0;
int flag=0;///Caps lock键有没有被按下,0表示小写锁定,1表示大写锁定
while(i<k)
{
if(flag==0&&ch[i]>='a'&&ch[i]<='z')///小写锁定的情况下是小写字母
{
i++;
ans++;
}
if(ch[i]>='A'&&ch[i]<='Z'&&flag==1)///大写锁定的情况下是大写字母
{
i++;
ans++;
}
if(flag==0&&ch[i]>='A'&&ch[i]<='Z')///小写锁定的情况下是大写字母
{
if(i+1<k)
{
if(ch[i+1]>='A'&&ch[i+1]<='Z')///下一个还是大写,就可以换成大写锁定
{
flag=1;
ans+=2; }
else///否则直接用Shift键就行
{
ans+=2; }
}
else///已经是最后一个字母了,肯定就是用shift键
ans+=2;
i++; }
if(flag==1&&ch[i]>='a'&&ch[i]<='z')///大写锁定的情况下是小写字母
{
if(i+1<k)
{
if(ch[i+1]>='a'&&ch[i+1]<='z')///下一个还是小写,就可以换成小写锁定
{
flag=0;
ans+=2;
}
else///下一个字母大写的话,用shift键
{
ans+=2;
}
}
else///已经是左后一个字母了
{
ans+=2;
flag=0;///肯定就回归小写锁定了
}
i++;
}
}
if(flag==1)///最后害的看一下是不是大写锁定
ans+=1;
printf("%d\n",ans);
}
return 0;
}

HDU 2577 How to Type (字符串处理)的更多相关文章

  1. HDU 2577 How to Type (字符串处理)

    题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...

  2. HDU 2577 How to Type (线性dp)

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU 2577 How to Type(dp题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...

  4. HDU 2577 How to Type DP也可以模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=2577 大意: 大家都打过字吧,现在有个有趣的问题:给你一串字符串,有大写有小写,要求你按键次数最少来输出它,输出 ...

  5. hdu 2577 How to Type(DP)

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. HDU 2577 How to Type【DP】

    题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就 ...

  7. hdu 2577 How to Type(dp)

    Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...

  8. HDU 2577 How to Type (DP,经典)

    题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...

  9. DP问题(1) : hdu 2577

    题目转自hdu 2577,题目传送门 题目大意: 现给你n个区分大小写的字符串,大小写可用Caps Lock和shift切换(学过计算机的都知道) 但是有一点需要注意(shift是切换,若现在是大写锁 ...

随机推荐

  1. LintCode-68.二叉树的后序遍历

    二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [3,2,1] 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code / ...

  2. IIS7,IIS7.5 URL重写模块工具

    URL 重写模块 2.0 提供基于规则的重写机制,可在 Web 服务器处理请求的 URL 之前对其进行更改,以及在向 HTTP 客户端提供响应内容之前修改响应内容. 注意:使用环境为IIS7.0(x6 ...

  3. 3dContactPointAnnotationTool开发日志(三)

      今天的目的是把obj文件导到场景里.具体将制定路径的obj文件导进去我用的是这个方法.导进去后呈现的是一个黑色的影子.   导入后还想实现一下缩放功能,请看这个方法.缩放实现起来也很简单.   光 ...

  4. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  5. linux解压zip

    用 unzip 的先安装 yum install -y unzip #unzip file.zip -d /root  -d指解压路径 ,不写的话默认当前目录

  6. P3385 【模板】负环

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...

  7. 透彻掌握Promise的使用,读这篇就够了

    透彻掌握Promise的使用,读这篇就够了 Promise的重要性我认为我没有必要多讲,概括起来说就是必须得掌握,而且还要掌握透彻.这篇文章的开头,主要跟大家分析一下,为什么会有Promise出现. ...

  8. POJ2891:Strange Way to Express Integers——题解

    http://poj.org/problem?id=2891 题目大意: k个不同的正整数a1,a2,...,ak.对于一些非负m,满足除以每个ai(1≤i≤k)得到余数ri.求出最小的m. 输入和输 ...

  9. React Patterns

    Contents Stateless function JSX spread attributes Destructuring arguments Conditional rendering Chil ...

  10. [BZOJ1106/POI2007]Tet立方体大作战

    Description 一个叫做立方体大作战的游戏风靡整个Byteotia.这个游戏的规则是相当复杂的,所以我们只介绍他的简单规则:给定玩家一个有2n个元素的栈,元素一个叠一个地放置.这些元素拥有n个 ...