CCC '19 J5 - Rule of Three

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 1G

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

A substitution rule describes how to take a sequence of symbols and convert it into a different sequence of symbols. For example, ABA \to BBB, is a substitution rule which means that ABA can be replaced with BBB. Using this rule, the sequence A\textbf{ABA}A would be transformed into the sequence ABBBA (the substituted symbols are in bold).

In this task, you will be given three substitution rules, a starting sequence of symbols and a final sequence of symbols. You are to use the substitution rules to convert the starting sequence into the final sequence, using a specified number of substitutions.

For example, if the three substitution rules were:

  1. AA \to AB
  2. AB \to BB
  3. B \to AA

we could convert the sequence AB into AAAB in 4 steps, by the following substitutions:

\displaystyle \textbf{AB} \to \textbf{B}B \to AA\textbf{B} \to AA\textbf{AA} \to AAAB,

where the symbols to be replaced are shown in bold. More specifically, from the initial sequence AB, substitute rule 2 starting at position 1, to get the result BB. From BB, substitute rule 3, starting at position 1, to get the result AAB. From AAB, substitute rule 3, starting at position 3, to get the result AAAA. From AAAA, substitute rule 1, starting at position 3, to get the result AAAB, which is the final sequence.

Input Specification

The first three lines will contain the substitution rules. Each substitution rule will be a sequence of A's and B's, followed by a space following by another sequence of A's and B's. Both sequences will have between one and five symbols.

The next line contains three space separated values, S, I and F. The value S (1 \le S \le 15) is an integer specifying the number of steps that must be used, and the values I (the initial sequence) and F (the final sequence) are sequences of A's and B's, where there are at least one and at most 5 symbols in I and at least one and at most 50 symbols in F.

For 7 of the 15 marks available, S \le 6.

For an additional 7 of the 15 available marks, S \le 12.

Due to the official test data being weak, an additional subtask worth 15 marks has been added that consists of tests constructed to break solutions that are incorrect but AC on the official test data. Data are provided by d.

Output Specification

The output will be S lines long and describes the substitutions in order.

Line i of the output will contain three space-separated values, R_i, P_i, and W_i:

  • R_i is the substitution rule number (either 1, 2 or 3) that will be used.
  • P_i is the starting position index of where the substitution rule will be applied in the sequence. Notice that the string is 1-indexed (i.e., the first character of the string is at index 1).
  • W_i is the sequence that results from this substitution. Specifically, W_i is the sequence of symbols that results by applying substitution rule R_i starting at position P_i from the previous sequence of symbols, W_{i-1}, where we define W_0 to be the initial sequence I. Note that W_S = F, the final sequence.

There will always be at least one sequence of S substitutions that will convert I into F. If there is more than one possible sequence of substitutions, any valid sequence will be accepted.

Sample Input


Possible Output for Sample Input

2 1 BB
3 1 AAB
3 3 AAAA
1 3 AAAB

Explanation of Output for Sample Input

This is the example outlined in the problem description. Note that the following is another possible valid substitution sequence:

2 1 BB
3 2 BAA
1 2 BAB
3 1 AAAB

Specifically, showing the substitutions in bold, we get

\displaystyle \textbf{AB} \to B\textbf{B} \to B\textbf{AA} \to \textbf{B}AB \to AAAB.


  • 2
    Rainier  commented on Aug. 12, 2020, 4:42 a.m. edit 2

    Could somebody share their solution for this problem in c++. I have a hard time imagining how I would approach this.Thx.

  • 1
    kevinlu1248  commented on Feb. 11, 2020, 11:02 a.m.

    If you brute force isn't it still O(3^S)~1e7?

    • 3
      j9292002  commented on Feb. 11, 2020, 7:29 p.m. edit 2

      hello kevin! it is more than that I think example: bbbbbbbbbbb. more that one way to do b->aa

  • 9
    Neon  commented on Feb. 2, 2020, 4:35 p.m.

    Why is the time limit so low? Isn't it supposed to be at least 2.0s?

  • 3
    singbeilii  commented on Feb. 2, 2020, 1:50 p.m. edit 4

    This problem is so hard

  • 24
    const  commented on Dec. 17, 2019, 5:11 p.m.

    This question really should be worth much more points.

  • 13
    yeerk16  commented on Dec. 16, 2019, 1:16 p.m.

    I should stop working on this one (it's putting me behind in other tasks, oops!), but I can't get in under the 1 second time limit. My solutions all use recursion-based DFS. I started with Python3, converted to C, added memoization (hashing based on number of steps and the current string), added bidirectional/meet-in-the-middle search... each of these steps has helped, but not sufficiently. Now I'm stuck. Can anyone help? I've learned a lot with this problem, and I'm grateful for that, but it would be nice to see how to solve this more quickly so that I can fully move on.

    • 11
      souradeepsahaf9466880b1d54979  commented on Dec. 17, 2019, 12:40 a.m.

      I'm trying this problem for around 6 months now, and did all the things you've done but still can't pass the last test case. One other thing I did is calculating the number of times you can apply a rule by creating systems of equations and solving them. You might try that out as well, but I don't think it'll be very useful.

  • 12
    YidiChen  commented on Nov. 5, 2019, 5:13 p.m. edit 3

    Just 8 score...

    • 3
      Neon  commented on Feb. 2, 2020, 4:33 p.m.

      use memoization, you shud be able to get 15/30

  • 20
    pblpbl  commented on Oct. 4, 2019, 5:15 p.m.

    How does one not TLE?

  • 7
    souradeepsahaf9466880b1d54979  commented on May 9, 2019, 6:37 p.m.

    Ideas anyone? I'm using recursion and getting TLE. Thanks!

  • 0
    edmondyu01  commented on May 9, 2019, 10:40 a.m.

    Elegant how they changed the point value to 15 from 35.

  • -4
    TonyXer  commented on March 29, 2019, 10:38 a.m. edited

    Can someone tell me when people can get an AC with an incorrect answer? (i.e. you get AC but you don't get full marks)

    • 7
      Rimuru  commented on March 29, 2019, 5:28 p.m.

      For example, when your code is correct for some answers.

      • -2
        TonyXer  commented on March 30, 2019, 9:28 p.m.

        But wouldn't that be a WA? And why am I getting downvotes...

        • 3
          31501357  commented on April 26, 2019, 4:51 p.m.

          There could be a mistake in your code. However, the original test data would never be able to exploit your mistake, and that's how you could get away with an incorrect answer.