=solution in c++11, not fully tested