WOWSUBSTR, Want solution and how to approah this problem ?

this is spoj problem please give a right a best approach for this problem and solution too.
Problem link WOWSUBSTR