Celestial Pattern Synthesis
The Celestial Pavilion commissions a digital mural, rendered as a long string of colored glyphs. Artists contribute repeating pattern snippets with different creative costs, and the curator must stitch copies of these snippets (allowing overlaps) to recreate the entire mural. Each snippet can appear multiple times, may start at any position, and overlaps must match glyphs exactly where they intersect. The total cost equals the sum of the snippet costs used, regardless of how many glyphs each copy covers. The curator wants not only the minimum total cost but, among all minimum-cost solutions, the lexicographically smallest sequence of snippet indices that achieves the mural when listed in the order their starting positions are applied. If no sequence can produce the mural, the response should indicate impossibility.
You receive the mural string and a list of snippets, each with its creative token cost. Indices in the final plan are 1-based according to the given snippet order. Copies of the same snippet may appear multiple times, but overlapping glyphs must match the mural exactly. The final plan should cover every glyph of the mural (allowing multiple snippets to overlap any position) and build it from left to right so that, at each step, the partial synthesis matches the mural prefix. Return the minimal cost and the lexicographically smallest snippet-index sequence achieving that cost. If impossible, return -1 with an empty sequence.
Construct your answer with dynamic programming that tracks both minimum cost and lexicographic order. Do not mutate the input arrays. Costs fit in 64-bit integers, and the mural length can reach 2e5 glyphs, requiring careful optimization. This computation helps the curator book the right artists, balance the creative budget, and ensure the synthesis path remains graceful under festival deadlines.
Example 1:
Input:
mural = "ababa"
snippets = [("ab",3),("aba",4),("ba",2)]
Output:
cost = 6
sequence = [2,3]
Explanation: Use snippet 2 starting at index 1, and snippet 3 starting at index 3, total cost 6. No other plan matches the mural with lower cost.
Example 2:
Input:
mural = "aaaa"
snippets = [("aa",3),("a",2)]
Output:
cost = 6
sequence = [2,2,2,2]
Explanation: Minimal cost 6 using four copies of snippet 2; although snippet 1 covers more at once, any minimal-cost plan equals lexicographic sequence [2,2,2,2].
Example 3:
Input:
mural = "abc"
snippets = [("ac",5),("bc",4)]
Output:
cost = -1
sequence = []
Explanation: No set of snippets can synthesize the mural, so return -1 and an empty sequence.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
