fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. string n_to_letter{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  8. map<char, int> letter_to_n;
  9. string key;
  10.  
  11. void build_letter_to_n()
  12. {
  13. for (int i = 0; i < n_to_letter.size(); ++i)
  14. letter_to_n[n_to_letter[i]] = i;
  15. }
  16.  
  17. string cipher(string in)
  18. {
  19. string out;
  20. for (int i = 0; i < in.size(); ++i)
  21. {
  22. int n = letter_to_n[in[i]];
  23. int k = letter_to_n[key[i % key.size()]];
  24. n = (n + k) % n_to_letter.size();
  25. out += n_to_letter[n];
  26. }
  27.  
  28. return out;
  29. }
  30.  
  31. int main()
  32. {
  33. cin >> n_to_letter; // alphabet
  34.  
  35. build_letter_to_n();
  36.  
  37. cin >> key;
  38.  
  39. string in;
  40. cin >> in;
  41.  
  42. cout << cipher(in);
  43.  
  44. return 0;
  45. }
  46.  
  47.  
Success #stdin #stdout 0s 15240KB
stdin
!ABCDEFGHIJKLMNOPQRSTUVWXYZ
SECRET
HELLOWORLD!
stdout
!JOCTPGWOVE