Problem p00015 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

char c[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

int main() {
  int n;
  cin >> n;
  string s1, s2;

  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());

    string s = "";

    if (s1.size() == s2.size()) {
      int r = 0, t = 0;
      for (int i = 0; i < (int)s1.size(); i++) {
        int x = s1[i] - c[0], y = s2[i] - c[0];
        int z = x + y + t;
        r = z % 10;
        t = z / 10;
        s += c[r];
      }
      if (t)
        s += c[t];
    } else if (s1.size() > s2.size()) {
      int r = 0, t = 0;
      for (int i = 0; i < (int)s2.size(); i++) {
        int x = s1[i] - c[0], y = s2[i] - c[0];
        int z = x + y + t;
        r = z % 10;
        t = z / 10;
        s += c[r];
      }
      if (t) {
        for (int i = (int)s2.size(); i < (int)s1.size(); i++) {
          int z = s1[i] - c[0] + t;
          r = z % 10;
          t = z / 10;
          s += c[r];
        }
        if (t)
          s += c[t];
      } else {
        for (int i = (int)s2.size(); i < (int)s1.size(); i++)
          s += c[s1[i] - c[0]];
      }
    } else {
      int r = 0, t = 0;
      for (int i = 0; i < (int)s1.size(); i++) {
        int x = s1[i] - c[0], y = s2[i] - c[0];
        int z = x + y + t;
        r = z % 10;
        t = z / 10;
        s += c[r];
      }
      if (t) {
        for (int i = (int)s1.size(); i < (int)s2.size(); i++) {
          int z = s2[i] - c[0] + t;
          r = z % 10;
          t = z / 10;
          s += c[r];
        }
        if (t)
          s += c[t];
      } else {
        for (int i = (int)s1.size(); i < (int)s2.size(); i++)
          s += c[s2[i] - c[0]];
      }
    }

    if (s.size() > 80)
      cout << "overflow" << endl;
    else {
      reverse(s.begin(), s.end());
      cout << s << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

typedef long long ll;

const ll BASE = 10000;
const int LogB = 4;
const int MAX_DIGIT = 100;

struct Bigint {
  ll digit[MAX_DIGIT];
  int size;
  Bigint(int sz = 1, ll a = 0) : size(sz) {
    memset(digit, 0, sizeof(digit));
    digit[0] = a;
  }
};

const Bigint ZERO(1, 0), ONE(1, 1);

bool operator<(Bigint x, Bigint y) {
  if (x.size != y.size)
    return x.size < y.size;
  for (int i = x.size - 1; i >= 0; i--)
    if (x.digit[i] != y.digit[i])
      return x.digit[i] < y.digit[i];
  return false;
}

bool operator>(Bigint x, Bigint y) { return y < x; }
bool operator<=(Bigint x, Bigint y) { return !(y < x); }
bool operator>=(Bigint x, Bigint y) { return !(x < y); }
bool operator!=(Bigint x, Bigint y) { return x < y || y < x; }
bool operator==(Bigint x, Bigint y) { return !(x < y) && !(y < x); }

Bigint normal(Bigint x) {
  ll c = 0;
  for (int i = 0; i < x.size; i++) {
    while (x.digit[i] < 0)
      x.digit[i + 1] -= 1, x.digit[i] += BASE;
    ll a = x.digit[i] + c;
    x.digit[i] = a % BASE;
    c = a / BASE;
  }
  for (; c > 0; c /= BASE)
    x.digit[x.size++] = c % BASE;
  while (x.size > 1 && x.digit[x.size - 1] == 0)
    x.size--;
  return x;
}

Bigint convert(ll a) { return normal(Bigint(1, a)); }

Bigint convert(const string &s) {
  Bigint x;
  int i = s.size() % LogB;
  if (i > 0)
    i -= LogB;
  for (; i < (int)s.size(); i += LogB) {
    ll a = 0;
    for (int j = 0; j < LogB; j++)
      a = 10 * a + (i + j >= 0 ? s[i + j] - '0' : 0);
    x.digit[x.size++] = a;
  }
  reverse(x.digit, x.digit + x.size);
  return normal(x);
}

ostream &operator<<(ostream &os, Bigint x) {
  os << x.digit[x.size - 1];
  for (int i = x.size - 2; i >= 0; i--)
    os << setw(LogB) << setfill('0') << x.digit[i];
  return os;
}

istream &operator>>(istream &is, Bigint &x) {
  string s;
  is >> s;
  x = convert(s);
  return is;
}

Bigint operator+(Bigint x, Bigint y) {
  if (x.size < y.size)
    x.size = y.size;
  for (int i = 0; i < y.size; i++)
    x.digit[i] += y.digit[i];
  return normal(x);
}

int main() {
  int n;
  cin >> n;
  string s1, s2;

  for (int i = 0; i < n; i++) {
    cin >> s1;
    cin >> s2;
    Bigint x = convert(s1), y = convert(s2);
    Bigint z = x + y;
    if (z.size > 80)
      cout << "overflow" << endl;
    else
      cout << z << endl;
  }

  return 0;
}