PKU 2643 Election

  • シミュレーション
  • 連想コンテナを使うのが定石
int main() {
	int n;
	cin >> n;
	string line;
	getline(cin, line);

	map<string, string> candidateToParty;
	for (int i = 0; i < n; ++i) {
		string candidate, party;
		getline(cin, candidate);
		getline(cin, party);
		candidateToParty[candidate] = party;
	}

	int m;
	cin >> m;
	getline(cin, line);
	map<string, int> candidateToVote;
	for (int i = 0; i < m; ++i) {
		string candidate;
		getline(cin, candidate);
		++candidateToVote[candidate];
	}

	string bestParty;
	int bestVote = 0;
	int numberOfParties = 0;
	for (map<string, int>::iterator it = candidateToVote.begin(), itEnd = candidateToVote.end(); it != itEnd; ++it) {
		if (bestVote < it->second) {
			bestVote = it->second;
			bestParty = candidateToParty[it->first];
			numberOfParties = 1;
		} else if (bestVote == it->second) {
			++numberOfParties;
		}
	}

	if (numberOfParties == 1) {
		cout << bestParty << endl;
	} else {
		cout << "tie" << endl;
	}
}