Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:32:22+00:00 2026-06-13T05:32:22+00:00

I am working on the problem http://www.spoj.pl/problems/DISUBSTR/ Given a string, we need to find

  • 0

I am working on the problem http://www.spoj.pl/problems/DISUBSTR/
Given a string, we need to find the total number of its distinct substrings.

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each test case output one number saying the number of distinct substrings.

Example

Sample Input:
2
CCCCC
ABABA

Sample Output:
5
9

    Explanation for the testcase with string ABABA: 
    len=1 : A,B
    len=2 : AB,BA
    len=3 : ABA,BAB
    len=4 : ABAB,BABA
    len=5 : ABABA
    Thus, total number of distinct substrings is 9.

I have solved the problem using blind implementation of suffix array . However,I want to solve it using Dynamic Programming.However I am not able to think of any approach for the purpose.Also the time complexity needs to be 0(n*n) or faster.Please can anyone guide me in the right direction.Any hints/suggestions/pseudo code will be highly appreciated.I have been thinking on it for a long time but can’t think of any DP approach to solve the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:32:23+00:00Added an answer on June 13, 2026 at 5:32 am

    I don’t think you can solve this using dynamic programming, because there is no optimal substructure. Knowing the answer for a part of the string does not tell you anything about that part + another character for example.

    The problem can be solved by inserting all suffixes of the string in a trie then counting its number of nodes. This is O(n^2) and will most likely get AC with such short strings. More efficient methods involve using a suffix array (O(n log n)) and building a suffix tree in O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SPOILERS: I'm working on http://www.spoj.pl/problems/KNAPSACK/ so don't peek if you don't want a possible
When working with text in Flash, I often encounter the following problem: http://www.aino.se/media/i/subpixel.png This
i have a problem with the revert speed. Here is a working example http://www.jsfiddle.net/V9Euk/94/
I am working on http://www.qxl.dk/ and I am experiencing a problem. If you compare
I have this test page I am working from: http://www.problemio.com/test.php The problem happens when
I've just created default datepicker from the link http://www.androidpeople.com/android-datepicker-dialog-example .Its working fine.But my problem
Problem: to get the command working here. My domain is http://cs.edu.com/user/share_dir , but I
I'm working on http://lobounce.com.s138810.gridserver.com/ and have been having some problems with the slider which
http://www.stuartflake.com is the site that I am working on. The footer element contains the
I've extracted the following from a project we are working on: http://www.bridgedev1.com/videotest.html This plays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.