alexjercan/bugnet huggingface dataset:
Problem p00135 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int Tc;
  cin >> Tc;
  int H, M;

  while (Tc--) {
    scanf("%d:%d", &H, &M);

    double h = H * 30.0 + M / 2.0, m = M * 6;

    int ang = abs(h - m);
    if (ang > 180)
      ang = 360 - ang;
    if (0 <= ang && ang < 30) {
      cout << "alert" << endl;
    } else if (90 <= ang && ang <= 180) {
      cout << "safe" << endl;
    } else
      cout << "warning" << endl;
  }

  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

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

string func(int H, int M) {
  /*double a = l * 6;
  double b = s * 30.0 + l / 2.0;
  int m = abs(a - b);
  if(180 < m)m = 360 - m;
  if(0 <= m && m < 30)return "alert";
  if(90 <= m && m <= 180)return "safe";
  return "waring";
  if(m > 180)m = 360 - m;
  if(30 <= m){
          if(90 <= m)return "safe";
          return "warning";
  }
  return "alert";*/
  double h = H * 30.0 + M / 2.0, m = M * 6;

  int ang = abs(h - m);
  if (ang > 180)
    ang = 360 - ang;
  if (0 <= ang && ang < 30) {
    cout << "alert" << endl;
  } else if (90 <= ang && ang <= 180) {
    cout << "safe" << endl;
  } else
    cout << "warning" << endl;
}

int main() {
  int n, s, l;
  cin >> n;
  char c;
  while (n--) {
    cin >> s >> c >> l;
    func(s, l);
  }
  return 0;
}
Problem p00135 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int a, b, n;
double d, e, f;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d:%d", &a, &b);
    f = abs(a * 30 - b * 11.0 / 2.0);
    if (f > 180) {
      f = 360 - f;
    }
    if (f < 30) {
      cout << "alert" << endl;
    } else if (f < 90) {
      cout << "warning" << endl;
    } else {
      cout << "safe" << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int a, b, n;
double d, e, f;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d:%d", a, b);
    d = b * 6;
    e = 1.0 * (a * 30 + b) / 2.0;
    f = abs(d - e);
    if (f > 180) {
      f = 360 - f;
    }
    if (f < 30) {
      cout << "alert" << endl;
    } else if (f < 90) {
      cout << "warning" << endl;
    } else {
      cout << "safe" << endl;
    }
  }
}
Problem p00136 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, b[6] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    double h;
    cin >> h;
    if (h < 165)
      b[0]++;
    if (165 <= h && h < 170)
      b[1]++;
    if (170 <= h && h < 175)
      b[2]++;
    if (175 <= h && h < 180)
      b[3]++;
    if (180 <= h && h < 185)
      b[4]++;
    if (h >= 185)
      b[5]++;
  }
  for (int i = 0; i < 6; i++) {
    cout << i + 1 << ":";
    for (int j = 0; j < b[i]; j++) {
      cout << "*";
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    int s[15] = {};
    for (int i = 0; i < n; i++)
      cin >> s[i];

    int ans = 0;
    int c[15];
    while (1) {
      bool issame = true;
      for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = 0; j < n; j++) {
          if (s[i] == s[j])
            sum++;
        }
        if (s[i] != sum)
          issame = false;
        c[i] = sum;
      }
      if (issame) {
        cout << ans << endl;
        for (int j = 0; j < n; j++) {
          if (j == n - 1)
            cout << s[j] << "\\n";
          else
            cout << s[j] << " ";
        }
        break;
      }
      for (int j = 0; j < n; j++) {
        s[j] = c[j];
      }
      ans++;
    }
  }

  return 0;
}
Problem p00137 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  long n, m, i, j;
  string s, t;
  stringstream ss;
  string::size_type k;
  char mm[9];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> m;
    cout << "Case " << i + 1 << ":" << endl;
    for (j = 0; j < 10; j++) {
      sprintf(mm, "%08d", m * m);
      s = string(mm);
      t = s.substr(2, 4);
      k = t.find_first_not_of('0');
      if (k == string::npos)
        cout << '0' << endl;
      else
        cout << t.substr(k) << endl;
      ss.str("");
      ss.clear();
      ss << t;
      ss >> m;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
  long n, m, i, j;
  string s, t;
  stringstream ss;
  string::size_type k;
  char mm[9];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> m;
    cout << "Case " << i + 1 << ":" << endl;
    for (j = 0; j < 10; j++) {
      sprintf(mm, "%08d", m * m);
      s = string(mm);
      t = s.substr(2, 4);
      k = t.find_first_not_of('0');
      cout << t.substr(k) << endl;
      ss.str("");
      ss.clear();
      ss << t;
      ss >> m;
    }
  }
  return 0;
}
Problem p00138 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct score {
  int n;
  double t;
};
int main() {
  score a[8], o[8], x, b[18];
  int c1 = 0, c2 = 0;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 8; ++j) {
      cin >> x.n >> x.t;
      a[j] = x;
    }
    for (int j = 0; j < 8; ++j) {
      for (int k = 7; j < k; --k) {
        if (a[j].t > a[k].t) {
          swap(a[j], a[k]);
        }
      }
    }
    for (int j = 0; j < 8; ++j) {
      if (j < 2) {
        o[c1] = a[j];
        ++c1;
      } else {
        b[c2] = a[j];
        ++c2;
      }
    }
  }
  for (int i = 0; i < 18; ++i) {
    for (int j = 17; j > i; --j) {
      if (b[i].t > b[j].t) {
        swap(b[i], b[j]);
      }
    }
  }
  for (int i = 0; i < 2; ++i) {
    o[c1] = b[i];
    ++c1;
  }
  for (int i = 0; i < 8; ++i) {
    printf("%d %.2f\\n", o[i].n, o[i].t);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
struct score {
  int n;
  double t;
};
int main() {
  score a[8], o[8], x, b[18];
  int c1 = 0, c2 = 0;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 8; ++j) {
      cin >> x.n >> x.t;
      a[j] = x;
    }
    for (int j = 0; j < 8; ++j) {
      for (int k = 7; j < k; ++k) {
        if (a[j].t > a[k].t) {
          swap(a[j], a[k]);
        }
      }
    }
    for (int j = 0; j < 8; ++j) {
      if (j < 2) {
        o[c1++] = a[j];
      } else
        b[c2++] = a[j];
    }
  }
  for (int i = 0; i < 18; ++i) {
    for (int j = 17; j > i; ++j) {
      if (b[i].t > b[j].t) {
        swap(b[i], b[j]);
      }
    }
  }
  for (int i = 0; i < 2; ++i) {
    o[c1++] = b[i];
  }
  for (int i = 0; i < 8; ++i) {
    cout << o[i].n << " " << o[i].t << endl;
  }
  return 0;
}
Problem p00138 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define EPS 1e-10
#define F first
#define S second
using namespace std;
typedef pair<int, double> P;

