Happy 2009Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2394 Accepted Submission (s): 802
Problem Descriptionno matter you know me or not. Bless Happy in 2009.
Inputthe input contains multiple test cases.
Each test case included one string. There is made up of ' a '-' Z ' or blank. The length of string would not be large than 10000.
Outputfor Each test case tell me how many times "happy" can be constructed by using the string. Forbid to the position of the characters in the string. The answer would small than 1000.
Sample Input
Hopppayppy Happyhapp ACM Yhahappyppy
Sample Output
212
Very simple string, mainly simple practice, 1 a.
#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;}