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 8HintThe 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据说是一道DP题,但不用DP,通过分析情况,也能计算答案,先选定第i个元素,然后判断i之后的元素大小写是否和i一样,直到遇到不一样的元素,然后做差,若是两个及以上就要用capsblocks,但是当最后一个元素是小写,则需要直接按capsblocks,当全部元素循环结束,要判断capsblocks是否是开启状态,若是,必须要关上,(┬_┬)一开始忽略了小写也可以用shift来写=.=#include <iostream> #include <cstdio> #include <cstring> #define maxn 110 char c[maxn]; int flag; using namespace std; //只记得大写可以用shift //不要忽略小写也可以=.= int capital(char c1) { if(c1 >= 'A' && c1 <= 'Z') return 1; return 0; } int main() { int t; while(~scanf("%d", &t)) { int f = 1; while(t--) { if(f) { getchar(); f = 0; } //memset(f1, 0, sizeof(f1)); scanf("%s", c); int len = strlen(c); int num = len;//每个字母都要按一下 int t; flag = 0; int i, j; for(i = 0; i < len; i++) { t = capital(c[i]); for(j = i + 1; j < len; j++) { if(t != capital(c[j])) break; //判断同为大写或小写的长度 } //元素是大写,并且没有用capslock,只有i元素是大写=.=所以要按下shift; if(t && !flag && j - i < 2) { num += 1; } //元素是大写,i后面含有至少一个是大写,所以锁定capslock else if(t && !flag && j - i >= 2) { num += 1; flag = 1; } //元素是小写,但是现在锁定着capsblock,且元素个数为1,所以要按下shift; //但假如i是最后一个元素,那么按下的就因该是capsblocks else if(!t && flag && j - i < 2 && i == len - 1) { num += 1; flag = 0; } else if(!t && flag && j - i < 2) { num += 1; } //元素是小写,但锁定着capsblock,且元素个数大于等于2,那么再按下capsblock else if(!t && flag && j - i >= 2) { num += 1; flag = 0; } //在第j的元素时,与前面元素大小写不一样,但是要i++,所以i = j - 1; i = j - 1; } if(flag) num += 1; printf("%d\n", num); } } return 0; }