spoj SUBSTR: counting number of distinct substrings of a string