Problem p01696 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>

#define MAX_N 1000

using namespace std;

char str[100], res[100];

void reverse(char *str, char *res) {
  for (int i = 0; i < strlen(str); i++) {
    res[i] = str[strlen(str) - i - 1];
  }
  res[strlen(str)] = '\0';
}

void dfs(int l, int r, char *res) {
  int prel = l;
  char tmp1[100], tmp2[100], count = 0;

  res[0] = '\0';
  if (l + 1 == r) {
    res[0] = str[l];
    if (res[0] == '?')
      res[0] = 'A';
    res[1] = '\0';
    return;
  }
  for (int i = l; i < r;) {
    if (str[i] == '+' || str[i] == '-' || (str[i] >= 'A' && str[i] <= 'Z')) {
      int j = i;
      while (str[j] == '+' || str[j] == '-')
        j++;
      if (i == l && j == r - 1) {
        res[0] = str[r - 1];
        if (res[0] == '?') {
          res[0] = 'A';
        } else {
          for (int k = l; k < r - 1; k++) {
            if (str[k] == '+') {
              res[0]++;
              if (res[0] > 'Z') {
                res[0] = 'A';
              }
            } else {
              res[0]--;
              if (res[0] < 'A') {
                res[0] = 'Z';
              }
            }
          }
        }
        res[1] = '\0';
        break;
      } else {
        dfs(i, j + 1, tmp1);
        strcat(res, tmp1);
        i = j + 1;
      }
    }
    if (str[i] == '[') {
      int j, count = 0;
      for (j = i; j < r; j++) {
        if (str[j] == '[') {
          count++;
        } else if (str[j] == ']') {
          count--;
        }
        if (count == 0)
          break;
      }
      dfs(i + 1, j, tmp1);
      reverse(tmp1, tmp2);
      strcat(res, tmp2);
      i = j + 1;
    }
  }
}

int main() {
  while (1) {
    cin >> str;
    if (str[0] == '.')
      break;
    dfs(0, strlen(str), res);
    cout << res << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>

#define MAX_N 1000

using namespace std;

char str[100], res[100];

void reverse(char *str, char *res) {
  for (int i = 0; i < strlen(str); i++) {
    res[i] = str[strlen(str) - i - 1];
  }
  res[strlen(str)] = '\0';
}

void dfs(int l, int r, char *res) {
  int prel = l;
  char tmp1[100], tmp2[100], count = 0;

  res[0] = '\0';
  if (l + 1 == r) {
    res[0] = str[l];
    if (res[0] == '?')
      res[0] = 'A';
    res[1] = '\0';
    return;
  }
  for (int i = l; i < r;) {
    if (str[i] == '+' || str[i] == '-' || (str[i] >= 'A' && str[i] <= 'Z') ||
        str[i] == '?') {
      int j = i;
      while (str[j] == '+' || str[j] == '-')
        j++;
      if (i == l && j == r - 1) {
        res[0] = str[r - 1];
        if (res[0] == '?') {
          res[0] = 'A';
        } else {
          for (int k = l; k < r - 1; k++) {
            if (str[k] == '+') {
              res[0]++;
              if (res[0] > 'Z') {
                res[0] = 'A';
              }
            } else {
              res[0]--;
              if (res[0] < 'A') {
                res[0] = 'Z';
              }
            }
          }
        }
        res[1] = '\0';
        break;
      } else {
        dfs(i, j + 1, tmp1);
        strcat(res, tmp1);
        i = j + 1;
      }
    }
    if (str[i] == '[') {
      int j, count = 0;
      for (j = i; j < r; j++) {
        if (str[j] == '[') {
          count++;
        } else if (str[j] == ']') {
          count--;
        }
        if (count == 0)
          break;
      }
      dfs(i + 1, j, tmp1);
      reverse(tmp1, tmp2);
      strcat(res, tmp2);
      i = j + 1;
    }
  }
}

int main() {
  while (1) {
    cin >> str;
    if (str[0] == '.')
      break;
    dfs(0, strlen(str), res);
    cout << res << endl;
  }
  return 0;
}