#P8189. [USACO22FEB] Redistributing Gifts G
[USACO22FEB] Redistributing Gifts G
题目描述
Farmer John has gifts labeled for his cows, also labeled . Each cow has a wishlist, which is a permutation of all gifts such that the cow prefers gifts that appear earlier in the list over gifts that appear later in the list.
FJ was lazy and just assigned gift to cow for all . Now, the cows have gathered amongst themselves and decided to reassign the gifts such that after reassignment, every cow ends up with the same gift as she did originally, or a gift that she prefers over the one she was originally assigned.
There is also an additional constraint: a gift may only be reassigned to a cow if it was originally assigned to a cow of the same type (each cow is either a Holstein or a Guernsey). Given length- breed strings, for each one count the number of reassignments that are consistent with it.
输入格式
The first line contains . The next lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of .
The next line contains .
The final lines each contain a breed string, each characters long and consisting only of the characters G and H. No breed string occurs more than once.
输出格式
For each breed string, print the number of reassignments that are consistent with it on a new line.
4
1 2 3 4
1 3 2 4
1 2 3 4
1 2 3 4
5
HHHH
HHGG
GHGH
HGGG
GHHG
2
1
1
2
2
提示
【样例解释】
In this example, for the first breed string, there are two possible reassignments:
- The original assignment: cow receives gift , cow receives gift , cow receives gift , and cow receives gift .
- Cow receives gift , cow receives gift , cow receives gift , and cow receives gift .
【数据范围】
- For , test case T satisfies .
- Test cases 14-18 satisfy .