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 4002126
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:59:17+00:00 2026-05-20T07:59:17+00:00

Here is another spoj problem that asks how to find the number of distinct

  • 0

Here is another spoj problem that asks how to find the number of distinct subsequences of a string ?

For example,

Input
AAA
ABCDEFG
CODECRAFT

Output
4
128
496

How can I solve this problem ?

  • 1 1 Answer
  • 2 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-05-20T07:59:17+00:00Added an answer on May 20, 2026 at 7:59 am

    It’s a classic dynamic programming problem.

    Let:

    dp[i] = number of distinct subsequences ending with a[i]
    sum[i] = dp[1] + dp[2] + ... + dp[i]. So sum[n] will be your answer.
    last[i] = last position of character i in the given string.
    

    A null string has one subsequence, so dp[0] = 1.

    read a
    n = strlen(a)
    for i = 1 to n
      dp[i] = sum[i - 1] - sum[last[a[i]] - 1]
      sum[i] = sum[i - 1] + dp[i]
      last[a[i]] = i
    
    return sum[n]
    

    Explanation

    dp[i] = sum[i - 1] - sum[last[a[i]] - 1]
    

    Initially, we assume we can append a[i] to all subsequences ending on previous characters, but this might violate the condition that the counted subsequences need to be distinct. Remember that last[a[i]] gives us the last position a[i] appeared on until now. The only subsequences we overcount are those that the previous a[i] was appended to, so we subtract those.

    sum[i] = sum[i - 1] + dp[i]
    last[a[i]] = i
    

    Update these values as per their definition.

    If your indexing starts from 0, use a[i - 1] wherever I used a[i]. Also remember to wrap your computations in a mod function if you’re going to submit code. This should be implemented like this:

    mod(x) = (x % m + m) % m
    

    In order to correctly handle negative values in some languages (such as C/C++).

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

Sidebar

Related Questions

I must be retarded with searching, because here's another seemingly common problem that I
Here's another clichéd problem, but I couldn't find an exact match to this. I
Hooooookay so here's another I just have no idea what's going on problem: The
Now here is another problem.. my code: <script type=text/javascript> function validate_form () { if
i am here with another problem in my code since i am new to
Here's another problem with qt: I extend a QAbstractTableModel, but I get a compiling
Here another bash command that needs some explanation. Can someone explain what are does
Here is another problem I have had to deal with in the last couple
so here is another basic problem i'm having...my application crashes as i try to
Here's another one for you to help me solve: I have an ASP.NET website

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.