Link: http://www.spoj.com/problems/ORDERSET/
this link might be of some help !!
http://codeforces.com/blog/entry/6700