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