HDU 2577 How to Type (DP)

Source: Internet
Author: User

How to TypeTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4658 Accepted Submission (s): 2110


Problem Descriptionpirates has 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 so if she types a string by some ways, she'll type the key at least. But she had a bad habit that if the caps lock was 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.
Inputthe first line is a integer t (t<=100), which is the number of the "a" in the the input file. For each test case, the There is a string which consists of lowercase letter and upper case letter. The length of the string is at most 100.
Outputfor 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, Shift, p, I, R, a, T, E, S, the answer are 8.The string "HDUACM", can type this WA Y, Caps Lock, H, D, U, CAPS Lock, A, C, M, the answer is 8

The string "HDUACM", can type this on-the-the-same-to-the-----CAPS Lock H, D, U, A, C, M, Caps Lock, the answer is 8

Test instructions is to give a string to each group, asking you how many times you need to hit the string, and then CapsLock keep it closed after you have finished typing. (Note that you can enter only one letter at a time, and use shift to enter lowercase letters when CapsLock is turned on). As a rookie, the following is a look at the analysis of the Great God::This problem can be directly to the continuous (case of the same) letter based on the length of the length of 1 and the length of more than 1 to deal with, after the traversal of the results (complexity O (n)), of course, you can also use DP to do.This gives the DP approach.For each character, turn off status classification for CapsLock before and after inputIf CapsLock is turned off after inputIf the input is closed before, either direct input (lowercase letter), or shift+ the current letter input (capital letter)If the input before opening, either enter the current letter and then press CapsLock (the current letter is uppercase), or first press CapsLock and then enter the letter (the current letter lowercase), are pressed two timesIf CapsLock is turned on after inputIf open before input, either direct input (uppercase letter), or use shift+ current letter input (lowercase letter)If the input before closing, either enter the current letter and then press CapsLock (the current letter is lowercase), or press CapsLock and then enter the letter (the current letter capitalized), are pressed two timesThus the state transfer equation can be obtained.We use dp[i][0] to represent the minimum number of times required to enter the first letter and CapsLock closed, and dp[i][1] to represent the minimum number of times required to enter the letter of the I and capslock open, and to define the function int check (char ch), If CH is uppercase, 1 is returned, otherwise 0 is returned.Dp[i][0]=min (Dp[i-1][0]+check (s[i]) +1,dp[i-1][1]+2)Dp[i][1]=min (Dp[i-1][0]+2,dp[i-1][1]+2-check (S[i]))Special, when the i==0Dp[0][0]=check (S[i]) +1dp[0][1]=2More practice,,,, 2015,7,21

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;char s[110];int dp[110 ][2];int Check (char a) {if (a>= ' a ' &&a<= ' Z ') return 1;return 0;} int main () {int t,i,len;scanf ("%d", &t), while (t--) {scanf ("%s", s), Len=strlen (s), and for (i=0;i<len;i++) {if (i==0) { Dp[i][0]=check (S[0]) +1;dp[i][1]=2;} Else{dp[i][0]=min (Dp[i-1][0]+check (s[i]) +1,dp[i-1][1]+2);DP [I][1]=min (Dp[i-1][1]+2-check (S[i]), dp[i-1][0]+2);}} printf ("%d\n", Dp[len-1][0]);} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2577 How to Type (DP)

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.