HDU 2577 How to Type DP can also be simulated

Source: Internet
Author: User

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
    
     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.