#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<queue>
#include<stack>
using namespace std;
struct WordCount
{
int freInHead;
int freInText;
int finalFre;
int idInHline;
int idInTline;
int appearIdInHead;
int appredIDInText;
string word;
WordCount(){
freInHead = freInText = finalFre =0;
appearIdInHead = appredIDInText = 0;
}
WordCount(string w, int id, int idil):word(w){
if(id%2==0){
freInHead =1;
freInText = 0;
finalFre = 3;
appearIdInHead = id;
idInHline = idil;
idInTline = INT32_MAX;
appredIDInText = INT32_MAX;
}
else{
freInHead =0;
freInText = 1;
finalFre = 1;
idInTline = idil;
idInHline = INT32_MAX;
appearIdInHead = INT32_MAX;
appredIDInText = id;
}
}
void append(int id, int idil){
if(id%2==0){
freInHead +=1;
finalFre +=3;
if(id<appearIdInHead){
appearIdInHead = id;
idInHline = idil;
}
}
else{
freInText +=1;
finalFre +=1;
if(id<appredIDInText){
appearIdInHead = id;
idInTline = idil;
}
}
}
};
struct cmp
{
bool operator()(WordCount & a, WordCount &b){
if(a.finalFre>b.finalFre ) return true;
else if(a.finalFre==b.finalFre && a.freInHead > b.freInHead) return true;
else if( a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText>b.freInText) return true;
else if(a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText==b.freInText && a.freInText>b.freInText) return true;
else if(a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText==b.freInText && a.freInText== b.freInText && a.appearIdInHead<b.appearIdInHead) return true;
else if(a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText==b.freInText && a.freInText== b.freInText && a.appearIdInHead==b.appearIdInHead && a.idInHline<b.idInHline) return true;
else if(a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText==b.freInText && a.freInText== b.freInText && a.appearIdInHead==b.appearIdInHead && a.idInHline==b.idInHline&& a.appredIDInText<b.appredIDInText) return true;
else if(a.finalFre==b.finalFre && a.freInHead == b.freInHead && a.freInText==b.freInText && a.freInText== b.freInText && a.appearIdInHead==b.appearIdInHead && a.idInHline==b.idInHline&& a.appredIDInText==b.appredIDInText&& a.idInTline<b.idInTline) return true;
else return false;
}
};
int main(){
int N, M;
cin>>N>>M;
string inp;
getline(cin, inp);
unordered_map<string, WordCount> wordset;
priority_queue<WordCount, vector<WordCount> , cmp> topN;
for(int i=0;i<2*M;i++){
getline(cin, inp);
int pos = inp.find(" ");
int pre=0;
int idInLine=0;
while(pos!=string::npos){
string temp = inp.substr(pre, pos-pre);
if(wordset.find(temp)!=wordset.end()){
wordset[temp].append(i, idInLine++);
}
else{
wordset[temp] = WordCount(temp, i, idInLine++);
}
pre = pos+1;
pos = inp.find(" ", pre);
}
string temp = inp.substr(pre, inp.size()-pre);
if(wordset.find(temp)!=wordset.end()){
wordset[temp].append(i, idInLine++);
}
else{
wordset[temp] = WordCount(temp, i, idInLine++);
}
}
int cnt=0;
cmp com;
for(auto & item: wordset ){
if(cnt<N){
topN.push(item.second);
cnt++;
}
else{
WordCount t = topN.top();
if(com(item.second, t)){
topN.pop();
topN.push(item.second);
}
}
}
stack<string> stack_string;
while(!topN.empty()){
string temp = topN.top().word;
topN.pop();
stack_string.push(temp);
}
while(!stack_string.empty()){
string temp = stack_string.top();
cout<<temp;
stack_string.pop();
if(!stack_string.empty()) cout<<" ";
}
return 0;
}
|