UVA Problem 11278 – One-Handed Typist Solution

UVA Problem 11278 – One-Handed Typist Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Before starting this you can check out a similar problem UVA Problem 10082 – WERTYU Solution. That problem is very similar to this and also somewhat similar to a Caesar Cipher with right shift of 1.

The problem deals with two keyboard layouts. The author typed dovrak key layout keys in standard keyboard layout by mistake. So he was actually pressing standard keys and now his output ( input in this problem ) looks scrambled. The task is to convert all typed standard key inputs to equivalent dvorak keys.

There are a few ways to solve this problem. One ways is to painstakingly map each standard key to a dvorak key.

Another way is to map ASCII table characters from Decimal 32 to 126 as standard keys. This can be done with a loop and another array with equivalent dvorak keys. This method is easier that is eliminates one array but then creating the dvorak array becomes time consuming.

The third way which which i used here is create two arrays containing standard and dvorak keys. Then map each standard key to a dvorak key.

The code below is explained with comments.

 

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer. Please compile with c++ compiler as some of my codes are in c and some in c++.


Input:

Hg66t Mty6k!
Jhg 4ibl; pytmn 8tc 5i79urrr
t,gy jhg 6fxo kt.r

 


Output:

Hello World!
The quick brown fox jumps...
over the lazy dog.

Code Output as Characters:

/*
 * Author:     Quickgrid ( Asif Ahmed )
 * Site:       https://quickgrid.wordpress.com
 * Problem:    UVA 11278 - One Handed Typist
 * Techniques: Array mapping, Character mapping
 */

#include<stdio.h>


#define N  128
#define IO 1001


/**
 * Heart of the problem. Be careful with mapping, one typo in mapping and the verdict is wrong.
 * Here standard contains all the characters in standard keyboard including capital and small.
 * dvorak contains all the characters in dvorak keyboard including capital and small.
 */
static const char standard[N] = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>? ";
static const char dvorak[N]   = "`123qjlmfp/[]456.orsuyb;=\\789aehtdck-0zx,inwvg'~!@#QJLMFP?{}$%^>ORSUYB:+|&*(AEHTDCK_)ZX<INWVG\" ";


/**
 * This is the mapping array. It holds mappings of dvorak to standard keyboard.
 */
static char mappingKB[N];


static char input[IO];


int main(){

    /**
     * Although mapping "mappingKB[ standard[i] ] = dvorak[i]" seems wrong and
     * mapping from "mappingKB[ dvorak[i] ] = standard[i]" seems correct.
     *
     * But its wrong remember the coach was typing dvorak in standard keyboard keys
     * instead of dvorak key mappings. Since the input was in standard keyboard and we should convert it to dvorak otherwise it looks
     * scrambled.
     *
     * So the keys of standard keyboard maps to dvorak keys.
     */
    for(int i = 0; dvorak[i]; ++i)
        mappingKB[ standard[i] ] = dvorak[i];



    while(gets(input)){

        /**
         * Convert standard keys to dvorak using pre-mapped array
         */
        for(int i = 0; input[i]; ++i)
            printf("%c", mappingKB[ input[i] ] );

        printf("\n");

    }


    return 0;
}

Code Output as String:

/*
 * Author:     Quickgrid ( Asif Ahmed )
 * Site:       https://quickgrid.wordpress.com
 * Problem:    UVA 11278 - One Handed Typist
 * Techniques: Array mapping, Character mapping
 */

#include<stdio.h>
#include<string.h>


#define N  128
#define IO 1001


static const char standard[N] = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>? ";
static const char dvorak[N]   = "`123qjlmfp/[]456.orsuyb;=\\789aehtdck-0zx,inwvg'~!@#QJLMFP?{}$%^>ORSUYB:+|&*(AEHTDCK_)ZX<INWVG\" ";

static char mappingKB[N];

static char input[IO];
static char output[IO];


int main(){

    for(int i = 0; dvorak[i]; ++i)
        mappingKB[ standard[i] ] = dvorak[i];


    while(gets(input)){

        // Reset memory otherwise character form previous input will be shown along with new
        memset(output, 0, sizeof output);

        for(int i = 0; input[i]; ++i)
            output[i] = mappingKB[ input[i] ];

        puts(output);

    }


    return 0;
}
Advertisements

One thought on “UVA Problem 11278 – One-Handed Typist Solution

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s