AMSCO2 - Decipher the AMSCO cipher

Here you have to decipher the AMSCO cipher:

Due to A.M.SCOtt in the 19th century, it's an incomplete columnar transposition cipher with alternating single letters and digraphs. The first entry must be a digraph. In both even and odd periods the first column and the first row always alternate:

7 4 5 6 3 2 1
RI D ER S ON T HE
S TO R MI N TO T
HI S HO U SE W EA
R EB O RN J IM M
OR R IS O N

Input

N lines (N < 1000) Each line of the input contains the numeric key (permutation order of the columns) and a ciphertext. Ciphertext letters are in [A-Z] only with no punctuation. The keylength max is 9 and the length of the ciphertext is limited to 250. The last line ends with EOF.

Output

Output consist of exactly N lines of plaintexts with letters in [A-Z] with no spaces.

Example

Input:
7456321 HETEAMTTOWIMONNSEJNDTOSEBRERRHOOISSMIURNORISHIROR

Output:
RIDERSONTHESTORMINTOTHISHOUSEWEAREBORNJIMMORRISON
Input:
41325 CECRTEGLENPHPLUTNANTEIOMOWIRSITDDSINTNALINESAALEMHATGLRGR

Output:
INCOMPLETECOLUMNARWITHALTERNATINGSINGLELETTERSANDDIGRAPHS

Added by:legrand
Date:2013-04-04
Time limit:0.100s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ADA95 GAWK BASH C C++ 4.3.2 CPP C99 LISP sbcl LISP clisp FORTRAN GO HASK JAVA LUA OCAML PAS-GPC PAS-FPC PERL PHP PIKE PYTHON PYTHON3 PY_NBC RUBY SCM guile SCM qobi
Resource:classical ciphers

hide comments
2013-06-06 20:21:57 vishal chaudhary
@legrand: plz check my code, its working fine but i m getting WA, code id: 9393145 ,thank you..
=>your code fails for a lot of testcases. you can debug it by more testcases generated with your amsco code.

i've tested many cases generated with my amsco code..works fine every time...don't know where it is wrong :'(
=>test with more testcases ending with an extra char like in the exemple 'riders on the strom'

Last edit: 2013-06-12 12:30:38
2013-06-06 20:21:57 legrand
no, the ciphertext's length is at least three times the key's length.
2013-06-06 20:21:57 Ricardo Monascal
Hello! Can the ciphertext be smaller than the key's length? Thank you.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.