Editorial for COCI '13 Contest 6 #2 Font
Submitting an official solution before solving the problem yourself is a bannable offence.
We need to find the number of different subsets of the set of given words such that the combined words in the subsets contain all lowercase letters of the English alphabet. In a set of words, we can choose different subsets. Because is an integer less than , we have a maximum of subsets. This number is small enough for us to be able to pass through all possible subsets and check whether they contain the whole alphabet.
A regular way of passing through all subsets of a set is using a recursive function.
f( i ):
if i = N + 1: check whether the inserted sets contain the whole alphabet
(we've passed through all words)
else:
add the i'th word to the set
call f( i + 1 )
remove the i'th word from the set
call f( i + 1 )
Additionally, we need to decide how to represent these sets in the computer's memory so we could implement the addition and removal of a word in the set.
One possible way is using an array which keeps track of how many times each letter of the alphabet appears in the current subset and using a variable which keeps track of how many different letters there are in the current subset.
When we add a word to the subset, we increment the counter in the array for each letter of the word, and increment the counter only if was written so far in (if we've added a letter which hasn't appeared in the set so far). A similar procedure is done for removing a word from the set.
The complexity of this algorithm is , where is the maximal length of the word. This solution is sufficient for of points.
Another way of representing sets of letters in the computer's memory is using a bitmask. A bitmask is a series of ones and zeroes where the place is zero only if the element is present in the set and zero if it isn't. For example, the set is represented with a bitmask (the numbers are enumerated from right to left). The union operator of the sets represented with bitmasks corresponds to the OR operator of their bitmasks.
The advantage of this implementation of sets in the memory is because the processor deals with bitwise operations really quickly, the complexity being .
The complexity of the algorithm when using this kind of implementation of set operations is .
Comments