// G. Hagopian doing PPP4 exercise 17

/*
17. Write a program that finds the min, max, and mode of a 
sequence of strings.
*/

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;

string min(vector<string> vs);
string max(vector<string> vs);
string mode(vector<string> vs);
int main() {
	//get sequence of strings
	string s;
	cout << "Enter a sequence of strings (\"quit\" to quit): ";
	vector<string> vs;
	while (cin >> s && s != "quit") {
		vs.push_back(s);
	}
	cout << "You entered \"";
	for (auto str : vs) cout << str << " ";
	cout << endl;
	//find min
	cout << "The minimum string is " << min(vs) << endl;
	//find max
	cout << "The maximum string is " << max(vs) << endl;
	//find mode
	cout << "The mode is " << mode(vs) << endl;
}

string min(vector<string> vs) {
	string minStr = vs[0];
	for (string s : vs) if (s < minStr) minStr = s;
	return minStr;
}
string max(vector<string> vs) {
	string maxStr = vs[0];
	for (string s : vs) if (s > maxStr) maxStr = s;
	return maxStr;
}
string mode(vector<string> vs) {
	vector<pair<string, int>> freqs;
	freqs.push_back(make_pair(vs[0], 1));
	bool found = false;
	for (int i = 1; i < vs.size(); ++i) {
		for (int j = 0; j < freqs.size() && !found; ++j) {
			if (freqs[j].first == vs[i]) {
				found = true;
				++freqs[j].second;
			}
		}
		if (!found) freqs.push_back(make_pair(vs[i], 1));
		found = false;
	}
	int max_freq = 1;
	string mode = vs[0];
	for (pair<string, int> p : freqs) {
		if (p.second > max_freq) {
			max_freq = p.second;
			mode = p.first;
		}
	}
	for (pair<string, int> p : freqs) cout << p.first << ", " << p.second << endl;
	return mode;
}
