Synnerg Lifeform
Time limit1sMemory limit128 MB
Given rewriting rules that merge adjacent synnergs with multiplicative lifetimes, find all maximum-lifetime synnergs obtainable by fully unifying some contiguous block of each input sequence.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Intervals, String, Implementation
- Solved
- No attempts yet
Problem
Scientists discovered a new kind of microscopic lifeform and named them synnergs. Only a few facts about synnergs are known:
- There is more than one type of synnerg.
- Every newborn synnerg has the same lifetime. In this problem a newborn synnerg has a lifetime of 1.
- Two adjacent synnergs can unify into a single new synnerg. When a synnerg of type and a synnerg of type unify, they become a target synnerg whose lifetime is , where is an amplification factor. The target type may differ from either source. Unification is governed by a fixed set of rules; each rule gives a target type, its two source types, and the amplification factor . A rule is not sensitive to the order of its sources: a rule with sources and applies to an adjacent pair no matter which side or is on.
An example set of rules:
When two or more newborn synnergs are placed in a sequence, each synnerg may unify with its left or right neighbour, making the sequence shorter. Unification repeats recursively, and there can be many different ways to unify the same sequence, yielding different lifetimes.
Example unification steps:
In the first example the newborn sequence a a b b (lifetimes 1 1 1 1) unifies into aa[a a] bb[b b] using rules 1 and 3, where aa has lifetime and bb has lifetime . Then aa bb unifies into x[aa bb] using rule 4, with lifetime .
In the second example the same newborn sequence instead unifies into AA[a a] bb[b b] using rules 2 and 3, with lifetimes and . This sequence is final (no further unification is possible) even though it is not fully unified into one synnerg, and its AA has a longer lifetime than the fully unified x above.
Given a newborn sequence, find every synnerg with the maximum possible lifetime that can be produced by fully unifying some contiguous part of the sequence. The part may be the whole sequence or only a sub-sequence, and the result need not be completely unified. A synnerg is creatable when some contiguous block of the input can be unified, step by step, into that single synnerg; its lifetime depends on the unification order, so for each (type, block) take the largest achievable lifetime. Report all synnergs whose lifetime equals the overall maximum.
Input
The input has two parts separated by a blank line.
Part 1 — unification rules. Each line is one rule with four space-separated fields: the target type, source type #1, source type #2, and the amplification factor. Each type is a string of at most 20 alphanumeric characters. The amplification factor is a positive integer at most 100.
Part 2 — sequences. Each line is one newborn sequence: synnerg types separated by spaces. Every synnerg in a sequence starts as a newborn with lifetime 1.
A blank line (or the end of input) terminates the list of sequences.
Output
For each input sequence print two parts:
- First, a line with the number of maximum-lifetime synnergs, a space, and the maximum lifetime itself.
- Then one line per solution. Each line has three space-separated fields: the synnerg type, its starting offset, and its finishing offset (both 1-based and inclusive) in the sequence. Sort the solutions in ascending order by the starting offset, then the finishing offset, then the type. The two offset comparisons are numeric; the type comparison is lexicographic (ASCII) order.
Notes
The example uses the 6 rules above and 5 sequences.
- For
a a b b, even though it can be fully unified intox[aa[a a] bb[b b]]at offsets 1 to 4 with lifetime , that is smaller than the34ofAA[a a]at offsets 1 to 2, so the answer isAA 1 2. - For
a a b b athere are two solutions:AAat offsets 1 to 2 andxat offsets 1 to 5, both with lifetime 34. - For
a a b b a athere is one solution:xat offsets 1 to 6 with lifetime 70. - For the sequences containing
c, remember that rule 6 (targetcfrom sourcescanda) is order-insensitive, so it applies whenever an adjacentcandaoccur in either order.