HDU 2577 How to Type (linear dp)
How to Type
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4616 Accepted Submission (s): 2084Problem Description Pirates have finished developing the typing software. he called Cathy to test his typing software. she is good at thinking. after testing for several days, she finds that if she types a string by some ways, she will type the key at least. but she has a bad habit that if the caps lock is on, she must turn off it, after she finishes typing. now she wants to know the smallest times of typing the key to finish typing a string. input The first line is an integer t (t <= 100), which is the number of test case in the input file. for each test case, there is only one string which consists of lowercase letter and upper case letter. the length of the string is at Mo st 100. output For each test case, you must output the smallest times of typing the key to finish typing this string. sample Input
3PiratesHDUacmHDUACM
Sample Output
888HintThe 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 8The string HDUACM, can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8
Author Dellenge Source HDU 2009-5 Programming Contest
Question: The minimum number of buttons that can be played on the keyboard for a string consisting of upper and lower Cases
Key-pressing rules: For each lowercase letter, you can add shift to make It capitalized. Note that shift cannot always be pressed, and you can also enable the Caps lock key of the write lock, after you press this key, the input is converted to uppercase. Then press shift to make it lowercase (the mac keyboard does not look like this). Pay attention to the write lock and close it at last.
Question Analysis: after knowing the question, dp [I] [0] and dp [I] [1] indicate the I character write lock respectively, the minimum number of times to be used when the server is on, there are four situations
If the I-th character is lowercase and the big write lock is enabled at this time, the first one must be uppercase, because if the first one is lowercase and the current one is lowercase, I obviously do not need to open the big write lock.
Dp [I] [1] = dp [I-1] [1] + 2
If the I character is lowercase and the write lock is not enabled at this time, simply press
Dp [I] [0] = dp [I-1] [0] + 1
If the I character is capitalized and the write lock is enabled at this time, 1 is added for the first write lock and 2 is added for the previous write lock, indicating that the lock is unlocked.
Dp [I] [1] = min (dp [I-1] [1] + 1, dp [I-1] [0] + 2)
If the I character is in uppercase and the write lock is not enabled at this time, add 2 (shift) to the case where the previous write lock is not enabled) add the minimum value of 2 to the previous write lock to indicate the lock is closed.
Dp [I] [0] = min (dp [I-1] [0] + 2, dp [I-1] [1] + 2)
#include
#include
#include using namespace std;int const MAX = 105;char s[MAX];int dp[MAX][2];bool judge(char ch){ if(ch >= 'A' && ch <= 'Z') return true; return false;}int main(){ int T; scanf(%d, &T); while(T--) { scanf(%s, s + 1); int len = strlen(s + 1); memset(dp,0, sizeof(dp)); dp[0][1] = 1; for(int i = 1; i <= len; i++) { if(judge(s[i])) { dp[i][1] = min(dp[i - 1][1] + 1, dp[i - 1][0] + 2); dp[i][0] = min(dp[i - 1][0] + 2, dp[i - 1][1] + 2); } else { dp[i][1] = dp[i - 1][1] + 2; dp[i][0] = dp[i - 1][0] + 1; } } printf(%d, min(dp[len][0], dp[len][1] + 1)); }}