2

I'm searching for the name and an efficient solution for the following problem: Assume I have a string s='abcdef' and a set of find/replace rules Pn

P1: ab -> xy
P2: xyc -> 123
P3: ef -> ab

Sequentially applying these rules to s I could arrive at the following strings:

1. xycdef
2. 123def
3. 123dab
4. 123dxy

My goal is to reach a "stable" state where all (most?) rules have been applied (here: 123dxy).

So my question is, is there a well defined approach for dealing with this type of problems? Are there general constraints on the rules to avoid infinite loops (eg, ab -> xy, xy -> ab). Is there a way to determine a bound for the maximum number of iterations?

Any pointers to relevant concepts/related work are appreciated.

ws6079
  • 343
  • 2
  • 11

1 Answers1

1

I would translate this into a graph problem.
In your case, I would have a node named ab, another xy, xyc, etc.
Directed edges exist between node according to the rules.
Here: V={ab, xy, xyc, 123, ef}; E={(ab,xy), (xyz.123), (ef, ab)}

Basic check:
If at this stage you have loops in your graph, then you have a real problem.

Prefixes:
The case with ab -> xy and xyc -> 123 gives us an issue where two rules are not a problem unless the given string is built in a certain way. (abc becomes xyc). This could be checked with directed edges that are marked in a certain way. If they create a loop, then you will have an issue with certain strings, but not with others.

Hope this helped.

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39
  • 1
    Thanks a lot for your answer. I was hoping that someone had stumbled upon - and solved - the exact problem already. It doesn't look like it, so I'll accept your answer and start coding. – ws6079 Sep 01 '16 at 13:36