third attempt
This commit is contained in:
parent
77de00fd23
commit
841655da18
@ -20,17 +20,24 @@ class FSA:
|
|||||||
else:
|
else:
|
||||||
self.transitions[state_from] = dict()
|
self.transitions[state_from] = dict()
|
||||||
self.transitions[state_from][symbol] = {state_to}
|
self.transitions[state_from][symbol] = {state_to}
|
||||||
def get_final_state(self, string):
|
def get_final_state(self, string, start_state):
|
||||||
|
|
||||||
current_state = self.initial_state
|
current_state = start_state
|
||||||
for symbol in string:
|
for symbol in string:
|
||||||
current_state = self.transitions[current_state][symbol]
|
try:
|
||||||
|
for state in self.transitions[current_state][symbol]:
|
||||||
|
current_state = self.get_final_states(string[1:], state)
|
||||||
|
if current_state in self.final_states:
|
||||||
|
return current_state
|
||||||
|
except KeyError:
|
||||||
|
return -1
|
||||||
return current_state
|
return current_state
|
||||||
|
|
||||||
def add_final_state(self, state):
|
def add_final_state(self, state):
|
||||||
self.final_states.add(state)
|
self.final_states.add(state)
|
||||||
def accepts(self, string):
|
def accepts(self, string):
|
||||||
|
|
||||||
if self.get_final_state(string) in self.final_states:
|
if self.get_final_state(string, self.initial_state) in self.final_states:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
class NFA:
|
class FSA:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.initial_state = '0'
|
self.initial_state = '0'
|
||||||
self.final_states = set()
|
self.final_states = set()
|
||||||
|
self.transitions = dict()
|
||||||
self.transitions = dict()
|
self.alphabet = set()
|
||||||
self.alphabet = set()
|
|
||||||
|
|
||||||
def add_transition(self, state_from, state_to, symbol):
|
def add_transition(self, state_from, state_to, symbol):
|
||||||
|
|
||||||
if state_from in self.transitions.keys():
|
if state_from in self.transitions.keys():
|
||||||
if symbol not in self.transitions[state_from].keys():
|
if symbol not in self.transitions[state_from].keys():
|
||||||
self.transitions[state_from][symbol] = {state_to}
|
self.transitions[state_from][symbol] = {state_to}
|
||||||
@ -18,36 +16,25 @@ class NFA:
|
|||||||
else:
|
else:
|
||||||
self.transitions[state_from] = dict()
|
self.transitions[state_from] = dict()
|
||||||
self.transitions[state_from][symbol] = {state_to}
|
self.transitions[state_from][symbol] = {state_to}
|
||||||
|
|
||||||
def add_final_state(self, state):
|
def add_final_state(self, state):
|
||||||
self.final_states.add(state)
|
self.final_states.add(state)
|
||||||
|
def get_all_paths(self):
|
||||||
|
paths = []
|
||||||
|
stack = [('', '0')]
|
||||||
|
while stack:
|
||||||
|
current_path, current_state = stack.pop()
|
||||||
|
if current_state in self.final_states:
|
||||||
|
paths.append(current_path)
|
||||||
|
if current_state in self.transitions:
|
||||||
|
for symbol, states in self.transitions[current_state].items():
|
||||||
|
stack.extend((current_path + symbol, state) for state in states)
|
||||||
|
return sorted(paths)
|
||||||
|
|
||||||
def get_final_states(self, string):
|
fsa = FSA()
|
||||||
current_states = {self.initial_state}
|
|
||||||
for symbol in string:
|
|
||||||
current_states = {state for current_state in current_states for state in self.transitions.get(current_state, {}).get(symbol, set())}
|
|
||||||
return current_states
|
|
||||||
|
|
||||||
def accepts(self, string):
|
|
||||||
return any(final_state in self.final_states for final_state in self.get_final_states(string))
|
|
||||||
|
|
||||||
|
|
||||||
nfa = NFA()
|
|
||||||
|
|
||||||
table = open(sys.argv[1])
|
|
||||||
for line in table:
|
|
||||||
line = line.rstrip('\n')
|
|
||||||
if len(line.split('\t')) == 3:
|
|
||||||
a, b, c = line.split('\t')
|
|
||||||
c = c.replace("'","")
|
|
||||||
c = list(c)
|
|
||||||
for x in c:
|
|
||||||
nfa.add_transition(a, b, x)
|
|
||||||
nfa.alphabet.add(x)
|
|
||||||
elif len(line.split('\t')) == 1:
|
|
||||||
nfa.add_final_state(line)
|
|
||||||
else:
|
|
||||||
assert False
|
|
||||||
|
|
||||||
for line in sys.stdin:
|
for line in sys.stdin:
|
||||||
print("YES" if nfa.accepts(line.strip()) else "NO")
|
parts = line.strip().split('\t')
|
||||||
|
if parts == ['']: break
|
||||||
|
[fsa.add_transition(parts[0], parts[1], parts[2]) for parts in [parts]] if len(parts) == 3 else fsa.add_final_state(parts[0])
|
||||||
|
for path in fsa.get_all_paths():
|
||||||
|
print(path)
|
@ -1,9 +1,10 @@
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
for line in sys.stdin:
|
def emo_case(s):
|
||||||
numbers = re.match(r'^[a-ząćęłńóśźżA-ZĄĆĘŁŃÓŚŹŻ]([a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŹŻ]*[a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŹŻ])?$', line.replace("\n", ""), flags=re.IGNORECASE)
|
p = re.compile(r'^([A-Z\u0104-\u017B]?([a-z\u0105-\u017C][A-Z\u0104-\u017B])+[a-z\u0105-\u017C]?)')
|
||||||
if numbers:
|
return "yes" if p.match(s) else "no"
|
||||||
print('yes')
|
|
||||||
else:
|
|
||||||
print("no")
|
for _, l in enumerate(sys.stdin, start=1):
|
||||||
|
print(emo_case(l))
|
||||||
|
@ -71,7 +71,7 @@ def does_task_match_index(dir, index):
|
|||||||
|
|
||||||
|
|
||||||
def get_tasks(index):
|
def get_tasks(index):
|
||||||
all_tasks = Path('../djfz-2023-464933').glob('Task*')
|
all_tasks = Path('../djfz-2023-s464933').glob('TaskC06')
|
||||||
return [task for task in all_tasks if does_task_match_index(task, index)]
|
return [task for task in all_tasks if does_task_match_index(task, index)]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user