In this HackerEarth Bob - An Idiot! problem solution Andi and Bob were friends since childhood days. But, as they grew up Bob started behaving weirdly and this used to irritate Andi. Once, while Andi took a break after typing a large program Bob came from nowhere and swapped some alphabet keys on Andi's keyboard.

Andi got very angry on seeing this and decided to end their friendship once forever. As we all know Bob is very good at heart and never does anything wrong intentionally. He decided to type the remaining program with the same keyboard configuration. Given the original fragment of the code that Bob needs to type, You need to tell Bob the code that he should type to get the original code as output.

Help him save his friendship with Andi.


HackerEarth Bob - An Idiot ! problem solution


HackerEarth Bob - An Idiot problem solution.

#include<iostream>
#include<bits/stdc++.h>
#define ft first
#define sd second
using namespace std;
map<char,int> M1;
map<char,char> M2;
int main(){
 int n;
 cin>>n;
 assert(n<=1000000);
 char ch1,ch2,ch;
 for(int i=0;i<26;i++)
  M1['A'+i]=i;
 for(int i=0;i<n;i++){
  cin>>ch1>>ch2;
  swap(M1[ch1],M1[ch2]);
 }

 for(map<char,int> :: iterator it=M1.begin();it!=M1.end();++it)
 {
     M2[it->sd+'A']=it->ft;
     M2[it->sd+'a']=it->ft+32;
 }
 string str="";
 while(cin>>ch&&ch!=EOF){
  str+=ch;
 }
 n=str.length();

 for(int i=0;i<n;i++){
  if(M2[str[i]])
   cout<<M2[str[i]];
  else
   cout<<str[i];
 }
 cout<<endl;
 return 0;
}

Second solution

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define NIL 0
#define INF (1<<28)
#define MAXN 200001
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 5000000007
#define total 500005
#define M 1000000007
typedef long long int int64;
int pos[2000],val[2000];
string s;
int main(){
    int n,i;
    cin>>n;
    for(i=1;i<=200;i++){
        pos[i]=i;
        val[i]=i;
    }
    char x,y;
    for(i=1;i<=n;i++){
        cin>>x>>y;
        x=tolower(x);
        y=tolower(y);
        int f=val[int(x)];
        int s=val[int(y)];
        val[int(x)]=s;
        val[int(y)]=f;
    }
    for(i=1;i<=200;i++)
    pos[val[i]]=i;
    while(cin>>s){
        for(i=0;i<s.length();i++){
            x=tolower(s[i]);
            if(pos[int(x)]==int(x))
            cout<<s[i];
            else{
                int z=pos[int(x)];
                x=toupper(char(z));
                if(isupper(s[i]))
                cout<<x;
                else
                cout<<char(z);
            }
        }
        cout<<" ";
    }
    return 0;
}