Недетерминирован конечный автомат является конечным автоматом , где кортеж отображаются в нескольких штатов. То есть. мы заменяем обычную функцию перехода DFA другой функцией .
Если вы знаете, что такое NFA, вы можете пропустить следующий раздел.
Формальное определение
НФА однозначно описывается
- конечный набор состояний
- конечный набор символов
- функция перехода
- начальное состояние
- a set of final states
The machine starts out in and reads a finite string of symbols , for each symbol it will simultaneously apply the transition function function with a current state and add each new set of states to the set of current states.
Challenge
For this challenge we will ignore to simplify it, furthermore the alphabet will always be the (lower-case) letters to and the set of states will be for some non-negative integer . The initial state will always be .
Given a word and a description of the NFA, your task is to determine all the final states.
Example
Consider the string and the following description:
state, symbol, new-states
0, 'a', [1]
1, 'a', [0]
1, 'b', [1,2]
The machine will start in :
- read an : new states
- read a : new states
- read an : new states
- read an : new states
- read a : new states
So the final states and thus the output would be .
Note: In step (2) the transition of state maps to as the description only includes transitions to non-empty sets.
Rules
The input will consist of a string and some kind of description of the NFA (without -transitions):
- the input string will always be element of
- valid inputs (not limited to):
- list/array of tuples/lists
- new-line separated input
- the description of the NFA will only contain transitions with non-empty sets as result
- you may abbreviate rules with the same characters if their result is the same (eg. rules
0,'a',[1,2]
and0,'b',[1,2]
could be abbreviated with0,"ab",[1,2]
- you may take each rule separate (eg. rule
0,'a',[1,2]
can be0,'a',[1]
and0,'a',[2]
)
- you may abbreviate rules with the same characters if their result is the same (eg. rules
- you may choose upper-case letters if you want
- you may take the number of states as input
- you may assume some kind of ordering of the inputs (eg. ordered by state or symbols)
The output will be a list/set/new-line separated output etc. of the final states
- order doesn't matter
- no duplicates (as it's a set)
Test cases
These examples will be in the format description word -> states
where description
is a list of tuples (state,symbol,new-states)
:
[] "x" -> []
[] "" -> [0]
[(0,'a',[1]),(1,'a',[0]),(1,'b',[1,2])] "abaab" -> [1,2]
[(0,'a',[1]),(1,'a',[0]),(1,'b',[1,2])] "abc" -> []
[(0,'p',[0,1]),(0,'g',[2]),(1,'c',[1]),(1,'g',[4]),(1,'p',[2]),(2,'c',[0])] "ppcg" -> [2,4]
[(0,'f',[1]),(1,'o',[1,2]),(2,'b',[3]),(3,'a',[4]),(4,'r',[0,4])] "foobar" -> [0,4]
[(0,'f',[1]),(1,'o',[1,2]),(2,'b',[3]),(3,'a',[4]),(4,'r',[0,4])] "fooooooobar" -> [0,4]
[(0,'f',[1]),(1,'o',[1,2]),(2,'b',[3]),(3,'a',[4]),(4,'r',[0,4])] "fobarfo" -> [1,2]
[(0,'f',[1]),(1,'o',[1,2]),(2,'b',[3]),(3,'a',[4]),(4,'r',[0,4])] "foobarrf" -> [1]
[(0,'d',[1,2]),(1,'u',[2]),(2,'u',[2,3]),(2,'p',[3]),(3,'p',[3])] "dup" -> [3]
[(0,'a',[0,2]),(0,'b',[3]),(1,'a',[1]),(1,'b',[1]),(2,'b',[1,4]),(4,'b',[2])] "aab" -> [3,1,4]
[(0,'a',[0,2]),(0,'b',[3]),(1,'a',[1]),(1,'b',[1]),(2,'b',[1,4]),(4,'b',[2])] "abb" -> [1,2]