A restaurant listing website has asked you to analyse the reviews on its restaurants. The reviews need to be sorted by how good they are, the best reviews go on top. The goodness a review is determined by the number of times “good words” appear in them. You are given a list of good words.
Given n reviews and m good words, sort the reviews, such that the one with most good words appears first. For reviews with the same number of good words, maintain the initial order.
string parse(string& s, int& i){ int n = s.length(); string res = ""; while(i < n and s[i] != ' ') { res.push_back(s[i++]); } i++; return res; }
intcountGoodWords(string& s, unordered_set<string>& us){ int res = 0, i = 0, n = s.length(); while(i < n) { auto token = parse(s,i); res += us.count(token); } return res; }