Advertisement
zhangsongcui

Unique Hash

Feb 17th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdexcept>
  4.  
  5. constexpr uint64_t operator "" _uniq_hash(char const * str, size_t len) {
  6.     return len > sizeof(uint64_t) / sizeof(char)
  7.         ? throw std::length_error("str")
  8.         : len == 0
  9.             ? 0
  10.             : *str + (operator "" _uniq_hash(str + 1, len - 1) << sizeof(char) * 8);
  11. }
  12.  
  13. uint64_t get_uniq_hash(const std::string& str) {
  14.     if (str.length() > sizeof(uint64_t) / sizeof(char))
  15.         throw std::length_error("str");
  16.  
  17.     uint64_t result = 0;
  18.     for (auto rit = str.rbegin(); rit != str.rend(); ++rit) {
  19.         result <<= sizeof(char) * 8;
  20.         result += *rit;
  21.     }
  22.     return result;
  23. }
  24.  
  25. int main() {
  26.     using namespace std;
  27.     string word;
  28.     while (cin >> word) {
  29.         if (word.length() < sizeof(uint64_t)) {
  30.             switch (get_uniq_hash(word)) {
  31.             case "zero"_uniq_hash:
  32.             case "one"_uniq_hash:
  33.             case "two"_uniq_hash:
  34.             case "three"_uniq_hash:
  35.             case "four"_uniq_hash:
  36.             case "five"_uniq_hash:
  37.             case "six"_uniq_hash:
  38.             case "seven"_uniq_hash:
  39.             case "eight"_uniq_hash:
  40.             case "nine"_uniq_hash:
  41.                 cout << "A number" << endl;
  42.                 continue;
  43.             }
  44.         }
  45.         cout << "Not a number" << endl;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement