#include<bits/stdc++.h>
using namespace std;
bool comp(pair<string,int> p1,pair<string,int>p2)
{
if(p1.second == p2.second)
{
return p1.first<p2.first;
}
return p1.second>p2.second;
}
vector<string> topToys(int total,int top,vector<string>toysToSearch,vector<string>paragraph)
{
map<string,pair<string,int> >isToy;
for(auto it = toysToSearch.begin();it!=toysToSearch.end();it++)
{
string stringBeforeLower = *it;
string convertedString = "";
for(int i = 0;i<stringBeforeLower.length();i++)
{
convertedString+=tolower(stringBeforeLower[i]);
}
isToy[convertedString] = make_pair(stringBeforeLower,1);
}
unordered_map<string,pair<string,int>>frequencyCount;
for(auto it = paragraph.begin();it!=paragraph.end();it++)
{
stringstream ss(*it);
string word,convertedString;
while(ss >> word)
{
convertedString = "";
for(int i = 0;i<word.length();i++)
{
convertedString+=tolower(word[i]);
}
if(isToy[convertedString].second)
{
frequencyCount[convertedString].second++;
frequencyCount[convertedString].first = isToy[convertedString].first;
}
}
}
vector<pair<string,int>>v;
for(auto it = frequencyCount.begin();it!=frequencyCount.end();it++)
{
v.push_back(make_pair(it->second.first,it->second.second));
}
sort(v.begin(),v.end(),comp);
int i = 0;
vector<string>stringVector;
if(top>total)
{
for(auto it = v.begin();it!=v.end();it++)
{
stringVector.push_back(it->first);
}
}
else
{
for(auto it = v.begin();it!=v.end();it++)
{
if(i==top)
break;
stringVector.push_back(it->first);
i++;
}
}
return stringVector;
}
int main()
{
int total =6,top =2;
vector<string>toysToSearch;
vector<string>paragraph;
toysToSearch.push_back("elmo");
toysToSearch.push_back("elsa");
toysToSearch.push_back("legos");
toysToSearch.push_back("drone");
toysToSearch.push_back("tablet");
toysToSearch.push_back("warcraft");
paragraph.push_back("Elmo is the hottest of the season! Elmo will be on every kid's wishlist!");
paragraph.push_back("The new Elmo dolls are super high quality");
paragraph.push_back("Expect the Elsa dolls to be very popular this year, Elsa!");
paragraph.push_back("Elsa and Elmo are the toys I'll be buying for my kids, Elsa is good");
paragraph.push_back("For parents of older kids, look into buying them a drone");
paragraph.push_back("Warcraft is slowly rising in popularity ahead of the holiday season");
vector<string>v = topToys(total,top,toysToSearch,paragraph);
for(auto it = v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
return 0;
}