Cipher Discussions | Algorithms | HackerRank
  • + 1 comment

    C++

    string cipher(int k, string s)
    {
      int n = (int)s.size(), m = n-k+1, t = k+1;
      string r(m, 0), x(t, 0);
      for (int i = 0; i < m; i++) {
        x[i%t] = x[(i+1)%t] ^ (s[n-1-i]-'0');
        r[m-1-i] = (x[i%t] ^ x[(i+k)%t]) + '0';
      }
      return r;
    }