#K55332. Plant Collection Summary
Plant Collection Summary
Plant Collection Summary
You are given a collection of plant records. Each record is a string that consists of the plant's name and a positive integer count, separated by a space. The plant's name can consist of one or more words.
Your task is to summarize the collection by normalizing each plant name to a three‐letter code and aggregating the counts for plants with the same code. The normalization works as follows:
- If the name consists of a single word, take the first three letters and convert them to uppercase.
- If the name consists of two words, take the first letter of the first word and the first two letters of the second word (all converted to uppercase).
- If the name consists of three or more words, take the first letter of each of the first three words (all converted to uppercase).
If any record's plant name is exactly "Cactus" (case-insensitive), then the entire input is considered invalid and you should output "Invalid data".
After processing all records, if the data is valid, print the list of plant codes and their aggregated counts sorted in lexicographical order by the plant code. All data is provided via stdin and output must be printed to stdout.
Note: The input starts with an integer n on a single line indicating the number of plant records that follow. If n is 0, output an empty line.
inputFormat
The first line of input contains an integer n, which is the number of plant records. Each of the next n lines contains a plant record in the format:plant_name count
where plant_name
may contain multiple words and count
is a positive integer.
outputFormat
If any plant record has a name that is exactly "Cactus" (case-insensitive), output a single line with Invalid data
. Otherwise, output a single line with the sorted plant codes and their cumulative counts. Each code and count should be separated by a space.
6
Rose 5
Tulip 3
Sunflower 7
Rose 2
Tulip 9
Orchid 4
ORC 4 ROS 7 SUN 7 TUL 12