Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 3), problem: (B) Santa Claus and Keyboard Check Solution in C/C++
#include<stdio.h> typedef unsigned u; char M[128],A[1024],B[1024]; int main() { u i=-1,l=0; for(scanf("%s%s",A,B);A[++i];) { if(M[(u)A[i]]&&M[(u)A[i]]!=B[i]) {printf("-1\n");return 0;} if(M[(u)B[i]]&&M[(u)B[i]]!=A[i]) {printf("-1\n");return 0;} if(!M[(u)A[i]]&&B[i]!=A[i])++l; M[(u)A[i]]=B[i];M[(u)B[i]]=A[i]; } for(printf("%u\n",l),i=-1;++i<128;)if((u)M[i]>i)printf("%c %c\n",i,M[i]); return 0; }