bool cmp(const P &p, const P &q) {
  if (!(fabs(p.S - q.S) <= EPS))
    return p.S < q.S;
  return p.F > q.F;
}

int main() {
  P info[30];
  bool used[30];
  for (int i = 0; i < 24; i++) {
    cin >> info[i].F >> info[i].S;
    used[i] = false;
  }
  sort(info, info + 7, cmp);
  printf("%d %.2lf\\n", info[0].F, info[0].S);
  used[info[1].F] = true;
  printf("%d %.2lf\\n", info[1].F, info[1].S);
  used[info[0].F] = true;
  sort(info + 8, info + 15, cmp);
  printf("%d %.2lf\\n", info[8].F, info[8].S);
  used[info[8].F] = true;
  printf("%d %.2lf\\n", info[9].F, info[9].S);
  used[info[9].F] = true;
  sort(info + 16, info + 23, cmp);
  printf("%d %.2lf\\n", info[16].F, info[16].S);
  used[info[16].F] = true;
  printf("%d %.2lf\\n", info[17].F, info[17].S);
  used[info[17].F] = true;
  sort(info, info + 23, cmp);
  int cnt = 0;
  for (int i = 0; i < 24; i++) {
    if (!used[info[i].F]) {
      cnt++;
      printf("%d %.2lf\\n", info[i].F, info[i].S);
    }
    if (cnt == 2)
      break;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define EPS 1e-10
#define F first
#define S second
using namespace std;
typedef pair<int, double> P;

bool cmp(const P &p, const P &q) {
  if (!(fabs(p.S - q.S) <= EPS))
    return p.S < q.S;
  return p.F > q.F;
}

int main() {
  P info[24];
  bool used[24];
  for (int i = 0; i < 24; i++) {
    cin >> info[i].F >> info[i].S;
    used[i] = false;
  }
  sort(info, info + 7, cmp);
  printf("%d %.2f\\n", info[0].F, info[0].S);
  used[info[1].F] = true;
  printf("%d %.2f\\n", info[1].F, info[1].S);
  used[info[0].F] = true;
  sort(info + 8, info + 15, cmp);
  printf("%d %.2f\\n", info[8].F, info[8].S);
  used[info[8].F] = true;
  printf("%d %.2f\\n", info[9].F, info[9].S);
  used[info[9].F] = true;
  sort(info + 16, info + 23, cmp);
  printf("%d %.2f\\n", info[16].F, info[16].S);
  used[info[16].F] = true;
  printf("%d %.2f\\n", info[17].F, info[17].S);
  used[info[17].F] = true;
  sort(info, info + 23, cmp);
  int cnt = 0;
  for (int i = 0; i < 24; i++) {
    if (!used[info[i].F]) {
      cnt++;
      printf("%d %.2f\\n", info[i].F, info[i].S);
    }
    if (cnt == 2)
      break;
  }
}
Problem p00138 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main() {
  pair<double, int> runners[24];
  for (int i = 0; i < 24; ++i) {
    int a;
    double b;
    scanf("%d %lf", &a, &b);
    runners[i] = make_pair(b, a);
  }
  for (int i = 0; i < 3; ++i) {
    sort(runners + i * 8, runners + 8 + i * 8);
    printf("%d %.2f\\n", runners[0 + i * 8].second, runners[0 + i * 8].first);
    printf("%d %.2f\\n", runners[1 + i * 8].second, runners[1 + i * 8].first);
    runners[0 + i * 8].first = runners[1 + i * 8].first = 1e10;
  }
  sort(runners, runners + 24);
  printf("%d %.2f\\n", runners[0].second, runners[0].first);
  printf("%d %.2f\\n", runners[1].second, runners[1].first);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main() {
  pair<double, int> runners[24];
  for (int i = 0; i < 24; ++i) {
    int a;
    double b;
    scanf("%d %lf", &a, &b);
    runners[i] = make_pair(b, a);
  }
  for (int i = 0; i < 3; ++i) {
    sort(runners + i * 8, runners + 8 + i * 8);
    printf("%d %.2f\\n", runners[0 + i * 8].second, runners[0 + i * 8]);
    printf("%d %.2f\\n", runners[1 + i * 8].second, runners[1 + i * 8]);
    runners[0 + i * 8].first = runners[1 + i * 8].first = 1e10;
  }
  sort(runners, runners + 24);
  printf("%d %.2f\\n", runners[0].second, runners[0].first);
  printf("%d %.2f\\n", runners[1].second, runners[1].first);

  return 0;
}
Problem p00138 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits.h>

using namespace std;

struct p_tag {
  int num;
  double time;
} P[3][8], tmp;

void sort() {
  int i, j, k;
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 8; j++) {
      for (k = j + 1; k < 8; k++) {
        if (P[i][j].time > P[i][k].time) {
          tmp = P[i][j];
          P[i][j] = P[i][k];
          P[i][k] = tmp;
        }
      }
    }
  }

  return;
}

int main(void) {
  int i, j, min;

  for (i = 0; i < 3; i++)
    for (j = 0; j < 8; j++)
      cin >> P[i][j].num >> P[i][j].time;

  sort();

  for (i = 0; i < 3; i++)
    for (j = 0; j < 2; j++)
      printf("%d %.2f\\n", P[i][j].num, P[i][j].time);

  for (i = 0; i < 2; i++) {
    min = 0;
    for (j = 1; j < 3; j++)
      if (P[min][2].time > P[j][2].time)
        min = j;
    printf("%d %.2f\\n", P[min][2].num, P[min][2].time);
    P[min][2].time = INT_MAX;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  double t[1000] = {};
  vector<double> ans;
  vector<double> p;

  for (int i = 0; i < 3; ++i) {
    vector<double> vc;
    for (int j = 0; j < 8; ++j) {
      int No;
      double time;
      scanf("%d %lf", &No, &time);
      t[No] = time;
      vc.push_back(time);
      p.push_back(time);
    }
    sort(vc.begin(), vc.end());
    ans.push_back(vc[0]);
    ans.push_back(vc[1]);
  }
  vector<double> foo;
  for (int i = 0; i < p.size(); ++i) {
    bool flag = true;
    for (int j = 0; j < (int)ans.size(); ++j)
      if (p[i] == ans[j])
        flag = false;

    if (flag)
      foo.push_back(p[i]);
  }
  sort(foo.begin(), foo.end());
  ans.push_back(foo[0]);
  ans.push_back(foo[1]);

  for (int i = 0; i < (int)ans.size(); ++i) {
    for (int j = 0; j < 25; ++j) {
      if (t[j] == ans[i])
        printf("%d %.2f\\n", j, ans[i]);
    }
  }
}
Problem p00139 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string vbString(int Number, string Character) {
  string res = "";
  for (int i = 0; i < Number; i++)
    res += Character;
  return res;
}
string checksnake(string s) {
  string t;
  for (int i = 1; i < 100; i++) {
    t = ">'" + vbString(i, "=") + "#" + vbString(i, "=") + "~";
    if (t == s)
      return "A";
  }

  for (int i = 1; i < 100; i++) {
    t = ">^" + vbString(i, "Q=") + "~~";
    if (t == s)
      return "B";
  }
  return "NA";
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    cout << checksnake(s) << endl;
  }
}

Error: TLE

Fixed submission:

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

string vbString(int Number, string Character) {
  string res = "";
  for (int i = 0; i < Number; i++)
    res += Character;
  return res;
}
string checksnake(string s) {
  string t;
  for (int i = 1; i < 600; i++) {
    t = ">'" + vbString(i, "=") + "#" + vbString(i, "=") + "~";
    if (t == s)
      return "A";
  }

  for (int i = 1; i < 600; i++) {
    t = ">^" + vbString(i, "Q=") + "~~";
    if (t == s)
      return "B";
  }
  return "NA";
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    cout << checksnake(s) << endl;
  }
}
Problem p00139 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

