Almost Conjugates
Time limit1sMemory limit128 MB
Decide whether two length-n words are almost conjugates and, if so, list every rotation of the first that differs from the second in exactly one position.
- Level
Hard8 of 10
- Topics
- String, String matching, Math, Implementation
- Solved
- No attempts yet
Problem
Strictly speaking, the word almost means the same as the word no. Yet the two are not exact synonyms: almost is in fact closer in meaning to yes than to no.
Two words are conjugates if the first can be turned into the second by repeatedly taking the letter at the front of the first word and moving it to the back. For example, ababa and abaab are conjugates, while ababa and baaab are not.
Two words are almost conjugates if both of the following hold:
- they are not conjugates, and
- repeatedly moving the front letter of the first word to the back can produce a word that differs from the second word in exactly one position.
For example, ababa and aaaab are almost conjugates, while ababa and bbbbb are not.
Write a program that reads two words from standard input, decides whether they are almost conjugates, and if so lists the shifts that prove it.
Input
The first line contains an integer (), the length of each word. The second line contains the first word and the third line contains the second word. Each word is a string of lowercase English letters.
Output
Print TAK (Polish for yes) on the first line if the two words are almost conjugates, or NIE (Polish for no) otherwise.
If the first line is TAK, print on the second line an increasing sequence of non-negative integers taken from , separated by single spaces. This sequence must list every number of front-to-back moves applied to the first word after which the resulting word differs from the second word in exactly one position.