Use Trie to count distinct substrings for a given string.