Albocede DNA (Large)
Time limit5sMemory limit512 MB
Count subsequences of S that split into one or more blocks of the form a^i b^j c^i d^j with i and j at least 1, modulo 1000000007.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
The DNA of the Albocede species is made of four nucleotides, written a, b, c, and d. Two Albocedes may carry different nucleotide orders, but every Albocede DNA sequence obeys all of these rules.
- It contains at least one
a, at least oneb, at least onec, and at least oned. - Every
acomes before everyb, everybcomes before everyc, and everyccomes before everyd. - The number of
as equals the number ofcs. - The number of
bs equals the number ofds.
For example, abcd and aabbbccddd are valid Albocede DNA sequences, while acbd, abc, and abbccd are not.
The Albocede-n is a species that evolved from the Albocede. The DNA sequence of an Albocede-n is one or more valid Albocede DNA sequences written one after another. For example, abcd and aaabcccdaabbbccdddabcd are valid Albocede-n DNA sequences. A valid Albocede-n DNA sequence is not always a valid Albocede DNA sequence.
An expedition brought back a sequence made only of a, b, c, and d. Count the subsequences of that are valid Albocede-n DNA sequences. A subsequence is a string obtained by deleting zero or more characters and keeping the order of the rest. Two choices of positions count separately even when they produce the same string. The count can be very large, so report it modulo .
Input
The first line contains the number of test cases . Each of the next lines contains one string made only of the characters a, b, c, and d.
Limits
Output
For each test case, print one line of the form Case #x: y, where is the test case number starting from 1 and is the answer for that test case.