alexjercan/bugnet huggingface dataset:
Problem p00788 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int p, n;

struct data {
  double a, b; // a/b
  data() {}
  data(double a, double b) : a(a), b(b) {}
  double getValue() { return a / b; }
  bool operator<(const data &d) const { return a / b < d.a / d.b; }
};

vector<data> V;

void init() { V.clear(); }

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void solve() {

  data up((1 << 20), 1), low(-1, 1);

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      data tmp(i, j);
      if (tmp.getValue() < sqrt(p) && gcd(i, j) == 1) {
        if (low < tmp)
          low = tmp;
        break;
      }

      if (up.getValue() > tmp.getValue() && gcd(i, j) == 1)
        up = tmp;
    }

  cout << up.a << "/" << up.b << " " << low.a << "/" << low.b << endl;
}

int main() {

  while (cin >> p >> n && p + n) {
    init();
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int p, n;

vector<int> prime;

struct data {
  double a, b; // a/b
  data() {}
  data(double a, double b) : a(a), b(b) {}
  double getValue() { return a / b; }
  bool operator<(const data &d) const { return a / b < d.a / d.b; }
};

vector<data> V;

void init() { V.clear(); }

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void make() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i % j != 0 && gcd(i, j) == 1)
        V.push_back(data(i, j));
      if ((double)i / (double)j < sqrt(p) && gcd(i, j) == 1)
        break;
    }
}

void solve() {

  sort(V.begin(), V.end());
  vector<data>::iterator ite =
      upper_bound(V.begin(), V.end(), data(sqrt(p), 1.0));

  cout << ite->a << "/" << ite->b << " ";
  while (V.begin() < ite) {
    if (ite->getValue() < sqrt((double)p)) {
      cout << ite->a << "/" << ite->b << endl;
      break;
    }
    ite--;
  }
}

int main() {

  while (cin >> p >> n && p + n) {
    init();
    make();
    solve();
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int MAX = 10010;

struct P {
  int a, b;
};

void check(int a, int b, P &pr, P &pl, double sq) {
  if ((double)a / b < sq && (double)a / b > (double)pl.a / pl.b) {
    pl.a = a;
    pl.b = b;
  }
  if ((double)a / b > sq && (double)a / b < (double)pr.a / pr.b) {
    pr.a = a;
    pr.b = b;
  }
}

int main() {
  int p, n;
  while (cin >> p >> n, p || n) {
    double sq = sqrt(p);
    P pl, pr;
    pl.a = pl.b = 1;
    pr.a = MAX;
    pr.b = 1;

    for (int i = 1; i <= n; i++) {
      int l, r, m;
      l = 1;
      r = n;
      while (l + 1 < r) {
        m = (l + r) / 2;
        if ((double)m / i < sq)
          l = m;
        else
          r = m;
      }
      check(l, i, pr, pl, sq);
      check(m, i, pr, pl, sq);
      check(r, i, pr, pl, sq);
    }
    cout << pr.a << '/' << pr.b << ' ' << pl.a << '/' << pl.b << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

vector<int> prime;
const int MAX = 10010;

struct P {
  int a, b;
  double val;
  P(int _a, int _b) {
    a = _a;
    b = _b;
    val = (double)a / b;
  }
  P(double _val) { val = _val; }

  bool operator<(const P &p) const { return p.val < val; }

  void out() { cout << a << "/" << b; }
};

void make() {
  bool f[MAX];
  for (int i = 0; i < MAX; i++)
    f[i] = false;
  for (int i = 2; i < MAX; i++) {
    if (f[i])
      continue;
    prime.push_back(i);
    for (int j = i; j < MAX; j += i) {
      f[j] = true;
    }
  }
}

int main() {
  int p, n;

  make();

  while (cin >> p >> n, p || n) {
    vector<P> vec;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < prime.size() && prime[j] <= n; j++) {
        vec.push_back(P(prime[j], i));
        vec.push_back(P(i, prime[j]));
      }
    }

    sort(vec.begin(), vec.end());
    sqrt(1);
    int id = upper_bound(vec.begin(), vec.end(), P(sqrt(p))) - vec.begin();
    vec[id - 1].out();
    cout << ' ';
    vec[id].out();
    cout << endl;
  }
  return 0;
}
Problem p00788 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, x, y, u, v, l, r, mid;
  long long s[10100], p;
  for (int i = 0; i <= 10001; i++)
    s[i] = i * i;

  while (scanf("%lld%d", &p, &n), (p || n)) {
    x = n;
    y = u = v = 1;

    for (int i = 1; i <= n; i++) {
      if (s[n] < p * s[i]) {
        if (s[u] * s[i] < s[n] * s[v]) {
          u = n;
          v = i;
        }
        continue;
      }
      l = 1, r = n + 1;
      while (r - l > 1) {
        mid = (l + r) / 2;
        if (s[mid] < p * s[i])
          l = mid;
        else
          r = mid;
      }
      if (s[u] * s[i] < s[l] * s[v] && s[l] < p * s[i]) {
        u = l;
        v = i;
      }
      if (s[x] * s[i] > s[r] * s[y] && s[r] > p * s[i]) {
        x = r;
        y = i;
      }
    }

    printf("%d/%d %d/%d\\n", x, y, u, v);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#define EPS 1e-10
using namespace std;

int main() {
  int p, n, x, y, u, v;
  double l, r, tmp;

  while (scanf("%d%d", &p, &n), (p || n)) {
    l = r = 0.0;

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == 1 || j == 1 || (i % j && j % i)) {
          tmp = (double)i * i / j / j;
          if (tmp < p && (l < EPS || l + EPS < tmp)) {
            u = i;
            v = j;
            l = tmp;
          }
          if (tmp > p && (r < EPS || tmp + EPS < r)) {
            x = i;
            y = j;
            r = tmp;
          }
        }
      }
    }
    printf("%d/%d %d/%d\\n", x, y, u, v);
  }
}
Problem p00789 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MAX 514

