Another Problem

3.8

4 votes
Depth First Search, Algorithms, Graphs
Problem

While working on his project, Rishi ended up with an interesting problem as follows -

There is a string S which is broken down into N chunks (substrings) satisfying the below constraints:

1. For each chunk except the first, starting 4 characters will be overlapping with ending 4 characters of atleast one chunk.
2. For each chunk except the last, ending 4 characters will be overlapping with starting 4 characters of atleast one chunk.

Image for the reference:

Your task is to find original message from the given chunks. If multiple solutions exists, then print all of them in lexicographical order.

Note: Number of chunks and overlaps in each test case are generated in a way that it will always pass within given time constriants.

Input Format:

First line contains an integer N - the number of chunks
The next N line's contain a string each - representing a chunk

Output Format:

Print all the possible unique original messages in lexicographical order

Constraints:

1N5004len(chunk)100

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Arrange the chunks in the sequence as follows: "opens", "pensource", "urcekaruya", and finally "ruyadsa". This arrangement ensures that overlapping characters in each chunk are used to form the original message "opensourcekaruyadsa".

Editor Image

?