简体   繁体   English

图灵机算法

[英]Turing Machine Algorithm

Could you please help me?请你帮助我好吗? I need to write code for a one-tape Turing Machine that uses the following two-letter alphabet a and b.我需要为使用以下两个字母 a 和 b 的单磁带图灵机编写代码。
So the programme should show the common prefix of the two words.所以程序应该显示两个词的共同前缀。
For example:例如:
g(aab,aaaba) -> aa; g(_,abab) -> _; g(aaba,baa) -> _; g(_,_) -> _; g(babaab,babb) -> bab
Where g is the function of the Machine and underscore means an Empty word, between words we have space其中g是机器的function,下划线表示一个空词,词之间有空格
I tried to implement the following option:我试图实现以下选项:
If at the start we see the letter a , then we erase it and move to the beginning of the second word.如果在开头我们看到字母a ,那么我们将其删除并移至第二个单词的开头。 If we also see a letter a there, we erase it too and after both words we write a through a space.如果我们还在那里看到一个字母a ,我们也会将其删除,并且在两个单词之后,我们通过一个空格写一个a。 After that we return to the beginning of the first word and repeat this operation.之后我们回到第一个单词的开头并重复这个操作。 When the first letter of the first word and the first letter of the second no longer match, we erase everything that is left.当第一个单词的第一个字母和第二个单词的第一个字母不再匹配时,我们会删除剩下的所有内容。 But I have some troubles with code, because after each operation a space between two words gets longer and I don't know how to control this.但是我在代码上遇到了一些麻烦,因为每次操作后两个单词之间的空格会变长,我不知道如何控制它。 Also there is a trouble when the first or the second word is a common prefix fully, like this:当第一个或第二个单词完全是公共前缀时也会出现问题,如下所示:
g(baa,baabab) -> baa

Your approach seems reasonable.你的方法似乎很合理。 From your description it sounds like you just have trouble generalizing some of the individual steps.从您的描述看来,您只是难以概括某些单独的步骤。

For instance, to deal with the growing spaces between the two words, remember that at any time in the program, the two words are separated by one or more spaces.例如,要处理两个单词之间不断增长的空格,请记住,在程序中的任何时候,这两个单词都被一个或多个空格分隔。 So implement your seek operation for that general case.因此,针对该一般情况实施您的查找操作。

For the common prefix case you have to deal with the situation that you eventually run out of characters to compare.对于常见的前缀情况,您必须处理最终用完字符来比较的情况。 So after deleting the character from the first word, while seeking for the beginning of the second word, check whether the first character you pass over is a letter or a space.所以从第一个单词中删除字符后,在寻找第二个单词的开头时,检查你传递的第一个字符是字母还是空格。 If it's a space, you're in the prefix case and need to take care that you don't try to seek back to the first word later, because you already erased all of it and there's only spaces left.如果它是一个空格,那么你在前缀的情况下,需要注意你以后不要试图回到第一个单词,因为你已经删除了所有的单词,只剩下空格了。 Similarly, if the second word is the prefix, you can detect this when seeking to the output.同样,如果第二个单词是前缀,您可以在查找 output 时检测到这一点。

Try breaking the algorithm down into its essential steps and test each of those steps in isolation.尝试将算法分解为其基本步骤,并单独测试每个步骤。 It is much easier to make sure you handle the corner cases correctly when you can focus on a simple step in isolation, instead of having to test it as part of the larger algorithm.当您可以孤立地专注于一个简单的步骤时,确保正确处理极端情况会容易得多,而不必将其作为更大算法的一部分进行测试。 Doing this is an essential skill in debugging code, so consider this a good exercise for that.这样做是调试代码的一项基本技能,因此认为这是一个很好的练习。 Even if it seems painful at first, make sure you have a structured approach to analyzing problems and breaking your code down into smaller parts, and you will be able to fix any problems eventually.即使一开始看起来很痛苦,也要确保你有一种结构化的方法来分析问题并将代码分解成更小的部分,最终你将能够解决任何问题。 Happy coding!快乐编码!

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

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