int main() {
  int N;
  int dp[MAX] = {};
  dp[0] = 1;
  for (int i = 1; i <= 17; i++) {
    for (int j = 0; j < MAX - i * i; j++) {
      dp[j + i * i] += dp[j];
    }
  }

  while (cin >> N, N) {
    cout << dp[N] << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct P {
  string a, b, c;
  P() {}
  P(string &a, string &b, string &c) : a(a), b(b), c(c) {}
};

vector<P> rules, ans;
map<string, bool> mp;

bool check(P p) {
  for (int i = 0; i < (int)rules.size(); i++) {
    P rule = rules[i];
    bool flg = false;
    for (int j = 0; j < 8; j++) {
      if ((rule.b[j] != '?' && rule.b[j] != p.a[j]) ||
          (rule.c[j] != '?' && rule.c[j] != p.b[j])) {
        flg = true;
        break;
      }
    }
    if (!flg)
      return mp[rule.a];
  }
  return false;
}

int main() {
  int N, M;
  mp["permit"] = true;
  mp["deny"] = false;
  while (cin >> N >> M, (N | M)) {
    P in;
    rules.clear();
    ans.clear();
    while (N--) {
      cin >> in.a >> in.b >> in.c;
      rules.push_back(in);
    }
    reverse(rules.begin(), rules.end());
    while (M--) {
      cin >> in.a >> in.b >> in.c;
      if (check(in)) {
        ans.push_back(in);
      }
    }
    int s = ans.size();
    cout << s << endl;
    for (int i = 0; i < s; i++) {
      cout << ans[i].a << " " << ans[i].b << " " << ans[i].c << endl;
    }
  }
  return 0;
}
Problem p00789 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp[301][18];
int solve(int amount, int num) {
  // cerr << "solve(" << amount << "," << num << ")" << endl;
  if (amount == 0)
    return 1;
  if (num < 0)
    return 0;
  int &res = dp[amount][num];
  if (res != -1)
    return res;
  res = 0;
  for (int k = num; k >= 1; --k) {
    if (amount - k * k >= 0)
      res += solve(amount - k * k, k);
  }
  return res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    cout << solve(N, 17) << endl;
  }
}

Error: TLE

Fixed submission:

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

int dp[301][18];
int solve(int amount, int num) {
  // cerr << "solve(" << amount << "," << num << ")" << endl;
  if (amount == 0)
    return 1;
  if (num < 0)
    return 0;
  int &res = dp[amount][num];
  if (res != -1)
    return res;
  res = 0;
  for (int k = num; k >= 1; --k) {
    if (amount - k * k >= 0)
      res += solve(amount - k * k, k);
  }
  return res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  while (true) {
    int N;
    cin >> N;
    cout << solve(N, 17) << endl;
  }
}
Problem p00789 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int n;
long long dp[301][18];
int main() {
  while (~scanf("%d", &n), n) {
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 17; j++)
        dp[i][j] = 0;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j * j <= i; j++) {
        for (int k = 0; k <= j; k++)
          dp[i][j] += dp[i - j * j][k];
      }
    }
    long long ret = 0;
    for (int i = 0; i <= 17; i++)
      ret += dp[n][i];
    printf("%lld\\n", ret);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int n, dp[301][18];
