fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <algorithm>
  12. #define dibs reserve
  13. #define OVER9000 1234567890
  14. #define tisic 47
  15. #define soclose 10e-7
  16. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  17. #define chocolate win
  18. #define ff first
  19. #define ss second
  20. #define uint unsigned int
  21. // mylittlepony
  22. using namespace std;
  23.  
  24. int GCD(int a, int b) {
  25. if(a > b) swap(a,b);
  26. if(a == 0) return b;
  27. return GCD(b%a,a);}
  28.  
  29. int main() {
  30. int T,N;
  31. cin >> T;
  32. for(int t =0; t < T; t++) {
  33. string s,code;
  34. vector<int> dec(26,-1);
  35. vector<int> codeI(26);
  36. cin >> N >> s >> code;
  37. for(int i =0; i < 26; i++) codeI[code[i]-'a'] =i;
  38. vector< pair<int,int> > C;
  39. for(int i =0; i < s.length(); i++) if(dec[s[i]-'a'] < 0) {
  40. int akt =s[i]-'a', c =0;
  41. vector< pair<int,int> > V;
  42. vector<int> V0;
  43. while(dec[akt] == -1) {
  44. dec[akt] =-2;
  45. V.push_back(make_pair(akt,c));
  46. V0.push_back(akt);
  47. c++;
  48. akt =codeI[akt];}
  49. sort(V.begin(),V.end());
  50. // choose a valid solution
  51. int x =V.size()-1;
  52. for(int j =0; j < V.size(); j++) {
  53. bool ok =true;
  54. for(int k =0; k < C.size(); k++) {
  55. int d =GCD(C[k].ff,GCD(c,C[k].ss));
  56. if(V[j].ss%d != 0) ok =false;
  57. d =GCD(C[k].ff,GCD(c,V[j].ss));
  58. if(C[k].ss%d != 0) ok =false;
  59. if((C[k].ss-V[j].ss)%GCD(c,C[k].ff) != 0) ok =false;}
  60. if(ok) {
  61. x =V[j].ss;
  62. C.push_back(make_pair(c,V[j].ss));
  63. break;}
  64. }
  65. for(int j =0; j < V0.size(); j++) dec[V0[j]] =V0[(j+x)%V0.size()];
  66. }
  67. for(int i =0; i < s.length(); i++) cout << (char)('a'+dec[s[i]-'a']);
  68. cout << "\n";}
  69. return 0;}
  70.  
  71. // look at my code
  72. // my code is amazing
Success #stdin #stdout 0s 3480KB
stdin
3
7
pumiuau
xmurpwelifsobdhqkcajtzngvy
29
yzqjxynzysyrmttbjaymyrgyisvno
zcdefghijkamnopqrstulwxybv
24
bezmnftheuecthepegucagtz
cbafedihglkjonmrqputsxwvzy
stdout
acmicpc
ahlekashanamroozegarambadnist
beyondtheseathereisacity