思路:按照按下大小写切换来DP。。
#include<bits\stdc++.h> using namespace std; char str[105]; int dp[105][2]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",str+1); int len = strlen(str+1); dp[0][0]=0; dp[0][1]=2; for (int i = 1;i<=len;i++) { if(str[i]>='A' && str[i]<='Z') { dp[i][1]=min(dp[i-1][1]+1,dp[i-1][0]+2); dp[i][0]=min(dp[i-1][1]+2,dp[i-1][0]+2); } else { dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2); dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2); } } int ans = min(dp[len][0],dp[len][1]+1); printf("%d\n",ans); } }
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