int main() {
  while (~scanf("%d", &n), n) {
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 17; j++)
        dp[i][j] = 0;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j * j <= n; j++) {
        for (int k = 0; k <= j; k++)
          dp[i][j] += dp[i - j * j][k];
      }
    }
    long long ret = 0;
    for (int i = 0; i <= 17; i++)
      ret += dp[n][i];
    printf("%lld\\n", ret);
  }
  return 0;
}
Problem p00791 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 1001
using namespace std;
#define int long long
string mp[N];
int h;

int dx[] = {-1, 0, 1};
int calc(int lx, int y) {
  int rx = lx;

  mp[y][lx] = ' ';
  while (rx + 1 < mp[y].size() && mp[y][rx + 1] == '*')
    mp[y][++rx] = ' ';
  int res = rx - lx + 1;
  while (1) {
    if (mp[y][lx + 1] == '*')
      break;
    y++;
    int nrx, nlx;
    for (int i = -1; i <= 1; i++)
      if (rx + i >= 0 && rx + i < mp[y].size() && mp[y][rx + i] == '*')
        nrx = rx + i;

    for (int i = 1; i >= -1; i--)
      if (lx + i >= 0 && lx + i < mp[y].size() && mp[y][lx + i] == '*')
        nlx = lx + i;

    rx = nrx, lx = nlx;
    mp[y][rx] = mp[y][lx] = ' ';
    res += rx - lx + 1;
  }
  for (int i = lx; i <= rx; i++)
    mp[y][i] = ' ';
  return res;
}

signed main() {
  int cnt = 0;
  while (1) {

    cin >> h;
    if (!h)
      break;
    if (cnt++)
      cout << "----------" << endl;
    cin.ignore();
    for (int i = 0; i < h; i++)
      getline(cin, mp[i]);

    map<int, int> ans;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < mp[i].size(); j++)
        if (mp[i][j] == '*')
          ans[calc(j, i)]++;

    for (auto it = ans.begin(); it != ans.end(); it++)
      cout << it->first << " " << it->second << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define N 1001
using namespace std;
#define int long long
string mp[N];
int h;

int dx[] = {-1, 0, 1};
int calc(int lx, int y) {
  int rx = lx;

  mp[y][lx] = ' ';
  while (rx + 1 < mp[y].size() && mp[y][rx + 1] == '*')
    mp[y][++rx] = ' ';
  int res = rx - lx + 1;
  while (1) {
    if (mp[y][lx + 1] == '*')
      break;
    y++;
    int nrx, nlx;
    for (int i = -1; i <= 1; i++)
      if (rx + i >= 0 && rx + i < mp[y].size() && mp[y][rx + i] == '*')
        nrx = rx + i;

    for (int i = 1; i >= -1; i--)
      if (lx + i >= 0 && rx + i < mp[y].size() && mp[y][lx + i] == '*')
        nlx = lx + i;

    rx = nrx, lx = nlx;
    mp[y][rx] = mp[y][lx] = ' ';
    res += rx - lx + 1;
  }
  for (int i = lx; i <= rx; i++)
    mp[y][i] = ' ';
  return res;
}

signed main() {
  int cnt = 0;
  while (1) {
    if (cnt++)
      cout << "----------" << endl;

    cin >> h;
    if (!h)
      break;
    cin.ignore();
    for (int i = 0; i < h; i++)
      getline(cin, mp[i]);

    map<int, int> ans;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < mp[i].size(); j++)
        if (mp[i][j] == '*')
          ans[calc(j, i)]++;

    for (auto it = ans.begin(); it != ans.end(); it++)
      cout << it->first << " " << it->second << endl;
  }
  return 0;
}
Problem p00795 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

