Problem p03533 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int t[101];
int pri[26];
int res = 0;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
  for (size_t i = 0; i <= n; i++) {
    pr[i] = i;
  }
}

int parent(int x) {
  if (x == pr[x])
    return x;
  return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
  int px = parent(x);
  int py = parent(y);

  if (px == py)
    return false;
  if (px < py) {
    pr[py] = px;
  } else {
    pr[px] = py;
  }
  return true;
}
bool comp(string left, string right) {
  if (left.size() != right.size())
    return false;

  for (size_t i = 0; i < left.size(); i++) {
    if (left[i] != right[i])
      return false;
  }
  return true;
}
bool rec(string s, string ori, int in) {
  if (ori.size() < in) {

    if (comp(s, "AKIHABARA")) {
      return true;
    }
    return false;
  }
  bool res =
      rec(s + "A" + (in < ori.size() ? string(1, ori[in]) : ""), ori, in + 1);
  res |= rec(s + (in < ori.size() ? string(1, ori[in]) : ""), ori, in + 1);
  return res;
}
void solv() {
  string ak = "AKIHABARA";

  string s;
  cin >> s;
  if (s == ak) {
    cout << "YES" << endl;
    return;
  }
  bool res = rec("", s, 0);
  if (res) {
    cout << "YES" << endl;
  } else
    cout << "NO" << endl;
}

int main() {
  solv();

  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int t[101];
int pri[26];
int res = 0;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
  for (size_t i = 0; i <= n; i++) {
    pr[i] = i;
  }
}

int parent(int x) {
  if (x == pr[x])
    return x;
  return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
  int px = parent(x);
  int py = parent(y);

  if (px == py)
    return false;
  if (px < py) {
    pr[py] = px;
  } else {
    pr[px] = py;
  }
  return true;
}
bool comp(string left, string right) {
  if (left.size() != right.size())
    return false;

  for (size_t i = 0; i < left.size(); i++) {
    if (left[i] != right[i])
      return false;
  }
  return true;
}
bool rec(string s, string ori, int in) {
  if (ori.size() < in) {

    if (comp(s, "AKIHABARA")) {
      return true;
    }
    return false;
  }
  bool res =
      rec(s + "A" + (in < ori.size() ? string(1, ori[in]) : ""), ori, in + 1);
  res |= rec(s + (in < ori.size() ? string(1, ori[in]) : ""), ori, in + 1);
  return res;
}
void solv() {
  string ak = "AKIHABARA";

  string s;
  cin >> s;
  if (s.size() > ak.size()) {
    cout << "NO" << endl;
    return;
  }
  if (s == ak) {
    cout << "YES" << endl;
    return;
  }
  bool res = rec("", s, 0);
  if (res) {
    cout << "YES" << endl;
  } else
    cout << "NO" << endl;
}

int main() {
  solv();

  return 0;
}