If I understand the problem right, the encryption algorithm maps each plain text letter to a single encrypted letter. The "key" part of the input gives you clues to the mapping. If the key just told you the encrypted word for each plain word, it would be easy, but the encrypted words in the key have had their letters shuffled around.
To figure out the actual encryption mapping, you could do this:
do {
for each pair in the key
record how many times each uncracked letter occurs in the plain word and the secret word
if any uncracked letter occurs a unique number of times, then the plain letter must map to the secret one. Record that pair as a "cracked" letter.
}
} while (you cracked letters in this iteration)
Once you have the mapping, you can apply it to the plain word in the first part of the input.
Here's example 1 above:
load
3
app lol
old tip
odd itt |
Applying the algorithm:
PASS 1:
app lol: In the plain word, a occurs once and p occurs twice. In the secret word, 'o' occurs once and 'l' occurs twice. Since only 1 letter occurs once in the plain word (a) and the secret word (o), that means a maps to o. Similarly, p maps to l
old tip: Each letter occurs once so we don't know which maps to which.
odd itt: using the same logic as app->lol, we know that o maps to i and d maps to t.
Pass 2:
app lol: We know the mapping of all letters, so we can skip this
old tip: In pass 1, we learned the mappings of o and d, Since 'l' is the only unknown letter in the plain word and p is the only unknown letter in the secret word, l maps to p.
Pass 3:
app lol: all letters mapped.
old top: all letters mapped.
odd tip: all letters mapped.
No change in pass 3 so the loop exits.