Since this seems to be a homework, so giving complete solution would not be a nice idea.
However, I would give you this hint:
std::bitset
std::string
and related functions from<algorithm>
header.
These two together should solve your problem if you learn to use them cleverly.