Twin Friends
Time limit2sMemory limit1024 MB
Count pairs of a permutation A' of A and a length-N string B' from B, where each B' letter equals or follows the matching A' letter, modulo 998244353.
- Level
Medium6 of 10
- Topics
- Combinatorics, Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
You meet two new friends who are twins. The elder twin's name is , which has characters. The younger twin's name is , which has characters. .
You want to give each of them a nickname. For the elder twin, you choose any permutation of as the nickname . For the younger twin, you remove exactly characters from some permutation of to form the nickname .
The nicknames must satisfy this condition: for each with , is either or the letter that follows in the alphabet (if that letter exists).
Count the number of different pairs that satisfy the condition. Two pairs are different if at least one of their nicknames differs. Print the count modulo .
Input
The first line contains two integers and ().
The second line contains the string of length .
The third line contains the string of length .
All strings consist only of uppercase letters.
Output
Print one integer, the number of valid pairs modulo .