ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1585. Penguins

Why do I get WA 1???
Posted by Bebop 22 Nov 2020 09:33
#include <iostream>
#include <string>
#include <vector>
#include <set>

const std::string &solution(const std::vector<std::string> &penguins);

int main() {
    int n;
    std::cin >> n;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::vector<std::string> penguins;
    penguins.reserve(n);
    for (int i = 0; i < n; ++i) {
        std::string penguin;
        std::getline(std::cin, penguin);
        penguins.emplace_back(std::move(penguin));
    }
    std::cout << solution(penguins) << std::endl;
    return 0;
}

const std::string &solution(const std::vector<std::string> &penguins) {
    std::set<std::string> penguinSet(penguins.begin(), penguins.end());
    auto setIterator = penguinSet.begin();
    std::set<std::string>::iterator mostNumerous;
    int maxCount = 0;
    while (setIterator != penguinSet.end()) {
        int count = 0;
        for (const auto &penguin : penguins) {
            if (*setIterator == penguin) {
                count++;
            }
        }
        if (count > maxCount) {
            maxCount = count;
            mostNumerous = setIterator;
        }
        setIterator++;
    }
    return *mostNumerous;
}

I tested locally and it works...