现在的位置: 首页 > 综合 > 正文

hdu2577简单DP

2014年02月19日 ⁄ 综合 ⁄ 共 1380字 ⁄ 字号 评论关闭
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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=101;
int open_lock[MAX],close_lock[MAX];//分别代表输入当前这个字符并且大写键打开的情况下最少需要按的次数
                                  //大写键没有打开的情况下最少需要按的次数. 

int main(){
	int t;
	string s;
	cin>>t;
	while(t--){
		cin>>s;
		open_lock[0]=1;
		int l=s.size();
		for(int i=0;i<l;++i){
			if(s[i]>='a' && s[i]<='z'){
				open_lock[i+1]=min(open_lock[i]+2,close_lock[i]+2);
				close_lock[i+1]=min(open_lock[i]+2,close_lock[i]+1);
			}
			else{
				open_lock[i+1]=min(open_lock[i]+1,close_lock[i]+2);
				close_lock[i+1]=min(open_lock[i]+2,close_lock[i]+2);
			}
		}
		cout<<min(open_lock[l]+1,close_lock[l])<<endl;
	}
	return 0;
}

抱歉!评论已关闭.