#include <bits/stdc++.h>
using namespace std;
int n,m;
struct element {
int line;//行号
int dep;//缩进个数
string content;//实际内容
vector<int> p;//父类
string ele;
};
vector<element> elements;
string to_lower(string s) {
string ret(s);
for (auto& c:ret) {
if (islower(c) || isdigit(c)) {
continue;
} else {
c = c - 'A' + 'a';
}
}
return ret;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
string hd;
for (int i = 0; i <= n; i++) {
getline(cin,hd);
element e;
e.line = i;
int dep = 0;
while (dep < hd.size() && hd[dep] == '.') {
dep++;
}
auto pos = hd.find(' ');
if (pos != string::npos) {
string ele = hd.substr(pos + 1);
e.ele = ele;
e.content = to_lower(hd.substr(dep,pos - dep));
} else {
e.content = to_lower(hd.substr(dep));
}
dep /= 2;
e.dep = dep;
elements.push_back(e);
}
for (int i = 1; i <= n; i++) {
int dep = elements[i].dep;
for (int j = i + 1; j <= n; j++) {
if (elements[j].dep > dep) {
elements[j].p.push_back(i);
} else {
break;
}
}
}
for (int i = 1; i <= n; i++) {
sort(elements[i].p.begin(), elements[i].p.end(),[&](int a,int b) {return a > b;});
}
for (int i = 1; i <= m; i++) {
string e;
string tmp;
getline(cin,e);
stringstream ss (e);
vector<string> vec;
while (getline(ss,tmp,' ')) {
vec.push_back(tmp);
}
set<int> lines;
if (vec.size() == 1) {
if (vec[0][0] == '#') {
for (auto& ele : elements) {
if (ele.ele != "" && ele.ele == vec[0]) {
lines.insert(ele.line);
}
}
} else {
for (auto& ele : elements) {
if (ele.content != "" && ele.content == to_lower(vec[0])) {
lines.insert(ele.line);
}
}
}
} else {
bool eq = false;
for (int cz = 0; cz < elements.size(); cz++) {
int pp = vec.size() - 1;
if (vec[pp][0] == '#') {
if (elements[cz].ele == vec[pp]) {
pp--;
} else {
continue;
}
} else {
if (elements[cz].content == to_lower(vec[pp])) {
pp--;
} else {
continue;
}
}
for (auto& fl : elements[cz].p) {
if (vec[pp][0] == '#') {
if (elements[fl].ele ==vec[pp]) {
pp--;
}
} else {
if (elements[fl].content == to_lower(vec[pp])) {
pp--;
}
}
if (pp < 0) {
lines.insert(elements[cz].line);
break;
}
}
}
}
cout << lines.size() << ' ';
for (auto& l : lines) {
cout << l << ' ';
}
cout << '\n';
}
}
CSP 201809-3 元素选择器
最新推荐文章于 2024-07-05 22:37:42 发布