Before the midterm many students ask Buddy the same questions. Add a cache in front of the (expensive) AI call.
Input: first line capacity ttl costPerCall (capacity ≥ 1 entries, ttl in minutes, cost in yuan). Then lines time question... with non-decreasing times in minutes. Questions are compared after trimming, converting to lower case and collapsing multiple spaces into one.
For each request:
- if the normalized question is in the cache and
time - storedTime < ttl, it is a hit: printt: HIT <normalized question>and mark the entry as most recently used (its stored time does not change); - otherwise it is a miss: print
t: MISS <normalized question>(call the AI), store it with the current time (replacing an expired entry), and if the cache now holds more thancapacityentries, evict the least recently used one and printevicted: <question>.
At the end print Hits: h, Misses: m, Hit rate: x% (one decimal) and AI cost: c yuan, saved: s yuan (two decimals), where cost = misses × costPerCall and saved = hits × costPerCall.
Input:
2 60 0.05
0 What is polymorphism?
5 what is POLYMORPHISM?
10 What is a class?
12 What is an interface?
15 What is polymorphism?
72 What is an interface?
Output:
0: MISS what is polymorphism?
5: HIT what is polymorphism?
10: MISS what is a class?
12: MISS what is an interface?
evicted: what is polymorphism?
15: MISS what is polymorphism?
evicted: what is a class?
72: MISS what is an interface?
Hits: 1, Misses: 5, Hit rate: 16.7%
AI cost: 0.25 yuan, saved: 0.05 yuan