这道题写的好累,虽说是简单的模拟,但是脑子还是不太够用,要不就是这里多加了一,要不就是那里多加了一。调试了出来的数据竟然还有进入‘\0’的,之后初始化了,才解决了这个问题。倒是个教训,不一定字符串到\0后面就没东西了,上一次的字符串是会加到后面的。这道题说是动态规划,但是更多的是判断当前的条件,主要是在capslock到底是开着还是关着。这道题里面有几点要分出来判断:AaAaaaa AaAAA AAAaAA ........至于判断就看代码那个判断那里。因为当中间夹着两个小写字母的时候,capslock和shift的答案是一样的,所以划分的时候只要判断是否是夹着一个小写字母。
提示
The string “Pirates”, can type this way, Shift, p, i, r, a, t, e, s, the answer is 8.
The string “TZCacm”, can type this way, Caps lock, t, z, c, Caps lock, a, c, m, the answer is 8
The string "TZCACM", can type this way Caps lock t, z, c, a, c, m, Caps lock, the answer is 8
How to Type
描述
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.
输入
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.
输出
For each test case, you must output the smallest times of typing the key to finish typing this string.
样例输入
3
Pirates
TZCacm
TZCACM
样例输出
8
8
8
#include<stdio.h>
#include<string.h>
int main()
{
int n;
scanf ("%d", &n);
while (n--)
{
char a[102];
memset(a, 0, sizeof(a));
scanf ("%s", a);
int i = 0, j = 0, k = 0, on = 0, l = strlen(a), flag = 0, res = l;
for (i = 0; i < l; i++)
{
j = 0;
if (a[i]>='A'&&a[i]<='Z')//charge the amount of capitals
{
while (a[i]>='A'&&a[i]<='Z')
{
i++;
j++;
}
}
if (a[i-1]>='A'&&a[i-1]<='Z'&&a[i+1]>='A'&&a[i+1]<='Z')//charge the after count of small letters and it's one
{//这个地方当最后是大写的时候,那个a[i]已经变成‘\0’了,但是这里还是进来了,原因就是我开始上面说的,有了值
flag = 1;
}
if (on)
{//开关开着的情况要好写点
if (flag) res++;
else on = 0;
}
else
{
if (j>1)
{
if (flag) on = 1, res+=3;
else res+=2;//这里之前加错了,加成3 ,不过这倒不是主要的问题
}
else if (j==1)
{
if (a[i+2]>='A'&&a[i+2]<='Z')
{//自然这里当最后一个是大写字母的时候也进来了
if (flag) on = 1, res+=3;
else res++;
}
else
res++;
}
}
flag = 0;
}
printf ("%d\n", res);
}
return 0;
}