Exercise · Volume 1 · 2.2.1-3
Exercise 2.2.1-3
not stated yet in Lean solution written
| Rating | 25 |
|---|---|
| Kind | K1, prove: show that a stated fact is true from the written solution |
| Section | Section 2.2.1 of Volume 1 |
The task
Give a simple test that tells admissible sequences of S and X from inadmissible ones. Show that two different admissible sequences never give the same output permutation.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Count the cars in the stack after each move.
Hint 2
For the second part, look at the first move where two sequences differ, and ask which car is in play.
Solution
Show the solution
Plan. Track the size of the stack. Then compare two sequences at the first move where they differ.
The test. A sequence with letters S and letters X is admissible if and only if, in every prefix, the number of X is at most the number of S.
The stack size after a prefix is the number of S minus the number of X in it. An X is possible only when the stack is not empty, that is, when the size before it is at least . So the size must stay at or more after every move, and this is the same as the stated rule. The count of S is , so no S is ever blocked by a lack of input cars. The count of X is also , so the stack is empty at the end and all cars have left.
Different sequences give different permutations. Let and be two different admissible sequences. Cut off the longest common prefix. After it, both stacks hold the same cars, and the same cars have entered. At the next move, one sequence has S and the other has X. Say has S and has X.
Let be the top car of the stack at this point. It exists, because is admissible and takes X. Let be the next car of the input. It has not entered yet in either sequence.
In , the car enters on top of , so leaves after . In , the car leaves now, and has not yet entered, so leaves before . The two outputs order the cars and in opposite ways. So they differ.
Check
For I listed all sequences with letters S and letters X. The prefix rule picked out the same sequences as a direct run of the stack. The outputs of these runs were all different, and they were exactly the permutations that a stack can give.
Remarks
The test is the same as the rule for a correct bracket sequence, with S as the left bracket and X as the right bracket. The one-to-one link between sequences and permutations is the base of the count in Exercise 2.2.1-4.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.