uwc: cpp implementation
authorRichard Kojedzinszky <krichy@tvnetwork.hu>
Tue, 30 Sep 2014 13:54:56 +0000 (15:54 +0200)
committerRichard Kojedzinszky <krichy@tvnetwork.hu>
Tue, 30 Sep 2014 13:54:56 +0000 (15:54 +0200)
uwc.cpp [new file with mode: 0644]

diff --git a/uwc.cpp b/uwc.cpp
new file mode 100644 (file)
index 0000000..cf40b54
--- /dev/null
+++ b/uwc.cpp
@@ -0,0 +1,41 @@
+#include <regex>
+#include <unordered_map>
+#include <string>
+#include <iostream>
+
+typedef std::unordered_map<std::string, int> 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<const std::string*> 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";
+       }
+}