HDU 2577 How to Type【DP归纳】(Shift键 与 Caps Lock键 解密)
发布日期:2021-06-29 14:37:38 浏览次数:2 分类:技术文章

本文共 1936 字,大约阅读时间需要 6 分钟。

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

分析

状态转移方程:

在这里插入图片描述
C++库函数isupper(char c) 用来检测当前字符是否为大写字母,如果是大写字母返回true,否则返回false

当前字母是大写字母

下一个状态按下了Caps Lock等于

min(当前按下了Caps Lock的状态+输入一个大写字母,当前没有按下Caps Lock的状态+按下Caps Lock+输入一个大写字母)

下一个状态没有按下Caps Lock等于

min(当前按下了Caps Lock的状态+输入一个大写字母+再次按下Caps Lock(复原),当前没有按下Caps Lock的状态+shift+输入一个大写字母)

当前字母是小写字母

下一个状态按下了Caps Lock等于

min(当前按下了Caps Lock的状态+shift+输入一个小写字母,当前没有按下Caps Lock的状态+输入一个小写字母+按下Caps Lock)

下一个状态没有按下Caps Lock等于

min(当前按下了Caps Lock的状态+再次按下Caps Lock(复原)+输入一个小写字母,当前没有按下Caps Lock的状态+输入一个小写字母)

AC

#include
#include
#include
using namespace std;const int maxn=100+5;int dpc[maxn],dp[maxn];char s[maxn];int main(){
int t; cin>>t; while(t--) {
scanf("%s",s); int len=strlen(s); dpc[0]=1; dp[0]=0; for(int i=0;i

学如逆水行舟,不进则退

转载地址:https://chocolate.blog.csdn.net/article/details/98168178 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ZOJ 1041 Transmitters (凸包的简单应用)
下一篇:HDU 2571 命运 【DP归纳+两种求解方式】

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月26日 05时26分46秒