HDU-2577 How to Type (recursive, DP) (water) 2017 winter vacation training

Question meaning: Given a string s consisting of upper and lower case letters, the initial state of your keyboard is lower case, you can press shift to switch between upper and lower case, or press Caps-Lock to switch between upper and lower case, and finally restore the keyboard to lower case , find the minimum number of keystrokes to type this string

Data range: |s| < 100 , t <= 100

Idea: Set dp[i][j] as the first i letter and the minimum number of keystrokes when the status is uppercase/lowercase

Each input can choose two ways to switch case

It is easy to know that when s[i] is lowercase, the transition is dp[i][0] = min(dp[i-1][0]+1, dp[i-1][1]+2) and so on

The final answer is the lesser of dp[n][0] and dp[n][1]+1

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cctype>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int mx = 110;
 8 char s[mx];
 9 int dp[mx][2];
10 
11 int main(){
12     int t;
13     scanf("%d", &t);
14     while (t--){
15         memset(dp, 0, sizeof dp);
16         scanf("%s", s+1);
17         int len = strlen(s+1);
18         dp[0][0] = 0;
19         dp[0][1] = 1;
20         for (int i = 1; i <= len; i++){
21             if (islower(s[i])){
22                 dp[i][0] = min(dp[i-1][0]+1, dp[i-1][1]+2);
23                 dp[i][1] = min(dp[i-1][0]+2, dp[i-1][1]+2);
24             }
25             else {
26                 dp[i][0] = min(dp[i-1][0]+2, dp[i-1][1]+2);
27                 dp[i][1] = min(dp[i-1][0]+2, dp[i-1][1]+1);
28             }
29         }
30         printf("%d\n", min(dp[len][0], dp[len][1]+1));
31     }
32     return 0;
33 }

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326661985&siteId=291194637