#K63547. Reversal Segment to Sort Books
Reversal Segment to Sort Books
Reversal Segment to Sort Books
You are given a list of n book names. Your task is to determine whether there exists a continuous segment of the list which, if reversed, would sort the entire list in alphabetical order. The book names are compared using standard lexicographical order. If such a segment exists, output its 1-indexed starting and ending positions. Otherwise, print "no solution".
Detailed Explanation: Suppose we have a list of books. First, we sort a copy of the list in ascending alphabetical order. Then, by comparing the original list with the sorted list, we attempt to find the first and last position where the two lists differ. These positions outline the potential segment that, when reversed, might yield a sorted list. If reversing this segment indeed sorts the list, output the segment’s bounds (1-indexed). If not, or if the list is already sorted, output "no solution".
Note: The positions in the output are 1-indexed (i.e., the first element is at position 1).
inputFormat
The input is read from stdin and consists of:
- An integer
n
representing the number of books. n
lines, each containing a single book name (a non-empty string).
outputFormat
If a valid segment exists, print two integers separated by a space representing the 1-indexed positions of the segment to reverse that sorts the list. Otherwise, print no solution
.
5
apple
elderberry
cherry
banana
fig
2 4
</p>