fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6. int n,m;
  7.  
  8. char x,y;
  9.  
  10. int magic[30]; //It is an array where magic happens
  11.  
  12. string str;
  13.  
  14. cin>>n>>m;
  15.  
  16. cin>>str;
  17.  
  18. for(int i=0;i<26;++i){ // magic[0] means value of 'a' and so on
  19.  
  20. magic[i]=i;
  21.  
  22. }
  23.  
  24.  
  25.  
  26. for(int i=1;i<=m;++i){
  27.  
  28. cin>>x>>y;
  29.  
  30. int posa,posb;
  31.  
  32. for(int j=0;j<26;++j){ //search for the value which corresponds to x and search for value which corresponds to y
  33.  
  34. if(magic[j]==x-'a'){
  35. posa=j;
  36. }
  37.  
  38. if(magic[j]==y-'a'){
  39. posb=j;
  40. }
  41. }
  42.  
  43. magic[posa]=y-'a'; //if 'a' is replaced by 'b' by one of the persons then magic[0] will keep 1(the value of 'b')
  44.  
  45. magic[posb]=x-'a'; //and magic[1] will contain 0(the value of 'a')
  46.  
  47. }
  48.  
  49.  
  50. // magic array contains final values of each character
  51.  
  52.  
  53. int len=str.size();
  54.  
  55. for(int i=0;i<len;++i){
  56.  
  57. str[i]=magic[str[i]-'a']+'a';
  58.  
  59. }
  60.  
  61. cout<<str<<'\n';
  62.  
  63. return 0;
  64.  
  65. }
Runtime error #stdin #stdout 0s 2684KB
stdin
Standard input is empty
stdout
Standard output is empty