const int INF = 1000000000;
char chs[1001];

int main() {
  bool fi = false;
  string str;
  while (getline(cin, str) && str != "") {
    memset(chs, 0, sizeof(chs));
    string ss;
    string tmp;
    if (fi)
      cout << endl;
    else
      fi = true;
    while (getline(cin, tmp) && tmp != "")
      str += tmp;
    while (getline(cin, tmp) && tmp != "")
      ss += tmp;
    int sz = INF;
    int optHead = 0;
    int optTail = 0;
    int head = 0;
    int tail = -1;
    int cnt = 0;
    while (tail < (int)str.size()) {
      bool ok = true;
      for (int i = 0; i < ss.size(); i++)
        if (chs[ss[i]] <= 0)
          ok = false;
      // ツ個サツ催敖づ個氾債暗債つェツ適ツ静伉づ按づァ
      if (ok) {
        if (tail - head + 1 < sz) {
          optHead = head;
          optTail = tail;
          sz = tail - head + 1;
          cnt = 1;
        } else if (tail - head + 1 == sz)
          cnt++;
        chs[str[head]]--;
        head++;
      } else {
        tail++;
        if (tail < (int)str.size())
          chs[str[tail]]++;
      }
    }
    cout << cnt << endl;
    if (cnt != 0) {
      cout << endl;
      for (int i = optHead; i < optHead + sz; i++) {
        int cn = i - optHead + 1;
        cout << str[i];
        if (cn % 72 == 0 || i == optHead + sz - 1)
          cout << endl;
      }
    }
  }
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

char chs[1001];

int main() {
  bool fi = false;
  string str;
  while (getline(cin, str)) {
    if (fi)
      cout << endl;
    else
      fi = true;
    string tmp;
    while (getline(cin, tmp) && tmp != "")
      str += tmp;
    memset(chs, 0, sizeof(chs));
    string emp;
    string ss;
    while (getline(cin, tmp) && tmp != "")
      ss += tmp;
    int sz = 1000000000;
    int optHead = 0;
    int optTail = 0;
    int head = 0;
    int tail = -1;
    // chs[str[0]]++;
    int cnt = 0;
    while (tail < (int)str.size()) {
      bool ok = true;
      for (int i = 0; i < ss.size(); i++)
        if (chs[ss[i]] <= 0)
          ok = false;
      // ツ個サツ催敖づ個氾債暗債つェツ適ツ静伉づ按づァ
      if (ok) {
        if (tail - head + 1 < sz) {
          optHead = head;
          optTail = tail;
          sz = tail - head + 1;
          cnt = 1;
        } else if (tail - head + 1 == sz)
          cnt++;
        chs[str[head]]--;
        head++;
      } else {
        tail++;
        if (tail < (int)str.size())
          chs[str[tail]]++;
      }
    }
    cout << cnt << endl;
    if (cnt != 0) {
      cout << endl;
      for (int i = optHead; i < optHead + sz; i++) {
        int cn = i - optHead + 1;
        cout << str[i];
        if (i == optHead + sz - 1 || cn % 72 == 0)
          cout << endl;
      }
    }
  }
  return 0;
}
Problem p00802 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef complex<double> P;

double dp[41][41][41][41];
double getA[41][41][41];
int nx[41];
const double INF = 1e100;

double cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

int N, M;

P getpos(double p) {
  double a = 2 * M_PI * p;
  return P(cos(a), sin(a));
}

void init(const vector<P> pos) {
  for (int i = 0; i < N; i++)
    nx[i] = (i + 1) % N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        for (int l = 0; l <= M; l++)
          dp[i][j][k][l] = -1.0;

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        getA[i][j][k] = cross(pos[j] - pos[i], pos[k] - pos[i]) / 2.0;
}

double solve(int a, int b, int c, int m) {
  if (abs(dp[a][b][c][m] + 1.0) > 0.5)
    return dp[a][b][c][m];
  if (m == M)
    return dp[a][b][c][m] = 0.0;
  if (c == a)
    return dp[a][b][c][m] = -INF;
  int nc = nx[c];
  return dp[a][b][c][m] =
             max(solve(a, c, nc, m + 1) + getA[a][b][c], solve(a, b, nc, m));
}

int main() {
  while (cin >> N >> M && (N || M)) {
    vector<P> pos;
    for (int i = 0; i < N; i++) {
      double p;
      cin >> p;
      pos.push_back(getpos(p));
    }
    init(pos);
    double res = 0.0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        res = max(res, solve(i, j, nx[j], 2));
      }
    }
    printf("%.6lf\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef complex<double> P;

double dp[41][41][41][41];
double getA[41][41][41];
const double INF = 1e100;

double cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

int N, M;

P getpos(double p) {
  double a = 2 * M_PI * p;
  return P(cos(a), sin(a));
}

void init(const vector<P> pos) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        for (int l = 0; l <= M; l++)
          dp[i][j][k][l] = -1.0;

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < N; k++)
        getA[i][j][k] = cross(pos[j] - pos[i], pos[k] - pos[i]) / 2.0;
}

