Editorial for DMOPC '19 Contest 1 P5 - Broken String


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: KevinWan

For subtask 1, it suffices to print out the string achieved by appending all the strings together.

Time complexity: O(N)

For subtask 2, we can use a bitmask-DP to create such a string. If we have dp[i][mask] (where mask is a bitmask for which strings were already used) represent whether or not it's possible to create a string with the chosen strings in mask, where the last string chosen was the ith one. Thus, we have dp[i][mask] as 1 if and only if at least one of dp[j][mask(2i)] is 1 where sj is a string that has a suffix equivalent to the prefix of si. This can be checked in O(N) time by looping through all the given strings.

Time complexity: O(N22N)

For subtasks 3 and 4, we can represent the given strings as a graph where each node represents a given K1 length string, and each string is an edge connecting its K1 length prefix to its K1 length suffix. Thus, a solution string is represented by an Euler path or an Euler circuit in the graph. For subtask 3, we only need to determine the existence of an Euler path/circuit, while to get full points, we must determine a possible Euler circuit in the graph.

Time complexity: O(NK) or O(26K1+NK)


Comments

There are no comments at the moment.