Problem p01620 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define USE_CPP11
//  #define USE_PRINTF

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#ifdef USE_CPP11
#include <functional>
#include <unordered_map>
#include <unordered_set>
#endif

#define PRINT(obj) cout << obj << endl
#define REP(i, n) for (int(i) = 0; (i) < (n); i++)
#define SPACE " "

typedef long long ll;

const int INF = 1e9 + 10;

using namespace std;

namespace Scanner {
#ifdef USE_PRINTF
int nextInt() {
  int d;
  scanf("%d", &d);
  return d;
}
float nextFloat() {
  float f;
  scanf("%f", &f);
  return f;
}
double nextDouble() {
  double f;
  scanf("%lf", &f);
  return f;
}
string nextString() {
  string s;
  scanf("%s", &s);
  return s;
}
ll nextll() {
  ll l;
  scanf("%ll", &l);
  return l;
}
#else
int nextInt() {
  int d;
  cin >> d;
  return d;
}
float nextFloat() {
  float f;
  cin >> f;
  return f;
}
double nextDouble() {
  double f;
  cin >> f;
  return f;
}
string nextString() {
  string s;
  cin >> s;
  return s;
}
ll nextll() {
  ll l;
  cin >> l;
  return l;
}
#endif

string getLine() {
  string input;
  getline(cin, input);
  return input;
}
}; // namespace Scanner

namespace myLibrary {
vector<string> split(const string &str, char delim) {
  vector<string> ret;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    ret.push_back(string(str, current, found - current));
    current = found + 1;
  }
  ret.push_back(string(str, current, str.size() - current));
  return ret;
}

#ifdef USE_CPP11
///
/// higher-order functional
/// This is something like map in haskell, python, etc.
/// Sample usage:
///   vector<int> a = {1, 2, 3, 4, 5};
///   vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
///     return n * n;
///   });
/// Result:
///   b = 1, 4, 9, 16, 25
///
template <typename T_IN, typename T_OUT, class Functor>
vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
  vector<T_OUT> ret;
  for (int i = 0; i < src.size(); i++) {
    ret.push_back(func(src[i]));
  }
  return ret;
}
#endif

}; // namespace myLibrary

/// BEGIN //////////////////////////////////////////////////////////

int main() {
  char cs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
               'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
               'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
               'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

  while (true) {
    int n = Scanner::nextInt();
    if (n == 0) {
      break;
    }
    vector<int> key(n);

    REP(i, n) {
      int k = Scanner::nextInt();
      key[i] = k;
    }

    string s = Scanner::nextString();

    string ans = "";
    REP(i, s.size()) {
      REP(j, sizeof(cs)) {
        if (cs[j] == s[i]) {
          ans += cs[(j + sizeof(cs) - key[i % key.size()]) % sizeof(cs)];
        }
      }
    }
    PRINT(ans);
  }

  return 0;
}
/// END   //////////////////////////////////////////////////////////

Error: TLE

Fixed submission:

// #define USE_CPP11
//  #define USE_PRINTF

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#ifdef USE_CPP11
#include <functional>
#include <unordered_map>
#include <unordered_set>
#endif

#define PRINT(obj) cout << obj << endl
#define REP(i, n) for (int(i) = 0; (i) < (n); i++)
#define SPACE " "

typedef long long ll;

const int INF = 1e9 + 10;

using namespace std;

namespace Scanner {
#ifdef USE_PRINTF
int nextInt() {
  int d;
  scanf("%d", &d);
  return d;
}
float nextFloat() {
  float f;
  scanf("%f", &f);
  return f;
}
double nextDouble() {
  double f;
  scanf("%lf", &f);
  return f;
}
string nextString() {
  string s;
  scanf("%s", &s);
  return s;
}
ll nextll() {
  ll l;
  scanf("%ll", &l);
  return l;
}
#else
int nextInt() {
  int d;
  cin >> d;
  return d;
}
float nextFloat() {
  float f;
  cin >> f;
  return f;
}
double nextDouble() {
  double f;
  cin >> f;
  return f;
}
string nextString() {
  string s;
  cin >> s;
  return s;
}
ll nextll() {
  ll l;
  cin >> l;
  return l;
}
#endif

string getLine() {
  string input;
  getline(cin, input);
  return input;
}
}; // namespace Scanner

namespace myLibrary {
vector<string> split(const string &str, char delim) {
  vector<string> ret;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    ret.push_back(string(str, current, found - current));
    current = found + 1;
  }
  ret.push_back(string(str, current, str.size() - current));
  return ret;
}

#ifdef USE_CPP11
///
/// higher-order functional
/// This is something like map in haskell, python, etc.
/// Sample usage:
///   vector<int> a = {1, 2, 3, 4, 5};
///   vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
///     return n * n;
///   });
/// Result:
///   b = 1, 4, 9, 16, 25
///
template <typename T_IN, typename T_OUT, class Functor>
vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
  vector<T_OUT> ret;
  for (int i = 0; i < src.size(); i++) {
    ret.push_back(func(src[i]));
  }
  return ret;
}
#endif

}; // namespace myLibrary

/// BEGIN //////////////////////////////////////////////////////////

int main() {
  while (true) {
    int n = Scanner::nextInt();
    vector<int> key(n);

    REP(i, n) {
      int k = Scanner::nextInt();
      key[i] = k;
    }

    string s = Scanner::nextString();

    string ans = "";
    REP(i, s.size()) {
      char c = s[i] - key[i % key.size()];
      if (c < 65) {
        c += 58;
      }
      ans += c;
    }
    PRINT(ans);
  }

  return 0;
}
/// END   //////////////////////////////////////////////////////////