Canadian Computing Competition: 2003 Stage 1, Senior #4
How many distinct substrings does a given string
For example, if abc
, ,
a
, b
, c
, ab
, bc
, abc
. Note that the empty string and
On the other hand, if aaa
. ,
a
, aa
, aaa
.
Input Specification
The first line of the input file contains
Output Specification
Your output consists of one line per case, giving the number of distinct substrings of
Grading
50% of test cases will have
Sample Input
Copy
2
abc
aaa
Output for Sample Input
Copy
7
4
Comments
Last test case is actually the same string repeated multiple times, so a sub-optimal solution can pass by using a cache. I discovered this by looking at the output, not by finding the actual test case
A new test case has been added and all submissions have been rejudged.
Are the test strings only alphabetical or do they contain other characters? Is it safe to assume only ASCII characters are used?
Reread the input specification.
I don't think it was supposed to happen, but I just completely brute forced it... (https://dmoj.ca/submission/1661542) Now may I have some tips on how to not be stupid and do it properly?
Since you solved the problem you can view all the solutions to this problem, you could go to "Best submissions" and find the most readable code and try to understand it, or like someone in the comments mentioned google Suffix Tree.
What are the constraints on
?
When you get WA on a case and notice that n is -5i+0.85
Any tips to optimize the code? test case 3/6/7 are >1 second for me.
Your algorithm is brute force with a worst-case complexity of
. There are faster algorithms to solve this (like Suffix Tree).