## CCC '19 J5 - Rule of Three

View as PDF

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

Problem types
##### Canadian Computing Competition: 2019 Stage 1, Junior #5

A substitution rule describes how to take a sequence of symbols and convert it into a different sequence of symbols. For example, , is a substitution rule which means that can be replaced with . Using this rule, the sequence would be transformed into the sequence (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:

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

where the symbols to be replaced are shown in bold. More specifically, from the initial sequence , substitute rule starting at position , to get the result . From , substitute rule , starting at position , to get the result . From , substitute rule , starting at position , to get the result . From , substitute rule , starting at position , to get the result , 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, followed by another sequence of A's and B's. Both sequences will have between one and five symbols.

The next line will contain three space separated values, , and . The value is an integer specifying the number of steps that must be used, and the values (the initial sequence) and (the final sequence) are sequences of A's and B's, where there are at least one and at most symbols in and at least one and at most symbols in .

For of the marks available, .

For an additional of the available marks, .

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 lines long and describes the substitutions in order.

Line of the output will contain three space-separated values, , , and :

• is the substitution rule number (either , or ) that will be used.
• is the starting position index of where the substitution rule will be applied in the sequence. Notice that the string is -indexed (i.e., the first character of the string is at index ).
• is the sequence that results from this substitution. Specifically, is the sequence of symbols that results by applying substitution rule starting at position from the previous sequence of symbols, , where we define to be the initial sequence . Note that , the final sequence.

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

#### Sample Input

AA AB
AB BB
B AA
4 AB AAAB

#### 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

• commented on Nov. 5, 2023, 3:30 a.m.

this question is so hard that i did the funny

• commented on Sept. 19, 2021, 6:18 a.m.

Could an editorial be opened for this problem?

• commented on Sept. 20, 2021, 12:40 a.m.

The chances of that are pretty small, since only 3 people have AC'd the problem with the strong data. However, you already have a 15/30 submission which would get you AC on the original cemc data.

• commented on Feb. 16, 2021, 7:42 p.m.

Hi!

I'm new to this site, I use Python 3, and I'm trying to solve this problem, which has been getting on my case for a while now. XD. Is there anybody who could share their code so that I can understand how you solved it?

Thanks!

P.S. I'm not trying to cheat here. I only want to know how you guys solved this problem.

• commented on April 6, 2021, 1:50 a.m. edited

I would recommend trying to get 15/30 - that would allow you to pass this problem on the contest it was given. DMOJ has modified the test cases, so this problem is a lot harder. Only 3 people on the entire website have gotten 30/30. The general solution most people seem to use is a brute force with some optimisations.

• commented on June 15, 2021, 5:14 p.m. edited

It's actually 5 people, go to all submissions and change the filter submissions status to "Accepted." There seems to be a glitch where best submissions only shows 3 AC.

• commented on June 16, 2021, 5:32 a.m.

This is not a bug. Users who attempt to plagiarize or otherwise cheat the points system will be unlisted from most of the ranking boards on DMOJ.

• commented on June 15, 2021, 5:28 p.m. edited

It's because the other 2 users are unlisted.

• commented on Feb. 11, 2020, 4:02 p.m. edited

If you brute force isn't it still ?

• commented on Feb. 12, 2020, 12:29 a.m. edit 2

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

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

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

• commented on Dec. 17, 2019, 10:11 p.m.

This question really should be worth much more points.

• commented on Dec. 16, 2019, 6: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.

• commented on Jan. 17, 2021, 3:55 a.m.

Could you please clarify what you mean by "hashing based on number of steps and the current string"? I'm also trying to speed up my solution with memoization, but I can't get it to work

• commented on June 5, 2021, 6:07 p.m.

What I did to memoize was based on the hash of the current string and the number of steps it took to get there.

• commented on Dec. 17, 2019, 5: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.

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

Just 8 score...

• commented on Feb. 2, 2020, 9:33 p.m.

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

• commented on Oct. 4, 2019, 9:15 p.m.

How does one not TLE?