bang = { ("nhap", "gui"): "cho", ("cho", "duyet"): "da_duyet", ("cho", "tra_lai"): "nhap", ("da_duyet", "mo_lai"): "cho", } def kiem_tra(chuoi): hien_tai = "nhap" trang_thai = {hien_tai} canh = set() for su_kien in chuoi: khoa = hien_tai, su_kien if khoa not in bang: raise ValueError(khoa) canh.add(khoa) hien_tai = bang[khoa] trang_thai.add(hien_tai) return len(trang_thai), len(canh) assert kiem_tra(["gui", "duyet"]) == (3, 2) assert kiem_tra(["gui", "tra_lai", "gui", "duyet", "mo_lai"]) == (3, 4) try: kiem_tra(["duyet"]) except ValueError: print("Bao phủ đủ 4 chuyển tiếp, phát hiện thao tác không hợp lệ.") else: raise AssertionError("Không thể duyệt từ Nháp")