Олимпиадный тренинг

Задача 31899. Iterating over lines #3


Задача

Темы: Рекурсия
In the alphabet of the language of the tribe «tumba-yumba» four letters: "K", "L", "M" and "N". It is necessary to display on the screen all possible words consisting of K letters, in which there are at least two identical letters standing side by side. Count the number of such words. The program should not build other words that do not match the condition. 
 
Input Output
3 KKK
KKL
KKM
KKN
KLL
KMM
KNN
LKK
LLK
LLL
LLM
LN
LMM
LNN
MKK
MLL
MMK
MML
MMM
MMN
MNN
NKK
NLL
NMM
NNK
NNL
NNM
NNN
28


(c) K.Yu. Polyakov