Logo
(追記) (追記ここまで)

8090번 - Genotypes 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB1000.000%

문제

Genotype is a finite sequence of genes. It can be described by words built of capital letters AZ of the English alphabet. Different letters denote different kinds of genes. A gene can bud and in effect transform into a pair of new genes. Such transformations are controlled by a finite set of rules. Every rule of budding can be described by three capital letters A1A2A3. Such a description means that gene A1 can transform into the pair of genes A2A3.

We are using S to denote the special kind of genes called supergenes. The breeding of a genotype begins from a sequence of supergenes and it consists in controlled budding of chosen genes according to the given rules.

Write a program that:

  • reads from the standard input a finite set of budding rules and a sequence of words describing genotypes which we want to breed,
  • for every given genotype checks if it can be bred from a certain finite sequence of supergenes according to the given budding rules and if so finds the minimal length of such a sequence,
  • writes the result to the standard output.

입력

In the first line of the standard output there is one integer n, 1 ≤ n ≤ 10,000. In each of the following n lines there is one budding rule. Such a rule is described by a word consisting of three capital letters AZ. We allow budding rules in which the second or the third character denotes a supergene.

In the next line there is written one integer k, 1 ≤ k ≤ 10,000. In each of the following k lines there is one genotype. Such a genotype is described by a non-empty word consisting of at most 100 letters AZ.

출력

In the i-th of k lines of the standard output there should be written:

  • one positive integer — the minimal length of a sequence of supergenes needed to breed the i-th of the given genotypes, or
  • the word NIE (which means “no” in Polish), if such a genotype cannot be bred.

제한

예제 입력 1

6
SAB
SBC
SAA
ACA
BCC
CBC
3
ABBCAAABCA
CCC
BA

예제 출력 1

3
1
NIE

힌트

출처

Olympiad > Polish Olympiad in Informatics > POI 1996/1997 > Stage 2 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /