cswork / OA打开练习工作台

HarveyOA #1

Evaluate an Expression Map

估计困难评测数据准备中,可先阅读题面

考点(AI 标注)拓扑排序字符串单调栈与堆

You are given symbol definitions in the form name=expression. An expression may contain integer constants, references to previously defined or later defined symbols, and the binary operators +, -, *, and /. Evaluate the final integer value of every symbol. Return the results in the same order as the input definitions, formatted as "name=value". If the dependency graph contains a cycle, return a single-element array ["CYCLE"] instead of partial results. Function Description Complete the function evaluateExpressionMap in the editor below. evaluateExpressionMap has the following parameter:

  • String[] definitions: the symbol definitions Returns String[]: evaluated symbol assignments in input order, or ["CYCLE"] if a cycle exists.

Constraints

  • 1 ≤ definitions.length ≤ 10⁵
  • Expressions use integer arithmetic and may reference other symbols.
  • Use cycle detection to avoid infinite recursion.

Example 1

Input:

definitions = ["a=1", "b=a+2", "c=b*3"]

Output:

["a=1", "b=3", "c=9"]

Explanation: Each symbol depends only on earlier evaluated values, so the map resolves cleanly.

Example 2

Input:

definitions = ["a=b+1", "b=a+1"]

Output:

["CYCLE"]

Explanation: The definitions form a cycle, so the special cycle marker is returned.