fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. string shiftingLetters(string s, vector<int>& shifts) {
  6. int n = s.length();
  7. vector<int> prefix(n);
  8.  
  9.  
  10. prefix[n - 1] = shifts[n - 1] % 26;
  11. for (int i = n - 2; i >= 0; i--) {
  12. prefix[i] = (prefix[i + 1] + shifts[i]) % 26;
  13. }
  14.  
  15.  
  16. for (int i = 0; i < n; i++) {
  17. int shifted = ((s[i] - 'a') + prefix[i]) % 26;
  18. s[i] = 'a' + shifted;
  19. }
  20.  
  21. return s;
  22. }
  23.  
  24. int main() {
  25. string s = "abc";
  26. vector<int> shifts = {3, 5, 9};
  27.  
  28. string result = shiftingLetters(s, shifts);
  29. cout << "Shifted string: " << result << endl;
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Shifted string: rpl