Source: http://www.cril.univ-artois.fr/~koriche/Algorithmique-2012-TD1-Correction.pdf