Problem p00806 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

void dfs(int i, int n, const string dic[100], const bool b[300][100],
         vector<string> &tmp, vector<string> &ans) {
  if (i < -1)
    for (;;)
      ;
  if (i == -1) {
    string res;
    for (int j = (int)tmp.size() - 1; j >= 0; j--) {
      res += tmp[j];
      if (j > 0)
        res += ' ';
      else
        res += '.';
    }
    ans.push_back(res);
  }

  rep(j, n) if (b[i][j]) {
    int m = dic[j].length();
    tmp.push_back(dic[j]);
    dfs(i - m, n, dic, b, tmp, ans);
    tmp.pop_back();
  }
}

int main() {
  char f[128];
  f['a'] = '2';
  f['b'] = '2';
  f['c'] = '2';
  f['d'] = '3';
  f['e'] = '3';
  f['f'] = '3';
  f['g'] = '4';
  f['h'] = '4';
  f['i'] = '4';
  f['j'] = '5';
  f['k'] = '5';
  f['l'] = '5';
  f['m'] = '6';
  f['n'] = '6';
  f['o'] = '6';
  f['p'] = '7';
  f['q'] = '7';
  f['r'] = '7';
  f['s'] = '7';
  f['t'] = '8';
  f['u'] = '8';
  f['v'] = '8';
  f['w'] = '9';
  f['x'] = '9';
  f['y'] = '9';
  f['z'] = '9';

  for (int n; cin >> n, n;) {
    string dic[100];
    rep(i, n) cin >> dic[i];
    string seq;
    cin >> seq;

    string num[100];
    rep(i, n) rep(j, dic[i].length()) num[i] += f[dic[i][j]];

    bool b[300][100] =
        {}; // b[i][j] := ( num[j] が seq[0..i] の suffix かどうか )
    int cnt[300] = {};
    rep(i, seq.length()) {
      rep(j, n) {
        int m = num[j].length();
        if (m <= i + 1 && num[j] == seq.substr(i - m + 1, m) &&
            (i - m == -1 || cnt[i - m] > 0)) {
          b[i][j] = true;
          cnt[i]++;
        }
      }
    }

    vector<string> ans, tmp;
    dfs(seq.length() - 1, n, dic, b, tmp, ans);
    rep(i, ans.size()) cout << ans[i] << endl;
    cout << "--" << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

void dfs(int i, int n, const string dic[100], const bool b[300][100],
         vector<string> &tmp, vector<string> &ans) {
  if (i < -1)
    return;
  if (i == -1) {
    string res;
    for (int j = (int)tmp.size() - 1; j >= 0; j--) {
      res += tmp[j];
      if (j > 0)
        res += ' ';
      else
        res += '.';
    }
    ans.push_back(res);
  }

  rep(j, n) if (b[i][j]) {
    int m = dic[j].length();
    tmp.push_back(dic[j]);
    dfs(i - m, n, dic, b, tmp, ans);
    tmp.pop_back();
  }
}

int main() {
  char f[128];
  f['a'] = '2';
  f['b'] = '2';
  f['c'] = '2';
  f['d'] = '3';
  f['e'] = '3';
  f['f'] = '3';
  f['g'] = '4';
  f['h'] = '4';
  f['i'] = '4';
  f['j'] = '5';
  f['k'] = '5';
  f['l'] = '5';
  f['m'] = '6';
  f['n'] = '6';
  f['o'] = '6';
  f['p'] = '7';
  f['q'] = '7';
  f['r'] = '7';
  f['s'] = '7';
  f['t'] = '8';
  f['u'] = '8';
  f['v'] = '8';
  f['w'] = '9';
  f['x'] = '9';
  f['y'] = '9';
  f['z'] = '9';

  for (int n; cin >> n, n;) {
    string dic[100];
    rep(i, n) cin >> dic[i];
    string seq;
    cin >> seq;

    string num[100];
    rep(i, n) rep(j, dic[i].length()) num[i] += f[dic[i][j]];

    bool b[300][100] =
        {}; // b[i][j] := ( num[j] が seq[0..i] の suffix かどうか )
    int cnt[300] = {};
    rep(i, seq.length()) {
      rep(j, n) {
        int m = num[j].length();
        if (m <= i + 1 && num[j] == seq.substr(i - m + 1, m) &&
            (i - m == -1 || cnt[i - m] > 0)) {
          b[i][j] = true;
          cnt[i]++;
        }
      }
    }

    vector<string> ans, tmp;
    dfs(seq.length() - 1, n, dic, b, tmp, ans);
    rep(i, ans.size()) cout << ans[i] << endl;
    cout << "--" << endl;
  }

  return 0;
}