## CCC '03 S4 - Substrings

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
##### Canadian Computing Competition: 2003 Stage 1, Senior #4

How many distinct substrings does a given string have?

For example, if abc, has distinct substrings: , a, b, c, ab, bc, abc. Note that the empty string and itself are considered substrings of .

On the other hand, if aaa. has only distinct substrings: , a, aa, aaa.

#### Input Specification

The first line of the input file contains , the number of test cases. For each test case, a line follows giving , a string of from to alphanumeric characters.

#### Output Specification

Your output consists of one line per case, giving the number of distinct substrings of .

50% of test cases will have (the length of the string) where . For all cases, .

#### Sample Input

2
abc
aaa

#### Output for Sample Input

7
4

• commented on March 6, 2021, 1:51 p.m.

Are the test strings only alphabetical or do they contain other characters? Is it safe to assume only ASCII characters are used?

• commented on Aug. 2, 2021, 8:27 p.m.

A string of from 1 to 5000 alphanumeric characters.

• commented on March 6, 2021, 8:15 p.m.

• commented on Oct. 27, 2019, 5:42 p.m. edited

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Oct. 27, 2019, 6:25 p.m.

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.

• commented on Nov. 29, 2017, 10:04 p.m.

What are the constraints are N?

• commented on July 16, 2019, 10:36 a.m.

1 <= N <= 5

• commented on Nov. 29, 2017, 11:30 p.m.

N is a real number.

• commented on March 29, 2020, 11:24 a.m. edit 2

When you get WA on a case and notice that n is -5i+0.85

• commented on Aug. 14, 2020, 3:18 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Oct. 23, 2016, 8:12 a.m. edited

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Nov. 23, 2014, 12:00 a.m.

Any tips to optimize the code? test case 3/6/7 are >15 seconds for me.

• commented on Nov. 23, 2014, 9:49 a.m.

Your algorithm is brute force with a worst-case complexity of O(N^4). There are faster algorithms to solve this (like Suffix Tree).