Albocede DNA (Small)
Time limit5sMemory limit512 MB
Count subsequences of S that split into blocks of the form a^i b^j c^i d^j with i and j at least 1, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, Combinatorics
- Solved
- No attempts yet
Problem
The DNA of the alien species Albocede is made of four nucleotides: a, b, c, and d. Sequences differ between individuals, but every Albocede DNA sequence obeys all of these rules:
- Each of
a,b,c, anddappears at least once. - 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. acbd, abc, and abbccd are not.
The Albocede-n is a species that evolved from the Albocede. An Albocede-n DNA sequence is one or more valid Albocede DNA sequences concatenated end to end. For example, abcd and aaabcccdaabbbccdddabcd are valid Albocede-n DNA sequences. A valid Albocede-n DNA sequence is not necessarily 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 formed by choosing positions of and keeping their order, and two different sets of positions count as two different subsequences even when they spell the same string. The answer can be very large, so report it modulo .
Input
The first line contains the number of test cases . Each of the next lines contains a 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.