码迷,mamicode.com
首页 > 移动开发 > 详细

HDU 2617 Happy 2009(字符串)

时间:2014-06-19 11:54:54      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:字符串处理   hdu   2617   iaccepted   

Happy 2009

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2394    Accepted Submission(s): 802


Problem Description
No matter you know me or not. Bless you happy in 2009.
 

Input
The input contains multiple test cases.
Each test case included one string. There are made up of ‘a’-‘z’ or blank. The length of string will not large than 10000. 
 

Output
For each test case tell me how many times “happy” can be constructed by using the string. Forbid to change the position of the characters in the string. The answer will small than 1000.
 

Sample Input
hopppayppy happy happ acm y hahappyppy
 

Sample Output
2 1 2
 


很简单的字符串,主要是简单练下手,1A。


#include <iostream>
#include <string>
#include <map>

using namespace std;

map<string,int> smp;
int ans;

void init(){
	smp["h"] = 0;
	smp["ha"] = 0;
	smp["hap"] = 0;
	smp["happ"] = 0;
	ans = 0;
}

inline bool yes(char c){
	if (c == 'a' || c == 'h' || c == 'p' || c == 'y')return true;
	return false;
}

inline void add(char c){
	if (c == 'h'){
		++smp["h"];
	}
	else if (c == 'a' && smp["h"]>0){
		--smp["h"];
		++smp["ha"];
	}
	else if (c == 'p'){
		if (smp["hap"] > 0){
			--smp["hap"];
			++smp["happ"];
		}
		else if (smp["ha"] > 0){
			--smp["ha"];
			++smp["hap"];
		}
		
	}
	else if (c == 'y' && smp["happ"] > 0){
		++ans;
		--smp["happ"];
	}
}

void solve(string str){
	int len = str.length();

	for (int i = 0; i < len; ++i){
		if (yes(str[i])){
			add(str[i]);
		}
	}
}

int main(){
	string str;

	while (getline(cin, str)){
		init();
		solve(str);

		cout << ans << endl;
	}
	return 0;
}


HDU 2617 Happy 2009(字符串),布布扣,bubuko.com

HDU 2617 Happy 2009(字符串)

标签:字符串处理   hdu   2617   iaccepted   

原文地址:http://blog.csdn.net/iaccepted/article/details/30108365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!