#include
#include
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
unordered_set<char> perfect_digits;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
perfect_digits.insert(c);
}
for (int i = 0; i < m; i++) {
string s;
cin >> s;
bool is_perfect = true;
for (char c : s) {
if (perfect_digits.find(c) == perfect_digits.end()) {
is_perfect = false;
break;
}
}
if (is_perfect) {
cout << s << endl;
} else {
cout << "none" << endl;
}
}
return 0;
}
first reads n and m, along with n flawless one-digit numbers, stored using an unordered_set.
then loops in m multidigits, and for each multidigit, iterates over each of its digits. If any digit is not in the set of flawless digits, that multidigit is not a beautiful number, marking is_perfect as false. If all the digits of the entire multidigit number are in the set of flawless digits, the multidigit number is a beautiful number, and the multidigit number is printed.
Time complexity is O(m * len), where len is the maximum length of multiple digits.