CSP 201809-3 元素选择器

#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';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值