ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Event Subscribers

Deliver events to the subscribers of each topic, observer style, and count what everyone receives.

สิ่งที่คุณจะได้เรียนรู้

  • Map topics to the people who follow them
  • Notify every subscriber of an event
  • Ignore duplicate subscriptions with a set

โจทย์

ดูฉบับแปล

The observer pattern lets objects subscribe to a subject and get notified whenever something happens. Here, people subscribe to topics and every published event goes to everyone subscribed to its topic.

Each subscription is <person> <topic>. A repeated subscription has no extra effect. Then each event in events is a topic name: every subscriber of that topic receives one notification. Events on topics nobody follows are dropped.

Implement event_subscribers(subscriptions, events).

Input

subscriptions: "<person> <topic>" strings (names have no spaces).

events: Topic names, in publishing order.

Output

Return "<person>:<count>" for every person who has at least one subscription, sorted by name (ordinal character order).

เงื่อนไข

1 ≤ number of subscriptions ≤ 10,000

0 ≤ number of events ≤ 100,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
events = ["news", "sports", "weather"]
subscriptions = ["bob sports", "alice news", "alice sports"]

ผลลัพธ์ที่คาดหวัง:
expected = ["alice:2", "bob:1"]
อินพุต:
std::vector<std::string> events = {"news", "sports", "weather"};
std::vector<std::string> subscriptions = {"bob sports", "alice news", "alice sports"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"alice:2", "bob:1"};
อินพุต:
string[] events = new string[] {"news", "sports", "weather"};
string[] subscriptions = new string[] {"bob sports", "alice news", "alice sports"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"alice:2", "bob:1"};

คำอธิบาย

alice follows news and sports, so she gets both events; bob only gets sports; nobody follows weather: ["alice:2", "bob:1"].

ตัวอย่างที่ 2
อินพุต:
events = []
subscriptions = ["zoe tech", "zoe tech"]

ผลลัพธ์ที่คาดหวัง:
expected = ["zoe:0"]
อินพุต:
std::vector<std::string> events = {};
std::vector<std::string> subscriptions = {"zoe tech", "zoe tech"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"zoe:0"};
อินพุต:
string[] events = new string[] {};
string[] subscriptions = new string[] {"zoe tech", "zoe tech"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"zoe:0"};

คำอธิบาย

The repeated subscription counts once, and no events were published: ["zoe:0"].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Group subscribers by topic so each event finds its audience quickly.

คำใบ้ 2

Use a set per topic so a repeated subscription counts once.

คำใบ้ 3

Everyone who subscribed appears in the result, even with zero notifications.

ความซับซ้อนที่คาดหวัง

O(subscriptions + events × subscribers per topic) time, O(subscriptions) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก