hdu 2577 How to Type(dp)

Problem 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 most 100.
 
Output
For each test case, you must output the smallest times of typing the key to finish typing this string.
 
Sample Input
3
Pirates
HDUacm
HDUACM
 
Sample Output
8
8
8
 

题意:

有大写字母也有小写字母,她想尽可能少的敲键盘,可以按大写锁定键,可以按Shift键来进行敲字母

输出最少按键次数,注意,最后大写锁定键灯必须是灭的;

思路:把状态分为大写模式和小写模式进行dp

#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int dp[107][2]; //0表示小写状态 1表示大写状态 
int main(){
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        string s; cin>>s;
        int len=s.size();
        dp[0][0]=0;
        dp[0][1]=1;
        for(int i=1;i<=len;i++){
            if(s[i-1]>='a'&&s[i-1]<='z'){
                dp[i][0]=dp[i-1][0]+1;    //如果是小写字母 自然是从小写状态+1最优 
                dp[i][1]=min(dp[i-1][1]+2,dp[i][0]+1);    //大写状态则是由前一个大写状态用shift
                //或者由小写状态的最优情况转成大写 
            }else{ //同理 
                dp[i][1]=dp[i-1][1]+1;
                dp[i][0]=min(dp[i-1][0]+2,dp[i][1]+1);
            }
        }
        cout<<min(dp[len][0],dp[len][1]+1)<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/wmj6/p/10388968.html