Problem D
Hyphenland
In a distant country called Hyphen-Land, a benevolent dictator made a rule stating that upon marriage, every pair of people must hyphenate their names to make them identical by last-name. This was lauded both as a symbol of equality among spouses, and also a great way to manage people’s identities and track lineages. As a result, the children of each set of parents will have both their parents’ last names, hyphenated together. This pattern is to continue over every generation. For the case of this question, keep in mind that each “name” that is mentioned refers to a last name that exists within hyphens when applicable.
For example, if two individuals marry and their last names are “Smith” and “Doe” respectively, their resulting last name is “Smith-Doe”.
Speaking of managing people’s identities, this nation’s national register of citizens now has a different problem. Their task has evolved from keeping track of names and identities, to figuring out how to even contain these names in their databases. After all, they only have finite computing capabilities. Because last names are the issue, we will not consider first names.
The solution that Hyphen-Land’s engineers came up with was to reduce the length of each last name. So, as we add more and more last names, we must remove the ends of older last names. Keep in mind that the hyphens themselves don’t consume any data, so don’t count them in your name-size. Future last names may have hyphens in them too, but don’t count them either.
You are likely to have ties in your effort to shorten names. Here is the order in which you should break the ties:
-
Reduce the longest portion first.
-
If tied in length, reduce the leftmost one with longest length.
In order to ensure that history is not altogether, you must keep at least letter from each name.
Now, you need to write a program that implements this. You will start with a given last name, and then be given multiple new last names occuring due to marriages. After all changes, print the last name. Your organization will also go through multiple generations, with each one having a fixed length limit. Sometimes, it will be impossible to respect the length limit along with the earlier rules. In this case, shorten the name as much as possible without remvoing all letters.
Input
The first line of the input contains the integer $M$ ($1 \le M \le 100$), the number of generations.
The second line contains the initial last name.
Then, all $M$ generations are described.
Each generation start with the integers $g$ and $n$ ($1 \leq g \leq 50$, $1 \leq n \leq 10$), the maximum size (in number of letters) of each name and the number of new names in this generation.
Then, the following $n$ lines each contain one such name.
No name starts or ends with a hyphen or contains two hyphens in a row. Additionally, all names consist of at most $100$ lower or uppercase English letters a-z, A-Z, or hyphens -.
Keep in mind that hyphens don’t consume any space, so don’t count them in any size-calculations you make. Assume no remarriages (for the sake of simplifying the problem) and that we are working with immediate descendants only. Additionally, it holds that the sum of all $n$ is not larger than $200$.
Output
Print the final last name, appropriately hyphenated.
| Sample Input 1 | Sample Output 1 |
|---|---|
1 Smith 10 1 Doe |
Smith-Doe |
| Sample Input 2 | Sample Output 2 |
|---|---|
2 Smith 10 2 Doe Bruce-Lee 20 1 Cho-Chang |
Sm-Do-Bru-Lee-Cho-Chang |
