← 返回 meta 的题目列表Maximum Unique-Character Coverage With Non-Overlapping Words
类型:online_judge
Problem: Maximum Unique-Character Coverage With Non-Overlapping Words
Given a list of lowercase words words, choose a subset such that:
No two chosen words share any common letter (i.e., their character sets are pairwise disjoint).
Subject to that constraint, maximize the number of unique characters covered by the chosen subset.
Return the maximum possible number of unique characters.
Notes
If a word contains duplicate letters within itself (e.g., "apple"), it can never be part of a valid subset (even alone it violates uniqueness), so it can be treated as invalid/unselectable.
Alphabet is limited to a-z.
Input / Output
Input:
Line 1: integer n (number of words)
Next n lines: one word per line
Output:
One integer: the maximum number of unique letters that can be covered.
Constraints (suggested)
1 <= n <= 20 (larger requires pruning/bitmask optimization)
1 <= len(word) <= 26
Words contain only a-z
Example
Input:
12
jan
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
Output:
9
Explanation: For example, selecting feb + may + jun covers {f,e,b,m,a,y,j,u,n} which is 9 unique letters. Note that jan and may both contain a, so they cannot be selected together.
Example
Input
12
jan
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
Output
9