## COCI '16 Contest 1 #3 Cezar

View as PDF

Points: 10 (partial)
Time limit: 0.6s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Mirko has an array of different words that he wants to encrypt using a substitution cypher.

We encrypt the text using a substitution cypher by first choosing a key – a permutation of the English alphabet. Then we replace all occurrences of letter a with the first letter of the key, all occurrences of letter b with the second letter of the key, and so on until letter z.

Besides the words, Mirko has an array consisting of numbers from to given in a certain order (in other words, array is a permutation of numbers from to ). Mirko wants to pick a key such that the array of words after encrypting and lexicographic sorting corresponds to array . More precisely, he wants the word initially located at to be at location after encryption and sorting.

Let's recall that the lexicographic word order is the order in which the words appear in the dictionary. If we are comparing two words, going from left to right, we search for the first position in both words where the letters differ and, based on that, we determine which word is lexicographically smaller. If word is the beginning of the word , then word is lexicographically smaller than word .

Mirko is currently not in the mood for encrypting, so he kindly asks you to do it for him.

#### Input Specification

The first line of input contains the integer .
Each of the following lines contains a single word that consists of at most lowercase letters of the English alphabet. The words will be mutually distinct.
The last line contains integers – the elements of array .

In test cases worth 30 points total, the words will consist of only the first letters of the English alphabet.

#### Output Specification

In the case when a solution doesn't exist, output NE.
Otherwise, output DA in the first line, and in the second line output a word consisting of different letters of the English alphabet – the key for the substitution cipher.
If multiple solutions exist, output any.

#### Sample Input 1

2
ab
bc
2 1

#### Sample Output 1

DA
bacdefghijklmnopqrstuvwxyz

#### Explanation for Sample Output 1

After encrypting, the words become ba, ac, after lexicographic sorting, the array becomes ac, ba, which means the first word ended up in the second spot, and the second word in the first spot.

#### Sample Input 2

3
abc
bcd
add
1 2 3

#### Sample Output 2

NE

#### Sample Input 3

3
bbb
ccc
ddd
2 3 1

#### Sample Output 3

DA
adbcefghijklmnopqrstuvwxyz

#### Explanation for Sample Output 3

After encrypting, the words become ddd, bbb, ccc, after lexicographic sorting, the array becomes bbb, ccc, ddd, which means the first word ended up in the third spot, the third word in the second spot, and the second word in the first spot.

## Comments

• commented on Dec. 18, 2016, 3:54 p.m. edited

Checker is up, but it may be incorrect. Please inform me of any errors.

• commented on Oct. 19, 2017, 7:40 p.m. edit 2

(The checker seems correct, it was my mistake , edited)