Little LCS
Time limit2sMemory limit512 MB
Count the ways to fill the question marks in two good ternary strings of length 2n+1 so that their LCS is exactly n.
- Level
Hard8 of 10
- Topics
- Combinatorics, String, Dynamic programming
- Solved
- No attempts yet
Statement
A string made of the letters A, B, and C is good if every two adjacent letters are different.
A pair of good strings of length is awesome if the length of their longest common subsequence (LCS) is exactly .
You are given two strings, and , made of the letters A, B, C and question marks (?). Count the ways to replace each ? with A, B, or C so that the pair is awesome. Output the count modulo .
Input
The first line contains a single integer (), the number of test cases.
The first line of each test case contains a single integer (). The second line contains a string of length , and the third line contains a string of length . Both strings consist of A, B, C, and ?.
The sum of over all test cases does not exceed .
Output
For each test case, print the number of ways to replace the question marks so that is awesome, modulo , on its own line.
Hint
In the first test case, the pair (ABA, CBC) is awesome.
In the second test case, there are 3 ways to replace the question marks so that the pair is awesome: (ABA, CBC), (ACA, CBC), and (ABA, CAC).