#include #include #include #include #include using namespace std; void radix_sort(vector& v){ array, 27> buckete; const int max_len = max_element(begin(v), end(v), [](const string& a, const string& b){ return a.size() < b.size(); })->size(); for(int i = max_len-1; i >= 0; --i){ for(auto& x : v){ if(x.size() <= i){ buckete[0].emplace_back(move(x)); } else{ const int poz = x[i]-'a'+1; buckete[poz].emplace_back(move(x)); } } auto it = begin(v), it2 = begin(v); for(auto& x : buckete){ it2 = it + x.size(); move(begin(x), end(x), it); it = it2; } } } array caracterizeaza(const string& str){ array rez = {}; for(const auto x : str){ ++rez[x-'a']; } return rez; } template bool all_of_dublu(Input_it1 st1, Input_it1 dr1, Input_it2 st2, Binary_pred bp){ for( ; st1 < dr1; ++st1, ++st2){ if(!bp(*st1, *st2)){ return false; } } return true; } int main(){ ios_base::sync_with_stdio(false); string mare; cin >> mare; const auto car_mare = caracterizeaza(mare); int n; cin >> n; vector v(n); for(auto& x : v){ cin >> x; } radix_sort(v); for(const auto& x : v){ const auto car_mic = caracterizeaza(x); if(all_of_dublu(begin(car_mic), end(car_mic), begin(car_mare), less_equal())){ cout << x << '\n'; } } return 0; }