Submit solution

Points:
15 (partial)

Time limit:
1.0s

Memory limit:
256M

Problem type

Allowed languages

Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, ~~CommonLisp~~, D, Dart, F#, Forth, Fortran, Go, ~~Groovy~~, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, ~~Nim~~, ~~ObjC~~, OCaml, ~~Octave~~, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

##### 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 .

#### Grading

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
```

## Comments

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

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 are N?

1 <= N <= 5

N is a real number.

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

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

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

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

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