From fc0a6bacc77257eb1b1ebe75809455b12802bea8 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Tue, 30 Sep 2014 15:54:56 +0200 Subject: [PATCH] uwc: cpp implementation --- uwc.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 uwc.cpp diff --git a/uwc.cpp b/uwc.cpp new file mode 100644 index 0000000..cf40b54 --- /dev/null +++ b/uwc.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +typedef std::unordered_map hash_t; + + +int main() +{ + std::cin.sync_with_stdio(false); + std::cout.sync_with_stdio(false); + std::regex pattern("[a-z']+", std::regex_constants::ECMAScript | std::regex_constants::icase); + std::cregex_iterator e; + hash_t hash; + + while (!std::cin.eof()) { + char buf[1024]; + std::cin.getline(buf, sizeof(buf)); + + for (auto i = std::cregex_iterator(buf, buf + ::strlen(buf), pattern); i != e; ++i) { + ++hash[(*i).str()]; + } + } + + std::vector words(hash.size()); + int i = 0; + for (const auto& e : hash) { + words[i++] = &(e.first); + // words.push_back(&(e.first)); + } + + std::sort(words.begin(), words.end(), [&hash] (const std::string* a, const std::string* b) -> bool { + int diff = hash[*a] - hash[*b]; + return (diff < 0) || (diff == 0 && (*a) < (*b)); // (a.compare(b) < 0); + }); + + for (const auto e : words) { + std::cout << *e << " " << hash[*e] << "\n"; + } +} -- 2.1.4