miércoles, 1 de junio de 2011

To and Fro

Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down 

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x


Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter. Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as

toioynnkpheleaigshareconhtomesnlewx

Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.
Input

There will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2...20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.
Output

Each input set should generate one line of output, giving the original plaintext message, with no spaces.


Example
Input:

5

toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Output:

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab


https://www.spoj.pl/problems/TOANDFRO/




public class ToAnFro 
{
   public static void main( String args[] ) throws Exception
   {
      int numColumnas = sig();
 
      while( numColumnas != 0 )
      {
         resolver( numColumnas );
         numColumnas = sig();
      }
   }
 
   public static void resolver( int cantCol ) throws Exception
   {
      // Almacenara la cadena de entrada, en funcion de su 
      // representacion ascci para cada caracter.
      int[] letras = new int[ 200  ];
      int cantLetras = 0;
  
      int car = System.in.read();
  
      // Leemos caracter por caracter y lo almaceamos uno despues
      // de otro.
      while( car != 10 )
      {
         letras[ cantLetras ++ ] = car;
         car = System.in.read();
      }

      // Cantidad de filas
      int cantFil = cantLetras / cantCol;
  
      char[] salida = new char[ cantLetras ];
  
      int incr = cantFil, pos = -1, i = 0;
  
      /*
      * Si estamos leendo de izquierda a derecha, cada caracter
      * ocupa la posicion del caracter anterior + cantFil.
      * Si estamos leendo de derecha a izquierda, cada caracter
      * ocupa la posicion del caracter anterior - cantFil
      */
      while( i < cantLetras )
      {
         pos = pos + 1;
   
         for( int j = 1; j < cantCol; pos = pos + incr, j ++ )
            salida[ pos ] = (char)letras[ i ++ ];
   
         salida[ pos ] = (char)letras[ i ++ ];
   
         // Indica si se lee de izquierda a derecha o de derecha a izquierda
         incr = - incr;
      }
  
      // Mostramos la cadena desencriptada
      System.out.println( new String( salida, 0, cantLetras ) );
   
   }
 
   public static int sig() throws Exception
   {
      int num = 0;
      int car = System.in.read();
  
      while( car != 10 )
      {
         num = num * 10 + ( car - 48 );
         car = System.in.read();
      }
  
      return num;
   }
}

1 comentario: