#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<cctype>
#include<deque>
using namespace std;
int point(string s) {
int ans = 0;
for (auto&ch : s) {
if (ch == '.') ++ans;
else break;
}
return ans;
}
void all_tolow(string& s) {for (auto&ch : s) ch = tolower(ch);}
bool matchsign(string s,string q) {
stringstream ss(s);
string t;
getline(ss, t, ' ');
all_tolow(t);
all_tolow(q);
return t == q;
}
bool matchid(string s, string q) {
int f =s.find('#');
if (f != -1) {
return s.substr(f) == q;
}
return false;
}
bool match(string s, string q) {return q.front()=='#'? matchid(s, q): matchsign(s, q);}
struct ac {
string c;
int level;
ac(string a, int b) {
c = a;
level = b;
}
};
int main() {
int n, m;
cin >> n >> m;
cin.get();
vector<vector<string>> content(n);
deque<ac> temp;
int last = -1;
for (int i = 0; i < n; ++i) {
string s;
getline(cin, s);
int now = point(s) / 2;
while (now <= last) {
if (temp.size()) {
temp.pop_back();
last = temp.back().level;
}
}
temp.emplace_back(ac(s.substr(now * 2), now));
last = now;
for (int j = 0; j < temp.size(); ++j) content[i].emplace_back(temp[j].c);
}
for (int i = 0; i < m; ++i) {
string s;
getline(cin, s);
string temp;
vector<string> q;
vector<int> ans;
stringstream ss(s);
while (getline(ss, temp,' ')) {
if (temp == "") continue;
q.emplace_back(temp);
}
if (q.size() == 1) {
for (int j = 0; j < n; ++j)
if (match(content[j].back(), q[0])) ans.emplace_back(j + 1);
}
else {
for (int j = 0; j < n; ++j) {
if (match(content[j].back(), q.back())) {
int count = 0, k = 0;
while (count < q.size() - 1 && k < content[j].size() - 1) {
if (match(content[j][k], q[count])) ++count;
++k;
}
if (count == q.size() - 1) ans.emplace_back(j + 1);
}
}
}
cout << ans.size() << " ";
for (int j = 0; j < ans.size(); ++j) cout << ans[j] << " ";
cout << endl;
}
}
06-05
212
06-07
265
06-13
281