29届CSP第三题参考解答

简化的LL1写法。

#include <bits/stdc++.h>
using namespace std;

struct user {
  int id;
  map<int, int> attributes;
};

struct expr {
  virtual bool accept(const user& u) = 0;
};

struct assert_expr : public expr {
  int attrid, attrval;
  assert_expr(int id, int val) : attrid(id), attrval(val) {}

  bool accept(const user& u) override {
    return (u.attributes.count(attrid) && (u.attributes.at(attrid) == attrval));
  }
};

struct nassert_expr : public expr {
  int attrid, attrval;
  nassert_expr(int id, int val) : attrid(id), attrval(val) {}

  bool accept(const user& u) override {
    return (u.attributes.count(attrid) && (u.attributes.at(attrid) != attrval));
  }
};

struct and_expr : public expr {
  expr *lhs, *rhs;
  and_expr(expr* l, expr* r) : lhs(l), rhs(r) {}

  bool accept(const user& u) override {
    return lhs->accept(u) && rhs->accept(u);
  }
};

struct or_expr : public expr {
  expr *lhs, *rhs;
  or_expr(expr* l, expr* r) : lhs(l), rhs(r) {}

  bool accept(const user& u) override {
    return lhs->accept(u) || rhs->accept(u);
  }
};

int parse_int(const char*& pos) {
  int ret = 0;
  while (*pos >= '0' && *pos <= '9') ret = ret * 10 + (*pos - '0'), pos++;
  return ret;
}

// base_expr := INT OP INT
expr* parse_base_expr(const char*& pos) {
  int attrid = parse_int(pos);
  int is_assert = (*pos == ':');
  pos++;  // skip : or ~
  int attrval = parse_int(pos);

  if (is_assert)
    return new assert_expr(attrid, attrval);
  else
    return new nassert_expr(attrid, attrval);
}

// expr := base_expr | logic_expr
// logic_expr := LOGIC ( expr ) ( expr )
expr* parse_expr(const char*& pos) {
  if (*pos >= '0' && *pos <= '9') return parse_base_expr(pos);
  int is_and = (*pos == '&');
  pos++;  // skip | or &
  pos++;  // skip (
  expr* lhs = parse_expr(pos);
  pos++;  // skip )
  pos++;  // skip (
  expr* rhs = parse_expr(pos);
  pos++;  // skip )

  if (is_and)
    return new and_expr(lhs, rhs);
  else
    return new or_expr(lhs, rhs);
}

expr* parse(const char* pos) { return parse_expr(pos); }

int main() {
  int users, queries;
  cin >> users >> queries;

  // read user info
  vector<user> U(users);
  for (auto& u : U) {
    cin >> u.id;
    int attrcnt, attrid, attrval;
    cin >> attrcnt;
    while (attrcnt--) {
      cin >> attrid >> attrval;
      u.attributes[attrid] = attrval;
    }
  }

  // read and deal with queries
  while (queries--) {
    string exprtext;
    cin >> exprtext;

    expr* result = parse(exprtext.data());
    vector<int> ans;
    for (const auto& u : U)
      if (result->accept(u)) ans.emplace_back(u.id);

    sort(ans.begin(), ans.end());
    copy(ans.begin(), ans.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
  }

  return 0;
}
### 关于第35次 CSP 认证考试第三解答 目前针对第35次 CSP 认证考试的具体第三官方解析尚未完全公开,但从以往类似的 CSP 考试目来看,通常第三属于中高难度范围内的编程问。这类目往往涉及较为复杂的逻辑处理或者特定的数据结构应用。 #### 目背景推测 基于引用中的描述[^1],可以推断该场考试的第一围绕字符串匹配展开,而第二则涉及到矩阵操作[^2]。因此,按照 CSP 的一贯命风格,第三可能更倾向于考察选手对于复杂算法的理解能力或者是对某些经典数据结构的应用技巧。 如果假设此为一道典型的算法设计类试,则需注意以下几个方面: - **时间与空间效率**:任何解决方案都应考虑最坏情况下的性能表现。 - **边界条件测试**:确保程序能够正确处理各种极端输入情形。 以下是关于如何构建此类问通用解法框架的一个示例代码片段(假定目标是对一组数列执行某种变换并统计结果),供参考学习之用: ```python def process_sequence(sequence, operation): result = [] for item in sequence: processed_item = apply_operation(item, operation) if validate_condition(processed_item): # 假设存在验证函数 result.append(processed_item) return result def apply_operation(value, op_type): """模拟不同类型的运算""" if op_type == 'A': return value * 2 elif op_type == 'B': return value - 3 else: raise ValueError("Unsupported Operation Type") # 示例调用 input_data = [1, 2, 3, 4] operation_code = 'A' output = process_sequence(input_data, operation_code) print(output) ``` 上述伪代码仅作为示范用途,并不代表实际考内容。真正的答过程还需要依据具体目要求来调整实现细节。 #### 数据结构的选择建议 当面对需要频繁增删节点或是快速查找元素的情况时,链表、哈希表等高级数据结构可能是更好的选择;而对于有序集合的操作,则平衡树或许更为合适。当然这都要视具体情况而定。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值