1. 程式人生 > >hdu 2577 How to Type(dp)

hdu 2577 How to Type(dp)

inline ott 鎖定 software 可能 its nds off nis

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; } }

hdu 2577 How to Type(dp)