EllysKeys
SRM 815 · 2021-10-06 · by espr1t
Problem Statement
The old-fashioned door lock keys are usually some pattern of cuts into a metal that should fit perfectly into the lock in order to unlock it. The key also has a handle at one of the ends which is used to turn the key after it is inserted into the lock.
The simplest keys have all the cuts at an equal depth. On such keys, each part of the key is either cut or not. We can represent such keys using strings of two characters: a period ('.') will represent a cut piece and a caret ('^') will represent an uncut piece (also called a "tooth").
An example key would be ".^^.^.^^^.^.^". This key has length 13 and has five cuts (at zero-indexed positions 0, 3, 5, 9, and 11) and eight teeth.
Elly is designing a new lock mechanism. Her lock will be opened by using multiple simple keys (as described above). The keys will be inserted parallel to each other and aligned so that they all end at the same depth.
The more simple keys the girl uses, the better the security of the lock gets. However, due to the specifics of the lock, no two keys can have a tooth at the same position.
For example if the girl has the following keys:
{ "..^.^^.^",
"^.^^...^",
"^.....^." }
she can use two of them: if she places "..^.^^.^" and "^.....^." next to each other, at most one of them has a tooth at each position. She cannot use all three keys, because there would be multiple teeth at zero-indexed positions 0, 2, and 7. Moreover, the girl cannot even use only the keys "^.^^...^" and "^.....^." together, because they have a tooth at the same position.
The girl has a collection of keys she can use for her new locking system.
They are given to you in the
Help Elly: Determine and return the maximal number of keys she can use for her lock.
Notes
- The keys all have handles on the same side, so they cannot be used in reverse. E.g., if you are given the key "^^...", you cannot use it as "...^^".
Constraints
- keys will contain between 1 and 50 elements, inclusive.
- Each element of keys will have length between 1 and 20, inclusive.
- Each element of keys will contain only the characters '.' and '^'.
- All elements of keys will have the same length.
{"..^.^^.^",
"^.^^...^",
"^.....^."}
Returns: 2
The example from the problem statement.
{"^.^"}
Returns: 1
With a single key the best solution is always to use it.
{"^..^...^",
".^^.....",
".^..^...",
"..^...^.",
"...^^.^."}
Returns: 3
Here the optimal configuration is to take keys 0, 2, and 3 (zero-indexed).
{"....................",
"^^^^^^..^^^^..^^^^^.",
"..^^...^^..^^.^^..^^",
"..^^...^^..^^.^^^^^.",
"..^^...^^..^^.^^....",
"..^^....^^^^..^^....",
"....................",
".^^^^...^^^^..^^^^..",
"^^...^.^^..^^.^^..^^",
"^^.....^^..^^.^^..^^",
"^^...^.^^..^^.^^..^^",
".^^^^...^^^^..^^^^..",
"....................",
"...^^^^^^...^^^^^...",
"...^^.......^^..^^..",
"...^^^^^....^^^^^...",
"...^^.......^^.^^...",
"...^^^^^^...^^..^^..",
"...................."}
Returns: 5
It is possible that some keys don't have any teeth, in which case all of them can be used.
{".^....^.^.^....^.", "^..^..^.....^....", "^......^.........", "^^..^........^..^", "^...^...^^...^...", "........^^^.....^", "...^.^....^^..^^.", "^...^..^.^.^.....", "..^^.^^.......^..", ".^.^.^..^^.....^.", ".^...^...^.^..^..", "..........^......", "^..^.^........^^.", "^.....^...^......", "...^^..^^.......^", "......^..^...^^^.", "^^....^^...^^.^..", "^^.^.^.....^^.^.^", ".......^^....^.^.", ".......^^^.^.^^.^"}
Returns: 4
Submissions are judged against all 114 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class EllysKeys with a public method int getMax(vector<string> keys) · 114 test cases · 2 s / 256 MB per case