string afunc(string str) {
  string res = "NA";
  if (str[0] == '#') {
    return res;
  }

  for (int i = 0, j = str.size() - 1; i <= j; i++, j--) {
    if (i == j) {
      if (str[i] == '#') {
        res = "A";
      }
    } else if (str[i] != '=' || str[j] != '=') {
      break;
    }
  }

  return res;
}

string bfunc(string str) {
  string res = "B";
  if (str.size() == 0) {
    return "NA";
  }
  for (int i = 0; i < str.size(); i += 2) {
    if (str[i] != 'Q' || str[i + 1] != '=') {
      res = "NA";
      break;
    }
  }

  return res;
}

int main() {
  int n;
  string str, res;

  while (cin >> n && n) {
    while (n--) {
      cin >> str;
      res = "NA";
      if ('>' == str[0]) {
        str.erase(str.begin());
        int l = str.size();
        if (str[0] == '\\'' && str[l - 1] == '~') {
          str.erase(str.end() - 1);
          str.erase(str.begin());
          if (str.size() % 2 == 1) {
            res = afunc(str);
          }
        } else if (str[0] == '^' && str[l - 1] == str[l - 2] &&
                   str[l - 1] == '~') {
          str.erase(str.end() - 1);
          str.erase(str.end() - 1);
          str.erase(str.begin());

          if (str.size() % 2 == 0) {
            res = bfunc(str);
          }
        }
      }

      cout << res << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
  int n, f, m;
  char a[101], w;
  cin >> m;

  while (m--) {
    f = 0;
    scanf("%s", a);
    w = (a[1] == '^') ? 'B' : 'A';
    n = strlen(a);
    if (w == 'A') {
      if (a[n - 1] == '~') {
        for (int i = 2; i < n - i; i++) {
          if (a[i] != a[n - i]) {
            f = 1;
            break;
          }
        }
      } else {
        f = 1;
      }
    } else {
      if (a[n - 1] == '~' && a[n - 2] == '~') {
        for (int i = 2; i < n - 2; i += 2) {
          if (a[i] != 'Q' && a[i + 1] != '=') {
            f = 1;
            break;
          }
        }
      } else {
        f = 1;
      }
    }

    if (f == 1) {
      cout << "NA" << endl;
    } else {
      cout << w << endl;
    }
  }

  return 0;
}
Problem p00139 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef set<string> SS;
SS SA;
SS SB;

bool checkA(string s) { return SA.count(s) > 0; }

bool checkB(string s) { return SB.count(s) > 0; }

string solve(string s) {
  if (checkA(s))
    return "A";
  if (checkB(s))
    return "B";
  return "NA";
}

int main() {
  for (int i = 1; i <= 100; ++i) {
    SA.insert(">'" + string(i, '=') + "#" + string(i, '=') + "~");
  }
  for (int i = 1; i <= 100; ++i) {
    VS con(i, "Q=");
    SB.insert(">^" + accumulate(con.begin(), con.end(), string()) + "~~");
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    cout << solve(s) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

typedef set<string> SS;

bool checkA(string s) {
  SS S;
  for (int i = 1; i <= 100; ++i) {
    S.insert(">'" + string(i, '=') + "#" + string(i, '=') + "~");
  }
  return S.count(s) > 0;
}

bool checkB(string s) {
  SS S;
  for (int i = 1; i <= 100; ++i) {
    VS con(i, "Q=");
    S.insert(">^" + accumulate(con.begin(), con.end(), string()) + "~~");
  }
  return S.count(s) > 0;
}

string solve(string s) {
  if (checkA(s))
    return "A";
  if (checkB(s))
    return "B";
  return "NA";
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    cout << solve(s) << endl;
  }
  return 0;
}
Problem p00139 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

int main(void) {
  int max, data;
  int num;
  int cut_i;

  char hebi[201];
  scanf("%d", &max);

  for (data = 0; data < max; data++) {
    scanf("%s", hebi);

    if (strlen(hebi) < 2 || hebi[0] != '>') {
      puts("NA");
      continue;
    }

    if (hebi[1] == '\\'') {
      num = 0;
      cut_i = 2;

      while (hebi[cut_i] == '=') {
        cut_i++;
        num++;
      }

      if (hebi[cut_i] != '#' || num <= 0) {
        puts("NA");
      } else {
        cut_i++;

        while (hebi[cut_i] == '=') {
          cut_i++;
          num--;
        }

        if (num != 0) {
          puts("NA");
        } else if (hebi[cut_i] != '~' || hebi[cut_i + 1] != 0) {
          puts("NA");
        } else {
          puts("A");
        }
      }
    }

    else if (hebi[1] == '^') {
      cut_i = 2;
      while (hebi[cut_i] == 'Q' && hebi[cut_i + 1] == '=') {
        cut_i += 2;
      }

      if (cut_i == 2) {
        puts("NA");
      }

      else if (hebi[cut_i] != '~' || hebi[cut_i + 1] != '~' ||
               hebi[cut_i + 2] != 0) {
        puts("NA");
      } else {
        puts("B");
      }
    } else {
      puts("NA");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {
  char str[] = "特に意味のない文章";
  int cut_i, cut_j;
  int len, kaisu;
  int hantei;

  scanf("%d", &kaisu);

  for (cut_i = 0; cut_i < kaisu; cut_i++) {
    scanf("%s", str);
    len = strlen(str);
    hantei = 0;

    if ((len < 5) || (str[0] != '>') || (len % 2 != 0)) {
      printf("NA\\n");
      continue;
    }

    len--;

    if ((str[1] == '\\'') && (str[len] == '~')) {
      cut_j = 2;
      while (str[cut_j] == '=') {
        hantei++;
        cut_j++;
      }

      if (str[cut_j] != '#') {
        printf("NA\\n");
      } else {
        cut_j++;

        while (str[cut_j] == '=') {
          hantei--;
          cut_j++;
        }

        if (hantei == 0) {
          printf("A\\n");
        } else {
          printf("NA\\n");
        }
      }
    }

    else if ((str[1] == '^') && (str[len - 1] == str[len]) &&
             (str[len] == '~')) {
      for (cut_j = 2; cut_j < len - 2; cut_j += 2) {
        if ((str[cut_j] != 'Q') || (str[cut_j + 1] != '=')) {
          hantei = -1;
        }
      }
      printf("B\\n");
    } else {
      printf("NA\\n");
      continue;
    }
  }
  return 0;
}