A - 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.

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

        
 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cstring>

using namespace std;

const int maxn = 1e4 + 10;
const int INF = 0x3f3f3f3f;
int t, n, m, ans = 0, sum;
int a[maxn][maxn], dpa[maxn], dpb[maxn];
int vis[maxn][maxn];
int dir[][2] = {{0, -1}, {0, -2}, {0, 1}, {0, 2}, {1, 0}, {2, 0}, {-1, 0}, {-2, 0} };
int main() {
    scanf("%d", &t);
    for(int i = 1; i <= t; i++) {
        char s[105];
        scanf("%s", s + 1);
        int len = strlen(s + 1) + 1;
        dpa[0] = 0; // Caps off
        dpb[0] = 1; // Caps on
        for(int j = 1; j < len; j++) {
            if(s[j] >= 'a' && s[j] <= 'z') {
                        // sc             // turn off Caps + sc
                dpa[j] = min(dpa[j - 1] + 1, dpb[j - 1] + 2);
                        // sc + turn on Caps // shift + sc 
                dpb[j] = min(dpa[j - 1] + 2, dpb[j - 1] + 2);
            } else {
                        // shift + sc       // sc + turn off Caps
                dpa[j] = min(dpa[j - 1] + 2, dpb[j - 1] + 2);
                        // turn on Caps + sc // sc
                dpb[j] = min(dpa[j - 1] + 2, dpb[j - 1] + 1);
            }
        }
        printf("%d\n", min(dpb[len - 1] + 1, dpa[len - 1]));
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qwqwdqwqwe/article/details/79857462