Character Input is incorrect. At first, the SHIFT + character input is not considered as the character in the opposite state of the current case lock status. Only two steps are required.
State transition equation: dp1 [I] indicates the number of steps required for the capitalized State after the I character is entered. dp2 [I] indicates the number of steps required for the lowercase state after the I character is entered.
If the I character is lower case:
Dp1 [I] = min (dp1 [I-1] + 2, dp2 [I-1] + 2 );
Dp2 [I] = min (dp1 [I-1] + 2, dp2 [I-1] + 1 );
If the I-th character is uppercase:
Dp1 [I] = min (dp1 [I-1] + 1, dp2 [I-1] + 2 );
Dp2 [I] = min (dp1 [I-1] + 2, dp2 [I-1] + 2 );
#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <vector>#include <cstring>#include <algorithm>using namespace std;char s[111];int dp1[111];int dp2[111];int main(){int t;scanf("%d",&t);while(t--){scanf("%s",s);int len=strlen(s);if(s[0]>='A'&&s[0]<='Z'){dp1[0]=2;dp2[0]=2;}else{dp1[0]=2;dp2[0]=1;}for(int i=1;i<len;i++){if(s[i]>='A'&&s[i]<='Z'){dp1[i]=min(dp1[i-1]+1,dp2[i-1]+2);dp2[i]=min(dp1[i-1]+2,dp2[i-1]+2);}else{dp1[i]=min(dp1[i-1]+2,dp2[i-1]+2);dp2[i]=min(dp1[i-1]+2,dp2[i-1]+1);}}cout<<dp2[len-1]<<endl;}return 0;}
HDU-2577 how to type DP