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.
3 Pirates HDUacm HDUACM
8
8
8
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<cstring> #include<cstdio> using namespace std; char a[105]; int dx(int i) { if(a[i]>='A'&&a[i]<='Z') return 1; return 0; } int caps; int fun(int i) { if(caps) { if(dx(i)==1) return 1; else if(dx(i)==0&&dx(i+1)==0) { caps=0; return 1; } else if(dx(i)==0&&dx(i+1)==1) { return 2; } else if(dx(i+1)=='\0') { caps=0; return 1; } } else { if(dx(i)==1&&dx(i+1)==1) { caps=1; return 3; } else if(dx(i)==1&&dx(i+1)==0) { return 2; } else { return 1; } } } int main() { int m,n,k,i; scanf("%d",&n); while(n--) { caps=0; scanf("%s",a); m=strlen(a); k=0; a[m]='\n'; for(i=0;i<m;i++) { k+=fun(i); } printf("%d\n",k); } return 0; }