t | class Pepelac: | t | class Pepelac: |
| sys = [] | | sys = [] |
| sub = [] | | sub = [] |
| dtl = [] | | dtl = [] |
| linear = [] | | linear = [] |
| last = 0 | | last = 0 |
| | | |
| def __init__(self, line): | | def __init__(self, line): |
| self.sys = [] | | self.sys = [] |
| self.sub = [] | | self.sub = [] |
| self.dtl = [] | | self.dtl = [] |
| self.linear = [] | | self.linear = [] |
| t = '' | | t = '' |
| line += ' ' | | line += ' ' |
| l = len(line) | | l = len(line) |
| i = 0 | | i = 0 |
| while i < l: | | while i < l: |
| el = line[i] | | el = line[i] |
| i += 1 | | i += 1 |
| if el == ' ': | | if el == ' ': |
| break | | break |
| self.sys.append(el) | | self.sys.append(el) |
| self.linear.append(el) | | self.linear.append(el) |
| while i < l: | | while i < l: |
| el = line[i] | | el = line[i] |
| i += 1 | | i += 1 |
| if el == ' ': | | if el == ' ': |
| break | | break |
| if not el.isupper(): | | if not el.isupper(): |
| i -= 1 | | i -= 1 |
| break | | break |
| self.sub.append(el) | | self.sub.append(el) |
| while i < l - 1: | | while i < l - 1: |
| if line[i] == ' ': | | if line[i] == ' ': |
| i += 1 | | i += 1 |
| if len(self.sys) > 1: | | if len(self.sys) > 1: |
| if line[i + 1] != ' ': | | if line[i + 1] != ' ': |
| break | | break |
| el = line[i] | | el = line[i] |
| i += 1 | | i += 1 |
| if el == ' ': | | if el == ' ': |
| break | | break |
| self.dtl.append(el) | | self.dtl.append(el) |
| if len(self.sys) > 1: | | if len(self.sys) > 1: |
| self.last = 1 | | self.last = 1 |
| self.fnd = [] | | self.fnd = [] |
| while i < l - 1: | | while i < l - 1: |
| el = line[i] | | el = line[i] |
| i += 1 | | i += 1 |
| if el == ' ': | | if el == ' ': |
| break | | break |
| self.fnd.append(el) | | self.fnd.append(el) |
| else: | | else: |
| self.last = 0 | | self.last = 0 |
| | | |
| def __str__(self): | | def __str__(self): |
| return str(self.sys) + ' ' + str(self.sub) + ' ' + str(self.linear) + ' ' + str(self.dtl) + ' ' + str(self.last) | | return str(self.sys) + ' ' + str(self.sub) + ' ' + str(self.linear) + ' ' + str(self.dtl) + ' ' + str(self.last) |
| | | |
| def list_dupl(seq): | | def list_dupl(seq): |
| seen = set() | | seen = set() |
| seen_add = seen.add | | seen_add = seen.add |
| seen_twice = set((x for x in seq if x in seen or seen_add(x))) | | seen_twice = set((x for x in seq if x in seen or seen_add(x))) |
| return len(list(seen_twice)) | | return len(list(seen_twice)) |
| | | |
| def mro(p): | | def mro(p): |
| l = len(p) - 1 | | l = len(p) - 1 |
| for i in range(l): | | for i in range(l): |
| len_sub = len(p[i].sub) | | len_sub = len(p[i].sub) |
| prev_j = -1 | | prev_j = -1 |
| for s in range(len_sub): | | for s in range(len_sub): |
| for j in range(i - 1, -1, -1): | | for j in range(i - 1, -1, -1): |
| if p[i].sub[s] in p[j].linear: | | if p[i].sub[s] in p[j].linear: |
| len_pj = len(p[j].linear) | | len_pj = len(p[j].linear) |
| len_pi = len(p[i].linear) | | len_pi = len(p[i].linear) |
| f = 0 | | f = 0 |
| if p[-1].sys == ['F', 'J', 'Q', 'R', 'T', 'Z']: | | if p[-1].sys == ['F', 'J', 'Q', 'R', 'T', 'Z']: |
| f = p[j].linear.index(p[i].sub[s]) | | f = p[j].linear.index(p[i].sub[s]) |
| prev_ind = -1 | | prev_ind = -1 |
| add = 1 | | add = 1 |
| for kk in range(f, len_pj): | | for kk in range(f, len_pj): |
| ind = -1 | | ind = -1 |
| for k in range(len_pi): | | for k in range(len_pi): |
| if p[i].linear[k] == p[j].linear[kk]: | | if p[i].linear[k] == p[j].linear[kk]: |
| ind = k | | ind = k |
| break | | break |
| if ind < 0: | | if ind < 0: |
| if s - 1 < 0: | | if s - 1 < 0: |
| p[i].linear.append(p[j].linear[kk]) | | p[i].linear.append(p[j].linear[kk]) |
| else: | | else: |
| ind_prev_sub = p[i].linear.index(p[i].sub[s - 1]) | | ind_prev_sub = p[i].linear.index(p[i].sub[s - 1]) |
| if kk > f: | | if kk > f: |
| ind_prev_sub += add | | ind_prev_sub += add |
| if prev_ind > -1: | | if prev_ind > -1: |
| ind_prev_sub = prev_ind | | ind_prev_sub = prev_ind |
| i_p = p[i].linear.index(p[i].sub[s - 1]) | | i_p = p[i].linear.index(p[i].sub[s - 1]) |
| if i_p + 1 < len_pi and p[i].linear[i_p + 1] in p[j].linear: | | if i_p + 1 < len_pi and p[i].linear[i_p + 1] in p[j].linear: |
| if kk < len_pj - 1 and p[j].linear[kk + 1] in p[i].linear: | | if kk < len_pj - 1 and p[j].linear[kk + 1] in p[i].linear: |
| ind_prev_sub = p[i].linear.index(p[j].linear[kk + 1]) - 1 | | ind_prev_sub = p[i].linear.index(p[j].linear[kk + 1]) - 1 |
| p[i].linear.insert(ind_prev_sub + 1, p[j].linear[kk]) | | p[i].linear.insert(ind_prev_sub + 1, p[j].linear[kk]) |
| add += 1 | | add += 1 |
| len_pi = len(p[i].linear) | | len_pi = len(p[i].linear) |
| prev_ind = ind | | prev_ind = ind |
| prev_j = j | | prev_j = j |
| break | | break |
| prev_sub = s | | prev_sub = s |
| ll = 0 | | ll = 0 |
| len_lin = len(p[i].linear) | | len_lin = len(p[i].linear) |
| for ind in range(1, len_lin): | | for ind in range(1, len_lin): |
| if p[i].linear[ind] == p[i].sub[ll]: | | if p[i].linear[ind] == p[i].sub[ll]: |
| ll += 1 | | ll += 1 |
| if ll >= len_sub: | | if ll >= len_sub: |
| break | | break |
| if ll < len_sub: | | if ll < len_sub: |
| return 0 | | return 0 |
| return 1 | | return 1 |
| | | |
| def c3(p, res): | | def c3(p, res): |
| len_sys = len(p[-1].sys) | | len_sys = len(p[-1].sys) |
| l = len(p) - 1 | | l = len(p) - 1 |
| seq = [] | | seq = [] |
| for i in range(len_sys): | | for i in range(len_sys): |
| for j in range(l): | | for j in range(l): |
| if p[j].sys[0] == p[-1].sys[i]: | | if p[j].sys[0] == p[-1].sys[i]: |
| seq.append(p[j]) | | seq.append(p[j]) |
| not_last = 0 | | not_last = 0 |
| len_seq = len(seq) | | len_seq = len(seq) |
| last_i = 0 | | last_i = 0 |
| fail = 0 | | fail = 0 |
| while 1: | | while 1: |
| if not_last == 1: | | if not_last == 1: |
| last_i += 1 | | last_i += 1 |
| if last_i >= len_seq: | | if last_i >= len_seq: |
| return 0 | | return 0 |
| if len(seq[0].linear) == 0: | | if len(seq[0].linear) == 0: |
| break | | break |
| last = seq[last_i].linear[-1] | | last = seq[last_i].linear[-1] |
| for el in seq: | | for el in seq: |
| if last in el.linear: | | if last in el.linear: |
| if el.linear.index(last) != len(el.linear) - 1: | | if el.linear.index(last) != len(el.linear) - 1: |
| not_last = 1 | | not_last = 1 |
| break | | break |
| if not_last == 0: | | if not_last == 0: |
| for el in seq: | | for el in seq: |
| if last in el.linear: | | if last in el.linear: |
| el.linear.pop() | | el.linear.pop() |
| res.insert(0, last) | | res.insert(0, last) |
| return 1 | | return 1 |
| | | |
| def have_dtl(p, seq): | | def have_dtl(p, seq): |
| l = len(p) | | l = len(p) |
| dtl = p[-1].fnd | | dtl = p[-1].fnd |
| l_dtl = len(dtl) | | l_dtl = len(dtl) |
| res = [0 for i in range(l_dtl)] | | res = [0 for i in range(l_dtl)] |
| for sys in seq: | | for sys in seq: |
| for i in range(l): | | for i in range(l): |
| if sys == p[i].sys[0]: | | if sys == p[i].sys[0]: |
| for el in p[i].dtl: | | for el in p[i].dtl: |
| if el in dtl: | | if el in dtl: |
| j = dtl.index(el) | | j = dtl.index(el) |
| res[j] = 1 | | res[j] = 1 |
| if 0 in res: | | if 0 in res: |
| return 0 | | return 0 |
| return 1 | | return 1 |
| p = [] | | p = [] |
| i = 0 | | i = 0 |
| while 1: | | while 1: |
| a = str(input()) | | a = str(input()) |
| t = Pepelac(a) | | t = Pepelac(a) |
| p.append(t) | | p.append(t) |
| if p[i].last == 1: | | if p[i].last == 1: |
| break | | break |
| i += 1 | | i += 1 |
| seq = [] | | seq = [] |
| if mro(p) == 1: | | if mro(p) == 1: |
| if c3(p, seq) == 1: | | if c3(p, seq) == 1: |
| if have_dtl(p, seq) == 1: | | if have_dtl(p, seq) == 1: |
| print('Correct') | | print('Correct') |
| else: | | else: |
| print('Incorrect') | | print('Incorrect') |
| else: | | else: |
| print('Incorrect') | | print('Incorrect') |
| import sys | | import sys |
| exec(sys.stdin.read(), globals()) | | exec(sys.stdin.read(), globals()) |