Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

string S, R;
char T[5] = "ACGT";
int U, a, q;

int power[12] = {1,    4,     16,    64,     256,     1024,
                 4096, 16384, 65536, 262144, 1048576, 4194304};

bool V;

bool x1[4];
bool x2[16];
bool x3[64];
bool x4[256];
bool x5[1024];
bool x6[4096];
bool x7[16384];
bool x8[65536];
bool x9[262144];
bool x10[1048576];
bool x11[4194304];
bool x12[16777216];

void insert() {
  U = 0;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (T[j] == S[i]) {
        U += power[i] * j;
      }
    }
  }
  a = S.size();
  if (a == 1) {
    x1[U] = true;
  }
  if (a == 2) {
    x2[U] = true;
  }
  if (a == 3) {
    x3[U] = true;
  }
  if (a == 4) {
    x4[U] = true;
  }
  if (a == 5) {
    x5[U] = true;
  }
  if (a == 6) {
    x6[U] = true;
  }
  if (a == 7) {
    x7[U] = true;
  }
  if (a == 8) {
    x8[U] = true;
  }
  if (a == 9) {
    x9[U] = true;
  }
  if (a == 10) {
    x10[U] = true;
  }
  if (a == 11) {
    x11[U] = true;
  }
  if (a == 12) {
    x12[U] = true;
  }
}

void find() {
  U = 0;
  V = false;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (T[j] == S[i]) {
        U += power[i] * j;
      }
    }
  }
  a = S.size();
  if (a == 1 && x1[U] == true) {
    V = true;
  }
  if (a == 2 && x2[U] == true) {
    V = true;
  }
  if (a == 3 && x3[U] == true) {
    V = true;
  }
  if (a == 4 && x4[U] == true) {
    V = true;
  }
  if (a == 5 && x5[U] == true) {
    V = true;
  }
  if (a == 6 && x6[U] == true) {
    V = true;
  }
  if (a == 7 && x7[U] == true) {
    V = true;
  }
  if (a == 8 && x8[U] == true) {
    V = true;
  }
  if (a == 9 && x9[U] == true) {
    V = true;
  }
  if (a == 10 && x10[U] == true) {
    V = true;
  }
  if (a == 11 && x11[U] == true) {
    V = true;
  }
  if (a == 12 && x12[U] == true) {
    V = true;
  }
  if (V == true) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }
}

int main() {
  memset(x1, false, sizeof(x1));
  memset(x2, false, sizeof(x2));
  memset(x3, false, sizeof(x3));
  memset(x4, false, sizeof(x4));
  memset(x5, false, sizeof(x5));
  memset(x6, false, sizeof(x6));
  memset(x7, false, sizeof(x7));
  memset(x8, false, sizeof(x8));
  memset(x9, false, sizeof(x9));
  memset(x10, false, sizeof(x10));
  memset(x11, false, sizeof(x11));
  memset(x12, false, sizeof(x12));
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> R >> S;
    if (R == "insert") {
      insert();
    }
    if (R == "find") {
      find();
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1

#include <cmath>
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

#define MAX_LETTERS 17

const char Alphabet[4] = {'A', 'C', 'G', 'T'};

int cipher(string S) {
  int Ans = 0;

  for (string::size_type i = 0; i < S.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (Alphabet[j] == S[i]) {
        Ans +=
            pow<int>(sizeof(Alphabet) / sizeof(const char), S.size() - i - 1) *
            j;
      }
    }
  }

  return Ans;
}

int main() {
  vector<vector<bool>> D;

  unsigned n;

  for (int i = 0; i <= MAX_LETTERS; i++) {
    D.push_back(vector<bool>(
        pow<unsigned long long>(sizeof(Alphabet) / sizeof(const char), i),
        false));
  }

  scanf("%d", &n);

  vector<string> W;

  vector<bool> F(n);

  for (vector<bool>::size_type i = 0; i < n; i++) {
    char S1[20], S2[20];

    string S3, S4;

    scanf("%s%s", &S1, &S2);

    S3 = S1;
    S4 = S2;

    if (S3 == "find") {
      F[i] = true;
    }

    W.push_back(S4);
  }

  for (vector<string>::size_type i = 0; i < n; i++) {
    if (F[i] == false) {
      D[W[i].size()][cipher(W[i])] = true;
    } else {
      printf("%s\\n", (D[W[i].size()][cipher(W[i])] ? "yes" : "no"));
    }
  }

  return 0;
}