`bool comp(pair<int,int>a,pair<int,int>b){
if(a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
class TrieNode{
public:
TrieNode* arr[26];
bool eow;
};
TrieNode* get(){
TrieNode* new_node = new TrieNode();
new_node->eow = false;
for(int i = 0; i < 26; i++) new_node->arr[i] = NULL;
return new_node;
}
void insert(TrieNode* root,string s){
TrieNode* crawl = root;
for(int i = 0; i < s.length(); i++){
int index = s[i]-'a';
if(!crawl->arr[index]){
crawl->arr[index] = get();
}
crawl = crawl->arr[index];
}
crawl->eow = true;
}
bool search(TrieNode* root,string s){
TrieNode* crawl = root;
for(int i = 0; i < s.length(); i++){
int index = s[i]-'a';
if(!crawl->arr[index]) return false;
crawl = crawl->arr[index];
}
return (crawl && crawl->eow);
}
vector<int> Solution::solve(string A, vector<string> &vec) {
vector<pair<int,int>>res;
string T;
stringstream X(A);
TrieNode* root = get();
while (getline(X, T, '_')) {
insert(root,T);
}
for(int i = 0; i < vec.size(); i++){
string s = vec[i];
string t;
stringstream x(s);
int count = 0;
while (getline(x, t, '_')) {
if(search(root,t)) count++;
}
res.push_back({count,i});
}
sort(res.begin(),res.end(),comp);
vector<int>ans;
for(int i = 0; i < res.size(); i++){
ans.push_back(res[i].second);
}
return ans;
}
`