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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int c2d(char c) { return c - '0'; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  string s;
  while (cin >> s, s != "0") {
    int N = SZ(s);
    VI dp(11, 0);
    int ans = 0, sum = 0;
    dp[0] = 1;
    for (int i = 0; i < N; ++i) {
      sum = (sum + (i % 2 ? -1 : 1) * c2d(s[i]) + 11) % 11;
      ans += dp[sum];
      if (i + 1 < N && c2d(s[i + 1]) != 0)
        dp[sum]++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int c2d(char c) { return c - '0'; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  string s;
  while (cin >> s, s != "0") {
    int N = SZ(s);
    VVI dp(2, VI(N + 1, 0));
    int crt = 0, nxt = 1;
    int ans = 0;
    for (int l = 1; l <= N; ++l) {
      for (int i = 0; i + l <= N; ++i) {
        dp[nxt][i] = c2d(s[i]) - dp[crt][i + 1];
        if (dp[nxt][i] % 11 == 0 && c2d(s[i]) != 0) {
          ++ans;
          //		  cout << l << "," << i << endl;
        }
      }
      swap(crt, nxt);
    }
    cout << ans << endl;
  }

  return 0;
}