Учитывая, что 8 букв можно переставить примерно 40 тысячами способов, можно просто запустить поиск в ширину, сохранить для всех перестановок то, из какой строчки они получились, и потом восстановить ответ для строчки abcdefgh.Код на python 3:from queue import Queueto_process = Queue()to_process.put(("edghcbfa", None))prec = {}while not to_process.empty(): s, prev = to_process.get() if s in prec: continue for i in range(7): for j in range(i + 1, 8): if i == 0: next_s = s[j::-1] + s[j+1:] else: next_s = s[:i] + s[j:i-1:-1] + s[j+1:] if next_s not in prec: to_process.put((next_s, s)) prec[s] = prevcurrent = "abcdefgh"print(current)while prec[current] is not None: current = prec[current] print(current)Вывод программы:abcdefghedcbafghedcbhgfaedbchgfaedghcbfa