Description
Input
Output
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 <cstring> using namespace std; char str[110]; int dp[110][2]; //打i号字母前按下的最小次数,j=0表示caplocks按了,1表示没按 int main(){ int t, l, i; cin >> t; while(t --){ memset(dp, 0, sizeof(dp)); dp[0][0] = 0; dp[0][1] = 1; scanf("%s", str); l = strlen(str); for(i = 0; i <= l-1; i ++){ //一共0到i-1号字母 if(str[i] >= 'A' && str[i] <= 'Z'){ dp[i+1][0] = min(dp[i][0] + 2, dp[i][1] + 2); dp[i+1][1] = min(dp[i][0] + 2, dp[i][1] + 1); } else{ dp[i+1][0] = min(dp[i][0] + 1, dp[i][1] + 2); dp[i+1][1] = min(dp[i][0] + 2, dp[i][1] + 2); } } cout << min(dp[l][0], dp[l][1] + 1) << endl; } return 0; }