double solve(int a, int b, int c, int m) {
  if (dp[a][b][c][m] > -0.5)
    return dp[a][b][c][m];
  if (m == M)
    return 0.0;
  if (c == a)
    return -INF;
  int nc = (c + 1) % N;
  return dp[a][b][c][m] =
             max(solve(a, c, nc, m + 1) + getA[a][b][c], solve(a, b, nc, m));
}

int main() {
  while (cin >> N >> M && (N || M)) {
    vector<P> pos;
    for (int i = 0; i < N; i++) {
      double p;
      cin >> p;
      pos.push_back(getpos(p));
    }
    init(pos);
    double res = 0.0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        res = max(res, solve(i, j, j + 1, 2));
      }
    }
    printf("%.6lf\\n", res);
  }
}
Problem p00802 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const double PI = 6 * asin(0.5);

double heron(double a, double b, double c) {
  double s = (a + b + c) / 2;
  return sqrt(s * (s - a) * (s - b) * (s - c));
}

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<pair<double, double>> v(n);
    vector<vector<double>> a(n, vector<double>(n, 0.0));
    vector<vector<vector<double>>> dp(
        m + 2, vector<vector<double>>(n, vector<double>(n, 0.0)));

    for (int i = 0; i < n; i++) {
      double d;
      cin >> d;
      v[i].first = cos(d * 2.0 * PI);
      v[i].second = sin(d * 2.0 * PI);

      for (int j = i - 1; j >= 0; j--)
        a[i][j] = a[j][i] = sqrt(pow(v[i].first - v[j].first, 2) +
                                 pow(v[i].second - v[j].second, 2));
    }

    for (int i = 2; i < m; i++) {
      for (int j = 0; j < n - 2; j++) {
        for (int k = j + 1; k < n - 1; k++) {
          for (int l = k + 1; l < n; l++) {
            dp[i + 1][j][l] =
                max(dp[i + 1][j][l],
                    dp[i][j][k] + heron(a[j][k], a[k][l], a[l][j]));
          }
        }
      }
    }

    double ans = 0.0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        ans = max(ans, dp[m][i][j]);
      }
    }

    printf("%6f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define Pi 6 * asin(0.5)
using namespace std;

class Point {
public:
  double x, y, a;
  Point(double x = 0, double y = 0, double a = 0) : x(x), y(y), a(a) {}
};

class State {
public:
  char m, i;
  char p[41];
};

int main() {
  while (1) {
    int i, j, k, n, m;
    Point p[40];
    double ans, a;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a;
      p[i].a = a;
      p[i].x = cos(a * 2.0 * Pi);
      p[i].y = sin(a * 2.0 * Pi);
    }

    ans = 0.0;
    for (i = 0; i <= n - m; i++) {
      queue<State> Q;
      State u, v;
      u.i = i;
      u.m = 1;
      u.p[0] = i;
      Q.push(u);

      while (!Q.empty()) {
        u = Q.front();
        Q.pop();

        for (j = u.i + 1; j <= n - m + u.m; j++) {
          v = u;
          v.i = j;

          v.p[v.m] = v.i;
          v.m++;

          if (j == n - m + u.m) {
            for (k = j + 1; k < n; k++) {
              v.p[v.m] = k;
              v.m++;
            }
          }

          if (v.m == m) {
            double ansi = 0.0;
            for (k = 0; k < m; k++)
              ansi += (p[v.p[k]].x - p[v.p[(k + 1) % m]].x) *
                      (p[v.p[k]].y + p[v.p[(k + 1) % m]].y);
            ansi /= 2.0;
            ans = max(ans, ansi);
          } else
            Q.push(v);
        }
      }
    }

    printf("%f\\n", ans);
  }
  return 0;
}
Problem p00806 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

