Prolog - PCP 求解器
我想知道是否有一种(可以理解的)方法可以使用序言谓词强力解决邮政通信问题?
例如:
?- pcp(["1","11"),("10111","101")], S).
S = [2,1,1]
I'm wondering if there's an (understandable) way to brute force solve Post correspondence problem using prolog predicates?
for example:
?- pcp(["1","11"),("10111","101")], S).
S = [2,1,1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是一个可能的程序,它使用广度优先搜索来找到问题的越来越大的解决方案。
从大小为 1 的解决方案开始搜索
尝试在当前大小下找到解决方案,如果找不到,请尝试下一个尺寸
如果在正确大小的解决方案末尾,字符串完全匹配,则问题是 检查解决
方案的一大步:从字符串元组列表中获取解决方案中索引的元组元素,将该元组中的字符串附加到上一步的字符串中,匹配所有相同的内容,至少留下一个字符串清空,然后继续解决方案的下一部分。 (显然,如果字符串在某个时刻不匹配,则 matchreduce 将失败。)
以下是其余谓词:
append 和 nth1 谓词位于列表库 (SWI-Prolog) 中,但可以轻松实现!
这是您的测试用例:
以及来自维基百科的几个测试用例:
Ok, here's a possible program, which uses breadth first search, to find increasingly bigger solution to the problem.
Start the search at solutions of size 1
Try finding a solution at the current size, and if you don't find one try the next size
If at the end of your solution of the correct size, the strings are matched completely then the problem is solved
Big step for checking the solution: get the tuple element indexed in the solution from the list of string tuples, append the strings in this tuple to the strings from the last step, match everything that's the same, leaving at least one of the strings empty, then go onto the next part of the solution. (Obviously, if the strings don't match at some point matchreduce will fail.)
Here are the rest of the predicates:
The append and nth1 predicates are in the lists library (SWI-Prolog) but can be implemented easily!
Here's your test case:
And a couple from wikipedia: