fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. struct text
  7. {
  8. text() = default;
  9. text(const std::string& txt) : data(txt) {}
  10.  
  11. std::string key() const { std::string k(data); std::sort(k.begin(), k.end()); return k; }
  12.  
  13. operator std::pair<std::string, std::string>() const { return std::make_pair(key(), data); }
  14.  
  15. std::string data;
  16. };
  17.  
  18. std::istream& operator>>(std::istream& is, text& t)
  19. {
  20. return is >> t.data;
  21. }
  22.  
  23. std::ostream& operator<<(std::ostream& os, const text& t)
  24. {
  25. return os << t.data;
  26. }
  27.  
  28. std::string quoted(const text& t)
  29. {
  30. return std::string(1, '"') + t.data + '"';
  31. }
  32.  
  33. int main()
  34. {
  35. using pair_type = std::pair<std::string, std::string> ;
  36.  
  37. std::unordered_map<std::string, std::string> map =
  38. {
  39. pair_type(text("scooby")), pair_type(text("shaggy")), pair_type(text("veronica")),
  40. pair_type(text("fred")), pair_type(text("daphne")), pair_type(text("wilma")),
  41. pair_type(text("betty")), pair_type(text("barney")), pair_type(text("dino")),
  42. pair_type(text("bambam")), pair_type(text("pebbles")), pair_type(text("george"))
  43. };
  44.  
  45. text t;
  46.  
  47. while (std::cin >> t)
  48. {
  49. auto it = map.find(t.key());
  50. if (it != map.end())
  51. std::cout << quoted(t) << " matches " << quoted(it->second) << '\n';
  52. else
  53. std::cout << quoted(t) << " matches nothing\n";
  54. }
  55. }
Success #stdin #stdout 0s 3488KB
stdin
hello
yobocs
incavero
name
yabren
noid
stdout
"hello" matches nothing
"yobocs" matches "scooby"
"incavero" matches "veronica"
"name" matches nothing
"yabren" matches "barney"
"noid" matches "dino"