← 返回 rippling 的题目列表Expense Rule Engine
类型:online_judge
Problem: Expense Rule Engine
Implement a simplified expense rule engine. The system has a list of rules. Each rule checks an expense's category and amount. If a rule matches, the engine returns that rule's decision.
Input Format
The first line contains two integers:
R E
R: number of rules
E: number of expenses
The next R lines each describe one rule:
priority rule_id category op threshold decision
Fields:
priority: integer; smaller value means higher priority
rule_id: rule identifier, no spaces
category: applicable expense category; * matches all categories
op: amount comparison operator, one of >, >=, <, <=, ==
threshold: non-negative amount
decision: one of APPROVE, REJECT, REVIEW
The next E lines each describe one expense:
expense_id category amount
Output Format
For each expense, evaluate rules from highest priority to lowest priority. The first rule is considered matched if:
The rule category equals the expense category, or the rule category is *;
The expense amount satisfies the rule's comparison operator against threshold.
Output:
expense_id decision matched_rule_id
If no rule matches, output:
expense_id APPROVE DEFAULT
If multiple rules have the same priority, their input order breaks ties.
Constraints
0 <= R <= 5000
1 <= E <= 5000
IDs and categories contain no spaces
Amounts are non-negative decimal values with at most two digits after the decimal point
Example
Input:
3 4
1 R1 alcohol > 0 REJECT
2 R2 travel > 1000 REVIEW
3 R3 * > 5000 REJECT
E1 meals 30
E2 travel 1200
E3 alcohol 10
E4 hardware 6000
Output:
E1 APPROVE DEFAULT
E2 REVIEW R2
E3 REJECT R1
E4 REJECT R3
Example
Input
3 4
1 R1 alcohol > 0 REJECT
2 R2 travel > 1000 REVIEW
3 R3 * > 5000 REJECT
E1 meals 30
E2 travel 1200
E3 alcohol 10
E4 hardware 6000
Output
E1 APPROVE DEFAULT
E2 REVIEW R2
E3 REJECT R1
E4 REJECT R3