Implementation in C++ of the PHP algorithm, this one got accepted! So the PHP algorithm works too.