CCC '03 S4 - Substrings

View as PDF

Submit solution

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 S have?

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

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

Input Specification

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

Output Specification

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


50% of test cases will have l (the length of the string) where l \le 1000. For all cases, l \le 5000.

Sample Input


Output for Sample Input



  • 2
    Kirito  commented on Jan. 13, 2022, 1:04 p.m.

    A new test case has been added and all submissions have been rejudged.

  • 1
    myl  commented on March 6, 2021, 6: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?

    • 3
      Tommy_Shan  commented on Aug. 3, 2021, 12:27 a.m. edit 2

      A string of from 1 to 5000 alphanumeric characters.

    • 1
      Kirito  commented on March 7, 2021, 1:15 a.m.

      Reread the input specification.

  • -2
    daviecar  commented on Oct. 27, 2019, 9:42 p.m. edited

    I don't think it was supposed to happen, but I just completely brute forced it... ( Now may I have some tips on how to not be stupid and do it properly?

    • 6
      kingW3  commented on Oct. 27, 2019, 10: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.

  • 13
    Beautiful_Times  commented on Nov. 30, 2017, 3:04 a.m. edited

    What are the constraints on N?

    • 10
      Arman_Lamei  commented on July 16, 2019, 2:36 p.m. edited

      1 \le N \le 5

    • 45
      InfernoApeZ  commented on Nov. 30, 2017, 4:30 a.m. edited

      N is a real number.

      • 46
        ArtyKing12  commented on March 29, 2020, 3:24 p.m. edit 2

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

  • 2
    raggarwal  commented on Nov. 23, 2014, 5:00 a.m. edited

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

    • 17
      FatalEagle  commented on Nov. 23, 2014, 2:49 p.m. edited

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