Skip to content

Commit 4fc2f16

Browse files
committed
srm 627 div 2 500
1 parent 91b8e31 commit 4fc2f16

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

HappyLetterDiv2.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <climits>
9+
using namespace std;
10+
typedef vector<int> vi;
11+
typedef vector<vi> vvi;
12+
typedef vector<string> vs;
13+
typedef vector<vs> vvs;
14+
15+
class HappyLetterDiv2 {
16+
public:
17+
char getHappyLetter(string L)
18+
{
19+
int m = L.size();
20+
int c[250];
21+
memset(c, 0, sizeof(c));
22+
for(char cc : L) c[cc]++;
23+
char r = '.';
24+
cout<<m<<endl;
25+
for(char cc : L) cout<<char(cc)<<" "<<c[cc]<<endl;
26+
for(char cc : L)
27+
if(c[cc] > m/2) r = cc;
28+
29+
return r;
30+
}
31+
32+
// BEGIN CUT HERE
33+
public:
34+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
35+
private:
36+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
37+
void verify_case(int Case, const char &Expected, const char &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
38+
void test_case_0() { string Arg0 = "aacaaa"; char Arg1 = 'a'; verify_case(0, Arg1, getHappyLetter(Arg0)); }
39+
void test_case_1() { string Arg0 = "dcdjx"; char Arg1 = '.'; verify_case(1, Arg1, getHappyLetter(Arg0)); }
40+
void test_case_2() { string Arg0 = "bcbbbbba"; char Arg1 = 'b'; verify_case(2, Arg1, getHappyLetter(Arg0)); }
41+
void test_case_3() { string Arg0 = "aabc"; char Arg1 = '.'; verify_case(3, Arg1, getHappyLetter(Arg0)); }
42+
43+
// END CUT HERE
44+
45+
};
46+
47+
// BEGIN CUT HERE
48+
int main()
49+
{
50+
HappyLetterDiv2 ___test;
51+
___test.run_test(-1);
52+
}
53+
// END CUT HERE

HappyLetterDiv2.txt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
PROBLEM STATEMENT
2+
The Happy Letter game is played as follows:
3+
At the beginning, several players enter the field.
4+
Each player has a lowercase English letter on their back.
5+
The game is played in turns.
6+
In each turn, you select two players with different letters, and both selected players leave the field.
7+
The game ends once it is impossible to take another turn.
8+
9+
If there are some players left in the field at the end of the game, they must all have the same letter.
10+
That letter is called the winning letter.
11+
If there are no players left in the field at the end of the game, there is no winning letter.
12+
13+
In general, there may be many different ways how to play the game, and these may lead to different outcomes of the game.
14+
However, sometimes we can look at the initial players and determine that each possible sequence of turns leads to the same winning letter.
15+
That winning letter is then called a happy letter.
16+
17+
You are given a string letters.
18+
The characters in letters are the characters carried by the players at the beginning of the game.
19+
Return a string with one character: either the happy letter (if it exists), or a period ('.') if there is no happy letter.
20+
21+
22+
DEFINITION
23+
Class:HappyLetterDiv2
24+
Method:getHappyLetter
25+
Parameters:string
26+
Returns:char
27+
Method signature:char getHappyLetter(string letters)
28+
29+
30+
CONSTRAINTS
31+
-letters will contain between 1 and 50 elements.
32+
-Each element of letters will be a lowercase English letter ('a'-'z').
33+
34+
35+
EXAMPLES
36+
37+
0)
38+
"aacaaa"
39+
40+
Returns: 'a'
41+
42+
In the first turn we send away the player with the 'c' and one of the players with an 'a'. All remaining players have an 'a', hence the game ends and 'a' is the winning letter.
43+
44+
45+
1)
46+
"dcdjx"
47+
48+
Returns: '.'
49+
50+
Each of the letters 'c', 'd', 'j', and 'x' can be the winning letter. Thus there is no happy letter.
51+
52+
53+
2)
54+
"bcbbbbba"
55+
56+
Returns: 'b'
57+
58+
59+
60+
3)
61+
"aabc"
62+
63+
Returns: '.'
64+
65+
Here, only 'a' can be the winning letter. However, it is also possible that there is no winning letter at the end of the game. Therefore 'a' is not considered to be the happy letter.
66+

0 commit comments

Comments
 (0)