SRM 683, D2, 250-Pointer (EqualSubstrings2)

James S. Plank

Wed Jan 27 16:58:02 EST 2021

In case Topcoder's servers are down

Here is a summary of the problem:

The examples

#  S                           Answer
0 "aa"                         1 - the only matching substrings are "a" and "a"
1 "abcd"                       0
2 "aba"                        1 - again, "a" and "a" are the only matching substrings.
3 "abab"                       3 - "a" & "a", "b" & "b", "ab" & "ab".
4 "aaaab"                      7 - 6 different pairs of "a", one pair of "aa".
5 "onetwothreeonetwothree"     86

Testing yourself

Like the Cryptography Problem, I have a shell script tests.sh, that you can use to test your program. When you run tests.sh, your answer should be identical to answers.txt

Hints

Let's evaluate the big-O of a brain-dead answer: That's looking like a whopping O(n4), but with n capped at 50, it's fine. Even with n at 400, it finishes within the Topcoder limits when compiler optimization is used.

You can make this a little faster if, instead of generating substrings and comparing them, you simply use the C library routine strncmp(). Now, you don't need to make copies of any substrings -- it compares them all in place.

A final way of making it faster is call s.find(s1, k), where s1 is the first substring, and k is the index for the second substring. If it returns string::npos, then you don't have to look any more. Otherwise, you increment the total, and set k to be one after the return value.

I've programmed all of these, and their timings on tests.sh are as follows:

This would be a great application of the Rabin-Karp algorithm. Someday, I'll hack it up here and see what the speedup is. Until then, it would be good practice for you!