HDU 2617 Happy 2009(字符串)

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

时间: 2024-10-26 07:04:11

HDU 2617 Happy 2009(字符串)的相关文章

HDU 2617 Happy 2009

Happy 2009 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2757    Accepted Submission(s): 922 Problem Description No matter you know me or not. Bless you happy in 2009. Input The input contains

HDU 2072 单词数 --- 字符串处理

/* HDU 2072 单词数 --- 字符串处理 */ #include <cstdio> //C语言改成stdio.h即可 #include <cstring> //C语言改成string.h即可 const int maxn = 85; int main() { char *head1, *head2; char a[maxn]; char b[maxn][maxn]; int i, k, len, cnt1, cnt2; while (gets(a)){ //遇到字符串&q

hdu 1062 Text Reverse 字符串反转

Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18240    Accepted Submission(s): 6900 Problem Description Ignatius likes to write words in reverse way. Given a single line of text

hdu 6068 Classic Quotation(字符串hash)

题目链接:hdu 6068 Classic Quotation 题意: 给你两个字符串S和T,现在有q个询问,每个询问给出一个l,r,问S[1..i] 和 S[j..n](1≤i≤L,R≤j≤n)中有多少个T,求出全部的总和. 题解: Claris的官方题解: 这里我用hash代替了kmp的作用. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4

HDOJ/HDU 1062 Text Reverse(字符串翻转~)

Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first line of the inpu

【HDU 5510 Bazinga】字符串

2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不超过2000个字符的字符串.求"存在秩小于 i 且不是 i 的子串"的最大的 i (1<= i <= n). 数据范围:n [1, 500],T组输入,T [1, 50] 思路:从1到n-1枚举每个字符串str[i],判断是否有 j < i 使得str[j]不是str[i

HDU 4545-魔法串-字符串

魔法串 问题描述 : 小明和他的好朋友小西在玩一个新的游戏,由小西给出一个由小写字母构成的字符串,小明给出另一个比小西更长的字符串,也由小写字母组成,如果能通过魔法转换使小明的串和小西的变成同一个,那么他们两个人都会很开心.这里魔法指的是小明的串可以任意删掉某个字符,或者把某些字符对照字符变化表变化. 如: 小西的串是 abba; 小明的串是 addba; 字符变化表 d b (表示d能转换成b). 那么小明可以通过删掉第一个d,然后将第二个d转换成b将串变成abba. 现在请你帮忙判断:他们能

hdu 2594 java实现字符串KMP算法

Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? Marge: OK. Homer: So I tak

HDU 1274 递归拼接字符串

题目大意: 根据所给的数字,表示其相连的字符的输出个数,或是下一个括号中的所有字符的输出个数 每一个相互对应的 '(' 和 ')' 中的所有字母均作为一组数据处理 在每一次dfs过程中都处理好这样一个对应组中间的字符 再根据前面所带的数字k 循环k次输出自己要的数据 每次输出一个字母,因为无论多复杂的字符串都能够转化成最后只带一个数字和一个字母连接的形式,第38行代码有较详细解释 1 #include <cstring> 2 #include <cstdio> 3 4 using