#570
Unrated
Redistributing Gifts
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Farmer John has NN gifts labeled 1N1\ldots N for his NN cows, also labeled 1N1\ldots N (1N181\le N\le 18). Each cow has a wishlist, which is a permutation of all NN 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 ii to cow ii for all ii. 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 QQ (1Qmin(105,2N)1\le Q\le \min(10^5,2^N)) length-NN breed strings, for each one count the number of reassignments that are consistent with it.

입력

The first line contains NN.

The next NN lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of 1N1\dots N.

The next line contains QQ.

The final QQ lines each contain a breed string, each NN 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.

예제 입력 1

4
1 2 3 4
1 3 2 4
1 2 3 4
1 2 3 4
5
HHHH
HHGG
GHGH
HGGG
GHHG

예제 출력 1

2
1
1
2
2

점수

For T=2,,13T = 2, \ldots, 13, test case TT satisfies N=T+4N = T + 4.Test cases 14-18 satisfy N=18N = 18.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.