Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2577
Note:
Everyone has typed it. Now there is an interesting question: I want to give you a string with uppercase letters and lowercase letters. You need to output at least the number of buttons and the number of buttons.
Uppercase letters can be obtained through caps lock or shift, and lowercase letters are also obtained.
However, the starting status and the final caps lock are not locked.
Calculate the minimum number of buttons.
Ideas:
The DP method is not used:
Use the caps lock key for consecutive upper or lower case cases, otherwise shift is used.
# Include
# Include
Const int MAXN = 110; int main () {int T; scanf ("% d", & T); while (T --) {char a [MAXN]; scanf ("% s", a); int len = strlen (a); bool capslock = false; int cnt = 0; for (int I = 0; I
= 'A' & A [I] <= 'Z') {if (capslock = false) cnt ++; // shift or capslockif (a [I + 1]> = 'A' & A [I + 1] <= 'Z') capslock = true; cnt ++ ;} else {if (capslock = true) cnt ++; // shift or capslockif (a [I + 1]> = 'A' & a [I + 1] <= 'Z' | a [I + 1] = '\ 0 ') // a [I + 1] = '\ 0' must be added, which is the reason for simulating wa capslock = false; cnt ++ ;}} if (capslock) cnt ++; printf ("% d \ n", cnt);} return 0 ;}
DP Method
Set lock [I] To locked after I is knocked, and notlock [I] To unlocked after I is knocked
Case Sensitive:
If a [I] is in uppercase:
Lock [I] = min (lock [I-1] + 1, notlock [I-1] + 2 );
Notlock [I] = min (lock [I-1] + 2, notlock [I-1] + 2 );
Otherwise:
Lock [I] = min (lock [I-1] + 2, notlock [I-1] + 2 );
Notlock [I] = min (lock [I-1] + 2, notlock [I-1] + 1 );
As for how it comes, you just need to tap on the keyboard.
There is also the final state to become unlocked ..
# Include
# Include
# Include
# Define deusing namespace std; const int MAXN = 110; int main () {int T; scanf ("% d", & T); while (T --) {char a [MAXN]; int lock [MAXN] = {0}; int notlock [MAXN] = {0}; scanf ("% s", ); int len = strlen (a); if (isupper (a [0]) {lock [0] = 2; notlock [0] = 2 ;} else {lock [0] = 2; notlock [0] = 1 ;}for (int I = 1; I