Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link ×

abcdabcd987's blog

By abcdabcd987, 7 years ago, In English,

Hello everybody. I took part in Codeforces Round #166 (Div. 2) last night. I'm a newbie, so I only solved Problem ABD.

Last night, I thought that enumerating substrings and using set<string> in C++ could solve this problem in O(n^2logn), and seems code length would be quite short. Then I coded it (only 32 lines) and submitted it. Unfortunately, I forgot to calculate the memory (needs 1G memory), and MLE: 3097364

Then I thought I should change map to hash, but actually, I don't know how to hash it. However, fortunately, I thought of a tiny&powerful data structure: Trie!

Using Trie to solve this problem is quite comfortable.

  1. Create a root node.
  2. Enumerate the start point of the substring in the string. Then enumerate the end of the substring. And at the same time, travel the Trie. If current node is valid and not marked in Trie, ++ans!

Quite easy to code (also 32 lines), and the memory cost is quite low, runs fast, too: 3098298 3105955

Read more »

  • Vote: I like it
  • 0
  • Vote: I do not like it