问题
Can anyone help :(
L = {ww^r | w element of (0,1)*}
Would it be like in :pseudo code, check if the string is equal to itself in reverse?
回答1:
This can be done with a PDA or a TM. I'll assume you need a deterministic TM, for what I'm assuming is homework.
The strategy is to find the length of the string to determine where the middle is, then treat the tape as a stack to see if all of the characters match before and after that midpoint.
Here's an algorithm that can do it:
- For every two input characters you read until you hit
#
, write a0
after the last#
. This will keep track of where the "middle" is. Mark the inputs as you read them. - If you hit
#
where you expected to find a number, the string length is odd, and so the machine can halt and reject. - Otherwise, write another
#
after your string of zeros. - Unmark all inputs and move the read head to the second
#
. - Using the number of zeros you have, mark the right half of the input.
- Starting in the middle, working your way out, mark the left character and unmark the right if they match.
- If you encounter a pair that doesn't match, halt and reject.
- If you get all of the way to the
#
s, halt and accept.
For example, let's look at input #101100#
(#
here means the edges of the input). The read head is at the first #
.
- Input starts as
# 1 0 1 1 0 0 #
(leaving room for markers) - Mark the first two inputs, write a 0 at the end:
# 1'0'1 1 0 0 # 0
- Mark the next two inputs, write a 0 at the end:
# 1'0'1'1'0 0 # 0 0
- Mark the next two inputs, write a 0 at the end:
# 1'0'1'1'0'0'# 0 0 0
- You've found the second
#
, so the string is of even length. Write a # at the end:# 1'0'1'1'0'0'# 0 0 0 #
- Unmark everything:
# 1 0 1 1 0 0 # 0 0 0 #
- Now, starting from the second
#
, mark the inputs from the right, and the0
s from the left until all of the0
s are marked: - First iteration:
# 1 0 1 1 0 0'# 0'0 0 #
- Second:
# 1 0 1 1 0'0'# 0'0'0 #
- Third:
# 1 0 1 1'0'0'# 0'0'0'#
- You now know the midpoint of the string (it's where an unmarked input is to the left of a marked input).
- Starting from that midpoint, mark the left and unmark the right if they match:
# 1 0 1'1 0'0'# 0'0'0'#
- Continue working outward, comparing in this way:
# 1 0'1'1 0 0'# 0'0'0'#
- On the third step, we find that
1
and0
don't match. Halt and reject.
I'll leave it as an exercise to you to determine how to convert this algorithm into a TM.
来源:https://stackoverflow.com/questions/59098293/design-a-turing-machine-that-accepts-l-wwr-w0-1