DSUBSEQ - Distinct Subsequences

no tags 

Given a string, count the number of distinct subsequences of it ( including empty subsequence ). For the uninformed, A subsequence of a string is a new string which is formed from the original string by deleting some of the characters without disturbing the relative positions of the remaining characters.
For example, "AGH" is a subsequence of "ABCDEFGH" while "AHG" is not.


First line of input contains an integer T which is equal to the number of test cases. You are required to process all test cases. Each of next T lines contains a string s.


Output consists of T lines. Ith line in the output corresponds to the number of distinct subsequences of ith input string. Since, this number could be very large, you need to output ans%1000000007 where ans is the number of distinct subsequences.




Constraints and Limits

T ≤ 100, length(S) ≤ 100000
All input strings shall contain only uppercase letters.

Added by:Ajay Somani
Time limit:0.245s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: C++ 5
Resource:CodeCraft 08, Problem Setter: Jin Bin

hide comments
kartikeya : 2015-07-13 14:56:37

stored strlen(s) in a different variable....and got ac after 10 tles!!!!!!
wasted my whole day!!!

Last edit: 2015-07-13 15:09:45
Shashank Tiwari: 2015-07-01 11:40:23

If getting wrong answers , make sure of following points :

1. |S|>0 in all test cases , so relax.
2. mod 1000000007 can give 0. And if you subtract something from it then remainder can get negative.So ,
use this : (1000000007 +any_remainder)%1000000007 will always give answer between 0 and 1000000006.
Note: any_remainder can be negative.

3. If answer stored as integer check if addition gives integer overflow.
Make sure mod 1000000007 can give remainder upto 1000000006. Best is to use long long and not unsigned long long (for c++ users )

4. wrong answer on test case 0 shows wrong logic used :
check them :


output :
Hope that helps :)

Last edit: 2015-07-01 11:42:14
xxbloodysantaxx: 2015-07-01 08:52:38

Dynamic programming :) is beautiful!

ISHANI: 2015-02-25 15:07:02

not so simple as i thought..

Swapnil Borse: 2014-12-29 14:24:06

was fun solving this problem :)

rajat arora: 2014-12-17 15:56:17

great question to test knowledge abt modulus

numerix: 2014-11-27 07:39:28

New time limit (set during switch to Cube cluster) is too strict for slower languages. My former Python AC solution (using psyco) cannot pass any more.
The problem is not a hard one, there are 1000+ AC users, so it shouldn't be a problem to increase the TL to allow slower languages to pass.

tranquil: 2014-05-23 17:01:45

getting WA#1 :(
donno what is wrong....mod looks fine to me

P_Quantum: 2014-05-23 10:36:06

Finally AC...don't use long long.

Nadbor Drozd: 2014-01-22 11:31:06

linear time and still time limit exceeded. Tried switching from python to java and c++, but couldn't get them even past compilation on with these ancient versions of the language. I give up