← 返回 amazon 的题目列表Domain Score Aggregation for Leaf Domains
类型:online_judge
Problem: Domain Score Aggregation for Leaf Domains
You are given several domains, each associated with an integer score. The parent domain of a domain is obtained by removing the leftmost label. For example:
The parent of mail.domain.com is domain.com.
The parent of domain.com is com.
For every leaf domain, output its total score. A leaf domain is a domain that is not the parent suffix of any more specific domain in the input.
The total score of a leaf domain is the sum of its own score and the scores of all its ancestor domains. If an ancestor domain does not appear in the input, its score is considered 0.
Implement a program that outputs all leaf domains and their total scores.
Input Format
The first line contains an integer n, the number of domains.
The next n lines each contain a domain domain and an integer score, separated by a space.
Assume each domain appears at most once.
Output Format
Output all leaf domains and their total scores.
To make the output deterministic, print leaf domains in lexicographical order.
Each line should have the following format:
domain total_score
Constraints
1 <= n <= 2 * 10^5
Each domain contains only lowercase English letters and dots ..
Every label is non-empty, so domains like a..com are invalid.
Each domain length is at most 255.
The total length of all domains is at most 10^6.
-10^9 <= score <= 10^9
Example
Input:
6
com 20
domain.com 10
mail.domain.com 5
test.com 10
user.test.com 30
contact.user.test.com -5
Output:
contact.user.test.com 55
mail.domain.com 35
Explanation:
mail.domain.com = 5 + 10 + 20 = 35
contact.user.test.com = -5 + 30 + 10 + 20 = 55
Example
Input
6
com 20
domain.com 10
mail.domain.com 5
test.com 10
user.test.com 30
contact.user.test.com -5
Output
contact.user.test.com 55
mail.domain.com 35