【HDOJ】2577 How to Type
DP。
1 /* 2577 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 125 7 #define isUpper(ch) (ch>='A' && ch<='Z') 8 9 char s[MAXN]; 10 int dp[MAXN][2]; 11 12 int min(int a, int b) { 13 return a<b? a:b; 14 } 15 16 int main() { 17 int t, l; 18 int i, j, k; 19 int ans; 20 21 #ifndef ONLINE_JUDGE 22 freopen("data.in", "r", stdin); 23 //freopen("data.out", "w", stdout); 24 #endif 25 26 scanf("%d", &t); 27 while (t--) { 28 scanf("%s", s+1); 29 memset(dp, 0, sizeof(dp)); 30 dp[0][0] = 0; 31 dp[0][1] = 1; 32 for (i=1; s[i]; ++i) { 33 if ( isUpper(s[i]) ) { 34 dp[i][0] = 1 + min(dp[i-1][1]+2, dp[i-1][0]+1); 35 dp[i][1] = 1 + min(dp[i-1][1], dp[i-1][0]+1); 36 } else { 37 dp[i][0] = 1 + min(dp[i-1][0], dp[i-1][1]+1); 38 dp[i][1] = 1 + min(dp[i-1][0]+2, dp[i-1][1]+1); 39 } 40 } 41 ans = min(dp[i-1][0], dp[i-1][1]+1); 42 printf("%d\n", ans); 43 } 44 45 return 0; 46 }