Algorithm


Problem Name: beecrowd | 2852

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2852

Messaging

 

By Jessica Dagostini, beecrowd BR Brazil

Timelimit: 1

João and Enzo love encrypt their messages. To do this encrypt, they use the Vigènere cipher. This technique is really similar to the Cesar cipher, however its use a specific key for each letter in a word to be encrypted. The below table shows the ciphering pattern, that consists of the repetition of the alphabet 26 times, one time per row of this table, and in each row, the letters are displaced one space left. This 26 rows correspond to the 26 Cesar ciphers.

A random word is chosen as the keyword and each letter of this word indicates a line of the table to use to cipher or decipher the encrypted message. For example:

  • The text to be ciphered is "computer science";
  • The key-word is "obi";
  • Now, we should repeat the keyword many times as necessary to compose all the text to be ciphered:
    • computer science
    • obiobiob obiobio
  • To do the encryption of the first letter, we should find the line of the letter "o" in the table, and look for the column of the first letter of the text to encrypt, "c". To the second letter, we should find the line of the letter "b" on the column "i", and so on, up to we find as result:
    • qpudvbss aqjmbdm

Once that do the encryption of all words in a message is a hard work, João and Enzo chosen to encrypt only words that initiate with a consonant letter. For this reason, they just apply the keyword on the words that they want to encrypt.

Given a keyword and a text message, your task is to encrypt this message with Vigènere cipher according to the João and Enzo added rule.

 

Input

 

The first line of the input consists of a keyword K (3 ≤ K ≤ 45), that represents the key to the cipher. This word is just composed by the alphabet (a-z) in lowercase, without spaces. The next line contains an integer N (1 ≤ N ≤ 150) that indicates how many messages we need to encrypt. The next N lines are the messages. These messages do not pass 105 characters and they are composed by the alphabet (a-z) in lowercase and by spaces.

 

Output

 

The output should display the encrypted message according to the friends rule.

 

 

 

Input Samples Output Samples
obi
2
olimpiada brasileira de informatica
ciencia da computacao
olimpiada psigjtsjzo em informatica
qjmbdqo ei qpudvbodic

 

 

 

informatica
2
ciencia da computacao
olimpiada brasileira de informatica
kvjbtua wi eouczhroah
olimpiada jefgzxebzc dm informatica

 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const [key, numLines, ...lines] = readFileSync("/dev/stdin", "utf8").split("\n")

const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toUpperCase()

function VigenèreCipher(private_key) {
	const PRIVATE_KEY = String(private_key ?? "")

	function createTable() {
		const cypherTable = {}

		for (let row = 0; row  <  PRIVATE_KEY.length; row++) {
			cypherTable[PRIVATE_KEY.charAt(row)] = {}

			for (let col = 0; col  <  PRIVATE_KEY.length; col++)
				cypherTable[PRIVATE_KEY.charAt(row)][PRIVATE_KEY.charAt(col)] = PRIVATE_KEY.charAt((row + col) % PRIVATE_KEY.length)
		}

		return cypherTable
	}

	const CIPHER = createTable()

	function encryptText(key, text) {
		let index = 0

		const encrypted = text.replace(
			/\b((?![aeiou])[a-z][a-z]+)\b/gi,
			(match) => {
				return Array
					.from(match, (char) => CIPHER[key.charAt(index++ % key.length).toUpperCase()][char.toUpperCase()] || char)
					.join("")
					.toLowerCase()
			})

		return encrypted
	}

	return {
		cipher: CIPHER,
		encrypt: encryptText
	}
}

const { encrypt } = VigenèreCipher(ALPHABET)

function main() {
	const responses = lines
		.slice(0, +numLines)
		.map(line => encrypt(key, line))

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
#2850 Beecrowd Online Judge Solution 2850 Polyglot Parrot Solution in C, C++, Java, Js and Python
Next
#2861 Beecrowd Online Judge Solution 2861 The Output Solution in C, C++, Java, Js and Python