Thursday, February 10, 2011

TopCoder SRM 148, DIV II, CeyKaps

Problem Statement

The keycaps on a keyboard have been switched around, and the user is now trying to remember what he was trying to type.

Create a class CeyKaps containing the method decipher that takes a String typed, representing the visible message on the screen, and a String[] switched, representing the keycap switches. The method should return the original intended message (what keys the user thought he was pressing).

A keycap can be switched around more than once. For example, if someone switched around 'A' and 'S', then switched around 'S' and 'D', then 'D' would be where 'A' originally was, 'S' where 'D' was, and 'A' where 'S' was.

The elements of switches will be formatted as (quotes added for clarity) "*:*", where the *'s represent the keycaps to be switched. The above example would be represented as: {"A:S","S:D","D:A"}, or alternately as {"S:A","D:S","A:D"} or any other such combination. The order of the keycaps doesn't matter, but the order of the switches does.


class CeyKaps {

public:

string decipher(string typed, vector <string> switches) {
 vector < pair<char,char> > keys;
 repsz(i, switches){
   char x,y;
   x=switches[i][0];
   y=switches[i][2];
   keys.pb(mp(x,y));
 }
 repsz(i,keys){
   repsz(j,typed){
     if(typed[j]==keys[i].first)typed[j]=keys[i].second;
     else if(typed[j]==keys[i].second)typed[j]=keys[i].first;
   }
 }
 return typed;
}

};

No comments:

Post a Comment