    #include <vector>
    #include <map>
    #include <set>
    #include <iostream>
    #include <string>
    #include <cstdint>

    typedef std::string IDdoc;
    typedef uint32_t position;

    typedef std::pair<IDdoc,position> Occurrence;
    typedef std::vector<Occurrence> OccurrencesOfWord;
    typedef std::map<std::string /*word*/, OccurrencesOfWord> Dictionary;
    typedef std::set<IDdoc> Matches;

    bool findMatchesForPhrase(const std::string& phrase, const Dictionary& dictionary, Matches& matches)
    {
        size_t pos = 0;
        size_t len = 0;
        while (pos < phrase.length()) {
            size_t end = phrase.find(' ', pos);
            size_t len = ((end == phrase.npos) ? phrase.length() : end) - pos;
            std::string word(phrase, pos, len);
            pos += len + 1; // to skip the space.

            // ignore words not in the dictionary.
            auto dictIt = dictionary.find(word);
            if (dictIt == dictionary.end())
                continue;

            auto& occurrences = dictIt->second; // shortcut/alias,.
            for (auto& occurIt : occurrences) {
                // Add all the IDdoc's of this occurence to the set.
                matches.insert(occurIt.first);
            }
        }

        return !matches.empty();
    }
    
    void addToDictionary(Dictionary& dict, const char* word, const char* doc, uint32_t position)
    {
        dict[word].push_back(std::make_pair(std::string(doc), position));
    }
    
    int main(int argc, const char** argv)
    {
        std::string phrase("pizza is life");
        Dictionary dict;
        
        addToDictionary(dict, "pizza", "book1", 10);
        addToDictionary(dict, "pizza", "book2", 30);
        addToDictionary(dict, "life", "book1", 1);
        addToDictionary(dict, "life", "book3", 1);
        addToDictionary(dict, "goat", "book4", 99);
        
        Matches matches;
        bool result = findMatchesForPhrase(phrase, dict, matches);
        
        std::cout << "result = " << result << std::endl;
        for (auto& ent : matches) {
            std::cout << ent << std::endl;
        }
        
        return 0;
    }