numbers and LETTERS P45146


Statement
 

pdf   zip

thehtml

Welcome, contestant. ¿Do you like letters? Because in the today test you must solve the famous game of the letters, of the well known game show “Numbers and Letters”. (Yes, really, it is a well known game show; if you do not know it, it is your fault. It was broadcasted in the 90s in La 2, and it is still broadcasted in some autonomic televisions; the original French game show, “Des chiffres et des lettres”, is being broadcasted since 1972).

The test of Letters consists of: given 9 letters, possibly some of them repeated, you have to form with them the longest possible word. For instance, if the letters were

G Y H D N O E U R

we could form the word younger or, better still, the word hydrogen.

Input

The input consists of a number n<1000 in a line, followed by exactly n lines, each one of them contains a word (lowercase letters) of at most 9 letters. These n words form the dictionary of valid words in the game. Then, a natural k<100, with number of cases to solve, followed by k lines, each one of them contains a case (exactly 9 uppercase letters).

Your program must solve all the cases in 1 second of time.

Output Exactly k lines, one for each case, with the longest word (with uppercase letters) of the dictionary that is possible to form using the letters of the case, or a line in white if it not possible to write any of the words. If there are more than one word of maximal length, it must print the word that appears first in the dictionary.

Public test cases
  • Input

    3
    alzar
    aljaraz
    alondra
    1
    ATZALJAIR
    

    Output

    ALJARAZ
    
  • Input

    20
    ntage
    uuvg
    neiclimas
    mbnoe
    oeqpogw
    astbcv
    gfr
    uvtufbse
    otsiguc
    bzairmpal
    czdo
    msow
    jast
    rffflvutu
    umsogiil
    ueihez
    dje
    ooun
    ioedooox
    upyabaef
    3
    IPSJENZAT
    TNQNPPEOU
    BNGUPCOOU
    

    Output

    JAST
    
    OOUN
    
  • Information
    Author
    Omer Giménez
    Language
    English
    Translator
    Carlos Molina
    Original language
    Spanish
    Other languages
    Spanish
    Official solutions
    C++
    User solutions
    C++