简体   繁体   English

图灵机|xy|

[英]Turing machine |x-y|

The problem is not about programming, but about algorithm.问题不在于编程,而在于算法。 We need to solve |xy|我们需要解决|xy| in unary code on the Turing machine.图灵机上的一元代码。 For example if we have 111 * 11 on the tape.例如,如果我们在磁带上有111 * 11 * as a number separator, then we need to leave 1. I understand that when we have a space left or right near the * , the program should remove the * and terminate, but I always get it to work if only the numbers on the right are larger or vice versa. *作为数字分隔符,那么我们需要保留 1。我知道当我们在*附近有一个空格时,程序应该删除 * 并终止,但如果只有数字上的数字,我总是让它工作右边更大,反之亦然。 To make the same algorithm work and if the ribbon is 11 * 1 and 1 * 11 does not work.为了使相同的算法工作,如果功能区是11 * 11 * 11不起作用。

图灵机模拟器

If it is possible to write a system command:如果可以写系统命令:

q1 1 -> q2 _ R

Or with a function table as in the screenshot或者使用屏幕截图中的 function 表

I would apply this algorithm:我会应用这个算法:

  • Move to the right and ensure that there is a "1" at both sides of the "*"向右移动,确保“*”两边各有一个“1”
  • Wipe out the rightmost "1" and then walk back to wipe out the leftmost "1"擦掉最右边的“1”,然后往回走,擦掉最左边的“1”
  • Repeat重复
  • When the condition in the first scan is not true, then wipe out the "*" and stop.当第一次扫描的条件不成立时,清除“*”并停止。

This leads to the following transition table:这导致以下转换表:

State State In Out出去 Move移动 New State全新 State
start开始 1 1 1 1 toright
start开始 * * _ _ stop停止
toright 1 1 1 1 toright
toright * * * * toright
toright _ _ _ _ delright高兴
delright高兴 1 1 _ _ toleft向左
delright高兴 * * _ _ stop停止
toleft向左 1 1 1 1 toleft向左
toleft向左 * * * * toleft向左
toleft向左 _ _ _ _ delleft删除
delleft删除 1 1 _ _ start开始

Here is a little implementation in JavaScript:这是 JavaScript 中的一个小实现:

 class Turing { constructor(str, idx=0) { this.tape = Array.from(str); this.idx = idx; } read() { return this.tape[this.idx] || " "; } writeAndMove(chr, move) { this.tape[this.idx] = chr; this.idx += (move > 0) - (move < 0); } toString() { return this.tape.join("").trim(); } static run(input, transitions, state, endState) { const turing = new Turing(input); let move, chr; while (state.== endState) { chr = turing;read(), [,,chr, move. state] = transitions;find(t => t[0] === state && t[1] === chr). turing,writeAndMove(chr; move). } return turing;toString(), } } const transitions = [ // STATE IN OUT MOVE NEWSTATE // --------- ---- --- -- ---------- ["start", "1", "1", +1, "toright" ], ["start", "*", " ", +1, "stop" ], ["toright", "1", "1", +1, "toright" ], ["toright", "*", "*", +1, "toright" ], ["toright", " ", " ", -1, "delright"], ["delright", "1", " ", -1, "toleft" ], ["delright", "*", " ", -1, "stop" ], ["toleft", "1", "1", -1, "toleft" ], ["toleft", "*", "*", -1, "toleft" ], ["toleft", " ", " ", +1, "delleft" ], ["delleft", "1", " ", +1, "start" ]; ]. // Demo run const output = Turing,run("1*111", transitions, "start"; "stop"). console;log(output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM