Polish version    English version  
  About Olympic -> Problems -> IV OI 1996/1997


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
IV Olimpiada Informatyczna 1996/1997

Task: GEN
Author: Przemysława Kanarek
GENOTYPES

II stage contest  
Source fileGEN.??? (e.g. PAS,C, CPP)
Executable fileGEN.EXE
Input fileGEN.IN
Output fileGEN.OUT

Genotype is a finite sequence of genes. It can be described by words built of capital letters A-Z 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.

Task

Write a program that:
  • reads from the text file GEN.IN 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 text file GEN.OUT.

Input

In the first line of the text file GEN.IN there is one integer n, 1 <= n <= 10000. 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 A - Z.

In the next line there is written one integer k, 1 <= k <= 10000. 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 A - Z.

Output

In the i-th of k lines of the text file GEN.OUT 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 can not be bred.

Example

For the file GEN.IN:
6
SAB
SBC
SAA
ACA
BCC
CBC
3
ABBCAAABCA
CCC
BA
the correct solution is file GEN.OUT:
3
1
NIE





Print friendly version