char to_num(char c) {
  if (c <= 'c')
    return '2';
  if (c <= 'f')
    return '3';
  if (c <= 'i')
    return '4';
  if (c <= 'l')
    return '5';
  if (c <= 'o')
    return '6';
  if (c <= 's')
    return '7';
  if (c <= 'v')
    return '8';
  return '9';
}

vector<string> w, v;

void dfs(string &seq, int idx, vector<string> words) {
  int size = seq.size();
  if (idx == size) {
    for (int i = 0; i < (int)words.size(); i++) {
      if (i > 0)
        cout << " ";
      cout << words[i];
    }
    cout << "." << endl;
    return;
  }

  for (int i = 0; i < (int)v.size(); i++) {
    int len = v[i].size();
    if (idx + len <= size) {
      bool ok = 1;
      for (int j = 0; j < len; j++) {
        if (seq[idx + j] != v[i][j]) {
          ok = 0;
          break;
        }
      }
      if (ok) {
        words.push_back(w[i]);
        dfs(seq, idx + len, words);
        words.pop_back();
      }
    }
  }
}

void init() {
  w.clear();
  v.clear();
}

int main() {
  int N;
  while (cin >> N, N) {
    init();
    string s, seq;
    for (int i = 0; i < N; i++) {
      cin >> s;
      string t;
      for (int j = 0; j < (int)s.size(); j++) {
        t += to_num(s[j]);
      }
      w.push_back(s);
      v.push_back(t);
    }
    cin >> seq;
    dfs(seq, 0, vector<string>());
    cout << "--" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

char to_num(char c) {
  if (c <= 'c')
    return '2';
  if (c <= 'f')
    return '3';
  if (c <= 'i')
    return '4';
  if (c <= 'l')
    return '5';
  if (c <= 'o')
    return '6';
  if (c <= 's')
    return '7';
  if (c <= 'v')
    return '8';
  return '9';
}

vector<string> w, v;

void dfs(string &seq, int idx, vector<string> words) {
  int size = seq.size();
  if (idx == size) {
    for (int i = 0; i < (int)words.size(); i++) {
      if (i > 0)
        cout << " ";
      cout << words[i];
    }
    cout << "." << endl;
    return;
  }

  for (int i = 0; i < (int)v.size(); i++) {
    int len = v[i].size();
    if (idx + len <= size) {
      bool ok = 1;
      for (int j = 0; j < len; j++) {
        if (seq[idx + j] != v[i][j]) {
          ok = 0;
          break;
        }
      }
      if (ok) {
        words.push_back(w[i]);
        dfs(seq, idx + len, words);
        words.pop_back();
      }
    }
  }
}

int main() {
  int N;
  while (cin >> N, N) {
    string s, seq;
    for (int i = 0; i < N; i++) {
      cin >> s;
      string t;
      for (int j = 0; j < (int)s.size(); j++) {
        t += to_num(s[j]);
      }
      w.push_back(s);
      v.push_back(t);
    }
    cin >> seq;
    dfs(seq, 0, vector<string>());
    cout << "--" << endl;
  }
  return 0;
}