当前位置:   article > 正文

CCF CSP 202303-3 LDAP题解_csp202303-3

csp202303-3

一道很简单的题,主要是看到BNF文法我就想写Parser,看见逻辑运算符就想写短路运算。然后浪费了很多时间。使用了类似解释器的结构,好像比一般用集合运算写快,但是时间限制是14s,所有没有任何作用。

`

  1. //
  2. // Created by 11067 on 2023/5/11.
  3. //
  4. #include <cstdio>
  5. #include <unordered_map>
  6. #include <set>
  7. using namespace std;
  8. enum class InstrType {
  9. JS,
  10. JNS,
  11. SEQ,
  12. SNE
  13. };
  14. struct Instr {
  15. InstrType type;
  16. int opr;
  17. int value;
  18. };
  19. Instr instrs[1000];
  20. int pc;
  21. int prog_length;
  22. struct Record {
  23. unordered_map<int, int> attributes;
  24. int dn;
  25. } records[2500];
  26. //parse query
  27. char buffer[2005];
  28. int current;
  29. int number() {
  30. int n = 0;
  31. while (buffer[current] >= '0' && buffer[current] <= '9') {
  32. n *= 10;
  33. n += buffer[current] - '0';
  34. current++;
  35. }
  36. return n;
  37. }
  38. void expr();
  39. void base_expr() {
  40. int attribute = number();
  41. char op = buffer[current++];
  42. int value = number();
  43. instrs[pc++] = Instr{op == ':' ? InstrType::SEQ : InstrType::SNE, attribute, value};
  44. }
  45. void logic_or() {
  46. current += 2;
  47. expr();
  48. int back_patch = pc;
  49. instrs[pc++] = Instr{InstrType::JS, 0, 0};
  50. current += 2;
  51. expr();
  52. instrs[back_patch].value = pc;
  53. current += 1;
  54. }
  55. void logic_and() {
  56. current += 2;
  57. expr();
  58. int back_patch = pc;
  59. instrs[pc++] = Instr{InstrType::JNS, 0, 0};
  60. current += 2;
  61. expr();
  62. instrs[back_patch].value = pc;
  63. current += 1;
  64. }
  65. void expr() {
  66. char peek = buffer[current];
  67. if (peek == '&') {
  68. logic_and();
  69. } else if (peek == '|') {
  70. logic_or();
  71. } else {
  72. base_expr();
  73. }
  74. }
  75. // runner
  76. int run(Record &record) {
  77. int flag = 0;
  78. pc = 0;
  79. while (pc < prog_length) {
  80. switch (instrs[pc].type) {
  81. case InstrType::JS:
  82. if (flag) {
  83. pc = instrs[pc].value - 1;
  84. }
  85. break;
  86. case InstrType::JNS:
  87. if (!flag) {
  88. pc = instrs[pc].value - 1;
  89. }
  90. break;
  91. case InstrType::SEQ:
  92. if (record.attributes.count(instrs[pc].opr) > 0 &&
  93. record.attributes[instrs[pc].opr] == instrs[pc].value) {
  94. flag = 1;
  95. } else {
  96. flag = 0;
  97. }
  98. break;
  99. case InstrType::SNE:
  100. if (record.attributes.count(instrs[pc].opr) > 0 &&
  101. record.attributes[instrs[pc].opr] != instrs[pc].value) {
  102. flag = 1;
  103. } else {
  104. flag = 0;
  105. }
  106. break;
  107. }
  108. pc++;
  109. }
  110. return flag;
  111. }
  112. int main() {
  113. int n, m;
  114. scanf("%d", &n);
  115. for (int i = 0; i < n; ++i) {
  116. auto &record = records[i];
  117. scanf("%d", &record.dn);
  118. int t;
  119. scanf("%d", &t);
  120. for (int j = 0; j < t; ++j) {
  121. int k, v;
  122. scanf("%d %d", &k, &v);
  123. record.attributes.insert({k, v});
  124. }
  125. }
  126. scanf("%d", &m);
  127. for (int i = 0; i < m; ++i) {
  128. scanf("%s", buffer);
  129. pc = 0;
  130. current = 0;
  131. expr();
  132. prog_length = pc;
  133. set<int> ans;
  134. for (int j = 0; j < n; ++j) {
  135. auto &record = records[j];
  136. if (run(record)) {
  137. ans.insert(record.dn);
  138. }
  139. }
  140. for (const auto& e: ans) {
  141. printf("%d ", e);
  142. }
  143. putchar('\n');
  144. }
  145. return 0;
  146. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/215723
推荐阅读
相关标签
  

闽ICP备14008679号