Problem Statement
????
An image editing application allows users to construct images containing multiple layers. When dealing with large images, however, it is sometimes necessary to limit the number of layers due to memory constraints. If certain layers will not be altered during an editing session, they can be merged together to reduce the total number of layers in memory. You are given a macro containing commands to open files and merge layers. Each time a file is opened, it is loaded into a new layer of the current image. Layers are numbered starting at 0 for the bottommost layer, 1 for the layer directly on top of the bottommost layer, etc... Whenever a new layer is created, it is positioned on top of the previous topmost layer. An open command is formatted as "OPEN filename" where filename is the name of the file to open. Consecutive layers can be merged using the merge command, which is formatted as "MERGE layer1-layer2", where layer1 and layer2 specify an inclusive range of layer numbers that exist in the current image. After multiple layers are merged, all the layers are renumbered according to the original specification. For example, if an image contains four layers (0, 1, 2, 3), and layers 1 and 2 are merged into a single layer, the final image will contain three layers numbered 0, 1, 2 from bottom to top, where layer 0 is the same as before, layer 1 was previously layers 1 and 2, and layer 2 was previously layer 3.
Given the String[] macro, perform all the operations in the macro in order and return the final state of the image layers as a String[]. The String[] should contain exactly the same number of elements as there are layers in the final image, and each element i should correspond to the ith layer. Each element of the String[] should be a single space delimited list of the filenames contained in that layer. The filenames should be sorted in alphabetical order within each layer.
Definition
????
Class:
ImageLayers
Method:
contents
Parameters:
String[]
Returns:
String[]
Method signature:
String[] contents(String[] macro)
(be sure your method is public)
????
Constraints
-
macro will contain between 1 and 50 elements, inclusive.
-
Each element of macro will be formatted as either "OPEN filename" or "MERGE layer1-layer2".
-
Each filename in macro will contain between 1 and 15 lowercase letters ('a'-'z'), inclusive.
-
Each layer1 and layer2 in macro will be integers between 0 and n-1, inclusive, with no leading zeros, where n is the number of layers that exist in the image immediately before the command is executed.
-
Within each element of macro that represents a merge command, layer1 will be less than layer2.
-
The first element of macro will be an OPEN command.
Examples
0)
????
{"OPEN background",
"OPEN aone",
"OPEN atwo",
"OPEN foreground",
"MERGE 0-2",
"OPEN border"}
Returns: {"aone atwo background", "foreground", "border" }
After the first four commands in macro are executed, the layers are (from bottom to top):
0. background 1. aone 2. atwo 3. foreground
The merge command combines the bottom three layers into a single layer. There are now only two layers (from bottom to top):
0. background aone atwo 1. foreground
Finally, one last file is opened and placed in a new layer on top. The final return value contains the filenames within each layer sorted alphabetically:
0. aone atwo background 1. foreground 2. border
1)
????
{"OPEN sky",
"OPEN clouds",
"OPEN ground",
"MERGE 0-1",
"OPEN grass",
"MERGE 0-2",
"OPEN trees",
"OPEN leaves",
"OPEN birds",
"MERGE 1-2",
"MERGE 0-1"}
Returns: {"clouds grass ground leaves sky trees", "birds" }
2)
????
{"OPEN a", "OPEN b", "OPEN a", "OPEN a", "MERGE 0-3"}
Returns: {"a a a b" }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.