From a8c5c425bea552e691e7753a82914f9d3ccdc244 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Thu, 5 Aug 2021 19:29:56 +0200 Subject: [PATCH] Slow version of pattern hash function --- a.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/a.c b/a.c index 6a7daca..8c2183d 100644 --- a/a.c +++ b/a.c @@ -17,6 +17,8 @@ string ToUpper(string s) { return s2; } +// -------------------------------------------------------------------------------- +//-Word struct Word { Word() {} // default empty constructor Word(string s) : word(s) {} // standard way of initialization @@ -25,9 +27,27 @@ struct Word { typedef vector Words; typedef unordered_map WordMap; // hash-table from stl +// -------------------------------------------------------------------------------- +//-Library class Library { public: Library() {} // hash-tables are automatically initialized + void FindWord(const string& s) const { // references are prefered instead of copies + int len = s.length(); // cache the length to avoid multiple calls + for (const Word& w : words_) { // references are prefered instead of copies + string temp = w.word; + if (len == temp.length()) { + for (int i=0; i