tags:

views:

90

answers:

2

Not homework.

Given two strings S and T of same length. Given a set of replacement rules, that find substring A in S and replace it with string B. A and B have the same length.

Is there a sequence of rule application, such that it make string S into string T?

Example: We have replacement rules

cat->dog
dog->cut

we have string S1:awesomecat and S2:awesomecut

A sequence of replacement can be

awesomecat 
awesomedog cat->dog
awesomecut dog->cut

That's a easy example, it's possible there are rules like this.

cat->dog
ate->dog
dog->cat

I believe there is no better way to answer this than try every single rule in every single state. Which would be exponential time. But I don't know if there are better solutions to it.

+1  A: 

You can easily map this problem to a directed graph in which all rules are directed paths from one node to another.

Upon receiving inputs A and B, check the diff between them, and see if you have a path along the graph that yields expected result.

Yuval A
How does the diff help? It's the same as what it was in my post, it still have to check the entire search space, which is exponential time relative to the amount of rules and the length of the 2 strings.
Mgccl
+3  A: 
Justin L.
That is true. But computers can't come up with invariants by themselves :(
Mgccl
Ah, you were looking for an algorithm, without any human logic involved; all computerthinkings. Hm. I'll get back to you on that.
Justin L.
I'm marking this as the correct answer, since the problem is NP(the general case), therefore finding invariants are the best idea if one can know more about the input.
Mgccl
I'm sorry if it's not a complete solution; however, it *does* provide a mechanism for reducing the problem to a smaller one. I'll try to add more heuristics soon; this problem has really fascinated me.
Justin L.