PROBLEM LINK:
Practice
Contest, div. 1
Contest, div. 2
Author: Kerim Kochekov
Tester: Teja Vardhan Reddy
Editorialist: Oleksandr Kulkov
DIFFICULTY:
CAKEWALK
PREREQUISITES:
None
PROBLEM:
You have N strings S_1, \dots, S_N. Their letters are all mixed up. What is the maximum amount of words codechef
that you may make up of these letters?
EXPLANATION:
To complete the word codechef
you need two letters e
, two letters c
, one letter o
, one letter h
, one letter d
and one letter f
. Let’s denote number of letter x in the set as \#_x. Under such notion the answer can be written as ans=\min(\left\lfloor\#_e/2\right\rfloor,\left\lfloor\#_c/2\right\rfloor, \#_h, \#_f,\#_o,\#_d). Indeed, each of arguments here make an upper bound on number of possible words codechef
and on the other hand you always may take all needed letters to get exactly ans codechef
’s. Possible code:
int N;
cin >> N;
vector<string> S(N);
string s;
for(int i = 0; i < N; i++) {
cin >> S[i];
s += S[i];
}
int nc = count(begin(s), end(s), 'c');
int no = count(begin(s), end(s), 'o');
int nd = count(begin(s), end(s), 'd');
int ne = count(begin(s), end(s), 'e');
int nh = count(begin(s), end(s), 'h');
int nf = count(begin(s), end(s), 'f');
cout << min({ne / 2, nc / 2, nh, nf, no, nd}) << endl;
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.
Editorialist’s solution can be found here.