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

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

int main() {
  int N;
  cin >> N;

  int p[5000];
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }

  int min_sum = 20000;

  for (int I = 0; I <= p[0]; I++) {
    int q[5000];
    for (int i = 0; i < N; i++) {
      q[i] = p[i];
    }

    int sum = I;
    q[0] -= I;

    for (int i = 0; i <= N - 2; i++) {
      if (q[i] <= 0)
        continue;

      sum += q[i] * 2;
      q[i + 1] -= q[i];
    }

    if (q[N - 1] > 0) {
      sum += q[N - 1];
    }

    min_sum = min(min_sum, sum);
  }

  cout << min_sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;

  int p[5000];
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }

  int turn = 0;

  for (int i = 0; i < N; i++) {
    while (p[i] != 0) {
      if (i == 0) {
        if (p[1] != 0) {
          p[0]--;
          p[1]--;
          turn += 2;
        } else {
          p[0]--;
          turn++;
        }
      } else if (i == N - 1) {
        if (p[N - 2] != 0) {
          p[N - 1]--;
          p[N - 2]--;
          turn += 2;
        } else {
          p[N - 1]--;
          turn++;
        }
      } else {
        p[i]--;
        p[i + 1]--;
        turn += 2;
      }
    }
  }

  cout << turn << endl;

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

#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int a, s[5001], d, f, g, h[5001], i, j;
  f = 0;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &s[i]);
    h[i] = s[i];
  }
  d = 4;
  while (1) {
    d--;
    if (d == 0)
      break;
    for (i = 1; i < a - 1; i++) {
      if (s[i] == d) {
        if (i == 1) {
          if (s[i + 1] > 0) {
            s[i]--;
            s[i + 1]--;
            f += 2;
          } else {
            s[i]--;
            s[i - 1]--;
            f += 2;
          }
        } else if (i == a - 2) {
          if (s[i - 1] > 0) {
            s[i]--;
            s[i - 1]--;
            f += 2;
          } else {
            s[i]--;
            s[i + 1]--;
            f += 2;
          }
        } else {
          if (s[i - 1] > s[i + 1]) {
            s[i]--;
            s[i - 1]--;
            f += 2;
          } else {
            s[i]--;
            s[i + 1]--;
            f += 2;
          }
        }
      }
    }
  }
  if (s[0] > 0)
    f += s[0];
  if (s[a - 1] > 0)
    f += s[a - 1];
  g = 0;
  d = 4;
  for (i = 0; i < a; i++)
    s[i] = h[i];
  while (1) {
    d--;
    if (d == 0)
      break;
    for (i = a - 2; i > 0; i--) {
      if (s[i] == d) {
        if (i == 1) {
          if (s[i + 1] > 0) {
            s[i]--;
            s[i + 1]--;
            g += 2;
          } else {
            s[i]--;
            s[i - 1]--;
            g += 2;
          }
        } else if (i == a - 2) {
          if (s[i - 1] > 0) {
            s[i]--;
            s[i - 1]--;
            g += 2;
          } else {
            s[i]--;
            s[i + 1]--;
            g += 2;
          }
        } else {
          if (s[i - 1] > s[i + 1]) {
            s[i]--;
            s[i - 1]--;
            g += 2;
          } else {
            s[i]--;
            s[i + 1]--;
            g += 2;
          }
        }
      }
    }
  }
  if (s[0] > 0)
    g += s[0];
  if (s[a - 1] > 0)
    g += s[a - 1];
  if (g < f)
    f = g;
  printf("%d\\n", f);
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int a, s[5001], d, f, g, h, j, i, k;
  scanf("%d", &a);
  h = 0;
  for (i = 0; i < a; i++) {
    scanf("%d", &s[i]);
    h += s[i];
  }
  while (1) {
    d = -100000;
    g = 0;
    for (i = 1; i < a - 1; i++) {
      if (d < s[i]) {
        d = s[i];
        f = i;
      }
    }
    for (i = 0; i < a; i++)
      if (s[i] <= 0)
        g++;
    // printf("%d\\n",g);
    if (g == a)
      break;
    if (f == 1) {
      if (s[2] > 0) {
        s[1]--;
        s[2]--;
      } else {
        s[0]--;
        s[1]--;
      }
    } else if (f == a - 1) {
      if (s[a - 2] > 0) {
        s[a - 1]--;
        s[a - 2]--;
      } else {
        s[a - 1]--;
        s[a]--;
      }
    } else {
      if (f - 1 < f + 1) {
        s[f + 1]--;
        s[f]--;
      } else if (f - 1 >= f + 1) {
        s[f - 1]--;
        s[f]--;
      }
    }
  }
  for (i = 0; i < a; i++)
    h += s[i] * -1;
  printf("%d\\n", h);
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int a[5005];
  int n;
  int i, j, k;
  int s, b, c;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  s = 0, b = 0, c = 0;
  for (i = 3; i > 0; i--) {
    for (j = 0; j < n; j++) {
      if (a[j] == i) {
        if (c == 0)
          b = j;
        c++;
      } else if (c != 0) {
        if (b == 0 || c % 2 == 0)
          s += c;
        else {
          if (a[j] > a[b - 1])
            a[j] = (a[j] > 0 ? a[j] - 1 : 0);
          else
            a[b - 1] = (a[b - 1] > 0 ? a[b - 1] - 1 : 0);
          s += c + 1;
        }
        for (k = b; k < b + c; k++) {
          a[k]--;
        }
        b = 0, c = 0;
      }
    }
    s += c;
    for (j = b; j < b + c; j++) {
      a[j]--;
    }
    b = 0, c = 0;
  }
  cout << s << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a[5005];
  int n;
  int i, j, k;
  int s, b, c;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    s = 0, b = 0, c = 0;
    for (i = 3; i > 0; i--) {
      for (j = 0; j < n; j++) {
        if (a[j] == i) {
          if (c == 0)
            b = j;
          c++;
        } else if (c != 0) {
          if (b == 0 || c % 2 == 0)
            s += c;
          else {
            if (a[j] > a[b - 1])
              a[j] = (a[j] > 0 ? a[j] - 1 : 0);
            else
              a[b - 1] = (a[b - 1] > 0 ? a[b - 1] - 1 : 0);
            s += c + 1;
          }
          for (k = b; k < b + c; k++) {
            a[k]--;
          }
          b = 0, c = 0;
        }
      }
      s += c;
      for (j = b; j < b + c; j++) {
        a[j]--;
      }
      b = 0, c = 0;
    }
    cout << s << endl;
  }
  return 0;
}
Problem p00335 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int INF = 2000000005;
int pk[5005] = {0}, pk2[5005] = {0};

int main() {

  int n;
  cin >> n;
  int sm = 0, sm2 = 0;
  for (int i = 0; i < n; i++) {
    cin >> pk[i];
    pk2[i] = pk[i];
    sm += pk[i];
    sm2 += pk[i];
  }
  for (int i = 1; i < n - 2; i++) {
    int m = min(pk[i], pk[i + 1]);
    pk[i] -= m;
    pk[i + 1] -= m;
  }
  int m2 = min(pk[0], pk[1]);
  pk[0] = 0;
  pk[1] -= m2;
  int m3 = min(pk[n - 2], pk[n - 1]);
  pk[n - 2] -= m3;
  pk[n - 1] = 0;
  // ここまでで合計分の削除が完了
  for (int i = 0; i < n; i++) {
    /*cout << pk[i] << " ";*/
    sm += pk[i];
  }
  /*cout << endl << sm << endl;*/
  // 今度は逆から
  for (int i = n - 2; i > 1; i--) {
    int m = min(pk2[i], pk2[i - 1]);
    pk2[i] -= m;
    pk2[i - 1] -= m;
  }
  int m4 = min(pk2[0], pk2[1]);
  pk2[0] = 0;
  pk2[1] -= m4;
  int m5 = min(pk2[n - 2], pk2[n - 1]);
  pk2[n - 2] -= m5;
  pk2[n - 1] = 0;
  // ここまでで合計分の削除が完了
  for (int i = 0; i < n; i++) {
    /*cout << pk2[i] << " ";*/
    sm2 += pk2[i];
  }
  /*cout << endl << sm2 << endl;*/
  cout << min(sm, sm2) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int INF = 2000000005;
int pk[5005] = {0}, cnter[5005] = {0};

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

    for (int i = 0; i < n; i++) {
      cin >> pk[i];
    }
    if (pk[0] == 0) {
    } else if (pk[0] <= pk[1]) {
      cnter[0] = pk[0];
      cnter[1] = pk[0];
    } else {
      cnter[0] = pk[0];
      cnter[1] = pk[1];
    }
    for (int i = 1; i < n; i++) {
      while (cnter[i] < pk[i]) {
        cnter[i]++;
        cnter[i + 1]++;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      /*cout << cnter[i] << " ";*/
      ans += cnter[i];
    }
    cout << /*endl << */ ans << endl;
  }
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[1001][1001];
int main() {
  string t, b;
  cin >> t >> b;
  dp[0][0] = 0;
  int MOD = 1000000007;
  for (int i = 1; i <= b.size(); i++) {
    for (int j = 1 + i - 1; j <= t.size(); j++) {

      if (b[i - 1] == t[j - 1]) {
        if (i == 1)
          dp[i][j] = (dp[i][j - 1] + 1) % MOD;
        else
          dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % MOD;
      } else
        dp[i][j] = dp[i][j - 1] % MOD;
    }
  }
  cout << dp[b.size()][t.size()] % MOD << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[1000][1000];
int main() {
  string t, b;
  cin >> t >> b;
  dp[0][0] = 0;
  int MOD = 1000000007;
  for (int i = 1; i <= b.size(); i++) {
    for (int j = 1 + i - 1; j <= t.size(); j++) {

      if (b[i - 1] == t[j - 1]) {
        if (i == 1)
          dp[i][j] = (dp[i][j - 1] + 1) % MOD;
        else
          dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % MOD;
      } else
        dp[i][j] = dp[i][j - 1] % MOD;
    }
  }
  cout << dp[b.size()][t.size()] % MOD << endl;
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  int A, B;
  string a, b;
  cin >> a >> b;
  A = a.size();
  B = b.size();

  vector<vector<ll>> dp(B + 1, vector<ll>(A + 1));
  constexpr ll MOD = ((1e9) + 7);

  for (int i = 0; i < B; i++) {
    for (int j = 0; j < A; j++) {
      if (i > 0)
        dp[i][j + 1] += dp[i][j];
      if (b[i] == a[j]) {
        dp[i + 1][j + 1] += (dp[i][j] + (i == 0));
        dp[i + 1][j + 1] %= MOD;
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= A; i++) {
    res += dp[B][i];
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  int A, B;
  string a, b;
  cin >> a >> b;
  A = a.size();
  B = b.size();

  vector<vector<ll>> dp(B, vector<ll>(A));
  constexpr ll MOD = ((1e9) + 7);

  for (int i = 0; i < B; i++) {
    for (int j = 0; j < A; j++) {
      if (i > 0)
        dp[i][j + 1] += dp[i][j];
      if (b[i] == a[j]) {
        dp[i + 1][j + 1] += (dp[i][j] + (i == 0));
        dp[i + 1][j + 1] %= MOD;
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= A; i++) {
    res += dp[B][i];
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;

int main() {

  char T[1001], P[1001];
  int t_length, p_length;
  vector<int> V[26];

  scanf("%s", T);
  scanf("%s", P);

  for (t_length = 0; T[t_length] != '\\0'; t_length++)
    ;
  for (p_length = 0; P[p_length] != '\\0'; p_length++) {
    V[P[p_length] - 'a'].push_back(p_length + 1);
  }

  int **dp = new int *[t_length];
  for (int i = 0; i < t_length; i++) {
    dp[i] = new int[p_length + 1];
    for (int k = 0; k < p_length + 1; k++)
      dp[i][k] = 0;
  }

  dp[0][0] = 1;
  if (T[0] == P[0]) {
    dp[0][1] = 1;
  }

  for (int i = 1; i < t_length; i++) {
    for (int k = 0; k < p_length + 1; k++) {
      dp[i][k] = dp[i - 1][k] % 1000000007;
    }
    for (int k = 0; k < V[T[i] - 'a'].size(); k++) {
      dp[i][V[T[i] - 'a'][k]] += dp[i - 1][V[T[i] - 'a'][k] - 1] % 1000000007;
    }
  }

  printf("%d\\n", dp[t_length - 1][p_length] % 1000000007);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;

int main() {

  char T[1001], P[1001], t_length, p_length;
  vector<int> V[26];

  scanf("%s", T);
  scanf("%s", P);

  for (t_length = 0; T[t_length] != '\\0'; t_length++)
    ;
  for (p_length = 0; P[p_length] != '\\0'; p_length++) {
    V[P[p_length] - 'a'].push_back(p_length + 1);
  }

  int **dp = new int *[t_length];
  for (int i = 0; i < t_length; i++) {
    dp[i] = new int[p_length + 1];
    for (int k = 0; k < p_length + 1; k++)
      dp[i][k] = 0;
  }

  dp[0][0] = 1;
  if (T[0] == P[0]) {
    dp[0][1] = 1;
  }

  for (int i = 1; i < t_length; i++) {
    for (int k = 0; k < p_length + 1; k++) {
      dp[i][k] = dp[i - 1][k] % 1000000007;
    }
    for (int k = 0; k < V[T[i] - 'a'].size(); k++) {
      dp[i][V[T[i] - 'a'][k]] += dp[i - 1][V[T[i] - 'a'][k] - 1] % 1000000007;
    }
  }

  printf("%d\\n", dp[t_length - 1][p_length]);

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

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

string t, b;
long long dp[1001][1001];

int main() {
  cin >> t;
  cin >> b;
  t = "a" + t;
  b = "a" + b;

  int h = b.size();
  int w = t.size();
  for (int i = 0; i < w; i++) {
    dp[0][i] = 1;
  }
  for (int i = 1; i < h; i++) {
    dp[i][0] = 0;
  }

  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      if (t[j] == b[i]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = 0;
      }
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= 1000000007;
    }
  }
  cout << dp[h - 1][w - 1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define DIV 1000000007

string t, b;
int c;
bool dp[1024][1024];

void rec(int x, int y) {
  if (x >= t.size() || y >= b.size()) {
    return;
  }
  if (dp[x][y]) {
    rec(x + 1, y);
    if (y + 1 == b.size()) {
      c++;
      if (c == DIV) {
        c = 0;
      }
    } else {
      rec(x + 1, y + 1);
    }
  } else {
    rec(x + 1, y);
  }
}

int main() {
  cin >> t >> b;
  c = 0;
  for (int i = 0; i < t.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      dp[i][j] = t[i] == b[j] ? true : false;
    }
  }
  rec(0, 0);
  cout << c << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
#define MOD 1000000007;
using namespace std;

string b, t;
long long dp[1005][1005] = {0};

int main() {
  cin >> t >> b;
  int bs = b.size();
  int ts = t.size();
  for (int j = 0; j < t.size(); j++) {
    if (j > 0)
      dp[0][j] += dp[0][j - 1];
    if (b[0] == t[j])
      dp[0][j]++;
  }
  for (int i = 1; i < b.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      dp[i][j] = dp[i][j - 1];
      if (b[i] == t[j]) {
        dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
      }
      dp[i][j] %= MOD;
    }
  }
  cout << dp[bs - 1][ts - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

string b, j;
long long res = 0;

void dfs(int now, int d) {
  if (d == j.size()) {
    res++;
    res %= 1000000007;
    return;
  }
  if (now == b.size())
    return;
  if (b[now] == j[d])
    dfs(now + 1, d + 1);
  dfs(now + 1, d);
  return;
}

int main() {
  cin >> b >> j;
  dfs(0, 0);
  cout << res << endl;
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Runtime Error

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

long long int dp[1002][1002];

int main() {

  string t, b;

  cin >> t >> b;

  int tsize = t.size(), bsize = b.size();

  dp[0][0] = 1;

  for (int i = 0; i <= t.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (t[i] == b[j]) {
        dp[i + 1][j + 1] += dp[i][j];
        dp[i + 1][j + 1] %= 1000000007;
      }
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= 1000000007;
    }
  }

  cout << dp[tsize][bsize] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

long long int dp[1001][1001];

int main() {

  string t, b;

  cin >> t >> b;

  int tsize = t.size(), bsize = b.size();

  dp[0][0] = 1;

  for (int i = 0; i <= t.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (t[i] == b[j]) {
        dp[i + 1][j + 1] += dp[i][j];
        dp[i + 1][j + 1] %= 1000000007;
      }
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= 1000000007;
    }
  }

  cout << dp[tsize][bsize] << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9
ll mod = 1e9 + 7;

int main() {
  string t, b;
  cin >> t >> b;
  ll n1 = t.size(), n2 = b.size();
  vector<vector<ll>> dp(n1, vector<ll>(n2, 0));
  for (int i = 0; i < n1; i++) {
    if (t[i] == b[0])
      dp[i][0] = 1;
  }
  if (n1 < n2) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i < n2; i++) {
    ll sum = 0;
    for (int j = 0; j < i; j++) {
      sum += dp[j][i - 1];
      sum %= mod;
    }
    for (int j = i; j < n1; j++) {
      if (t[j] == b[i]) {
        dp[j][i] = sum;
      }
      sum += dp[j][i - 1];
      sum %= mod;
    }
  }
  ll res = 0;
  /*for(int i=0;i<n2;i++){
          for(int j=0;j<n1;j++){
                  cout<<dp[i][j]<<' ';
          }
          cout<<endl;
  }*/
  for (int i = 0; i < n1; i++) {
    res += dp[i][n2 - 1];
    res %= mod;
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9
ll mod = 1e9 + 7;

int main() {
  string t, b;
  cin >> t >> b;
  ll n1 = t.size(), n2 = b.size();
  vector<vector<ll>> dp(n1, vector<ll>(n2, 0));
  for (int i = 0; i < n1; i++) {
    if (t[i] == b[0])
      dp[i][0] = 1;
  }

  for (int i = 1; i < n2; i++) {
    ll sum = 0;
    for (int j = 0; j < i; j++) {
      sum += dp[j][i - 1];
      sum %= mod;
    }
    for (int j = i; j < n1; j++) {
      if (t[j] == b[i]) {
        dp[j][i] = sum;
      }
      sum += dp[j][i - 1];
      sum %= mod;
    }
  }
  ll res = 0;
  /*for(int i=0;i<n2;i++){
          for(int j=0;j<n1;j++){
                  cout<<dp[i][j]<<' ';
          }
          cout<<endl;
  }*/
  for (int i = 0; i < n1; i++) {
    res += dp[i][n2 - 1];
    res %= mod;
  }
  cout << res << endl;
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define bigone t.size() + 1
#define bangone b.size() + 1
#define big t.size()
#define bang b.size()
#define USA 1000000007

using namespace std;

long long P[10000][10000];

int main() {
  string t, b;
  cin >> t >> b;
  // P=プロフェッショナル

  for (int i = 0; i < bang + 1; i++) {
    P[0][i] = 0;
  }

  for (int i = 0; i < big + 1; i++) {
    P[i][0] = 1;
  }

  for (int i = 1; i < big + 1; i++) {
    char ti = t.at(i - 1);
    for (int j = 1; j < bang + 1; j++) {
      char bj = b.at(j - 1);
      if (j <= i) {
        if (ti == bj) {
          P[i][j] = (P[i - 1][j - 1] + P[i - 1][j]) % USA;
        } else {
          P[i][j] = P[i - 1][j];
        }
      } else {
        P[i][j] = 0;
      }
    }
  }
  cout << P[big][bang] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define bigone t.size() + 1
#define bangone b.size() + 1
#define big t.size()
#define bang b.size()
#define USA 1000000007

using namespace std;

int P[1000][1000];

int main() {
  string t, b;
  cin >> t >> b;
  // P=プロフェッショナル

  for (int i = 0; i < bang + 1; i++) {
    P[0][i] = 0;
  }

  for (int i = 0; i < big + 1; i++) {
    P[i][0] = 1;
  }

  for (int i = 1; i < big + 1; i++) {
    char ti = t.at(i - 1);
    for (int j = 1; j < bang + 1; j++) {
      char bj = b.at(j - 1);
      if (j <= i) {
        if (ti == bj) {
          P[i][j] = (P[i - 1][j - 1] + P[i - 1][j]) % USA;
        } else {
          P[i][j] = P[i - 1][j];
        }
      } else {
        P[i][j] = 0;
      }
    }
  }
  cout << P[big][bang] << endl;
}
Problem p00336 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
#define mod 1000000007

typedef long long ll;

int main(void) {
  int n, i, j, k, m;
  char t[1200], b[1200];
  scanf("%s", t);
  scanf("%s", b);
  n = strlen(t), m = strlen(b);
  ll dp[m][n], sum[n];
  for (i = 0; i < m; ++i)
    for (j = 0; j < n; ++j)
      dp[i][j] = 0;
  for (i = 0; i < n; ++i)
    if (t[i] == b[0])
      dp[0][i] = 1;
  sum[0] = dp[0][0];
  for (i = 1; i < n; ++i)
    sum[i] = sum[i - 1] + dp[0][i];
  for (i = 1; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      if (b[i] == t[j]) {
        if (j)
          dp[i][j] = sum[j - 1];
        else
          dp[i][j] = 0;
      }
    }
    sum[0] = dp[i][0];
    for (j = 1; j < n; ++j) {
      sum[j] = sum[j - 1] + dp[i][j];
      sum[j] %= mod;
    }
  }
  ll ans = 0;
  for (i = 0; i < n; ++i) {
    ans += dp[m - 1][i];
    ans %= mod;
  }
  /*for(i = 0; i < m; ++i) {
    for(j = 0; j < n;++j) printf("%d ", dp[i][j]);
    printf("\\n");
  }*/
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define mod 1000000007

typedef long long ll;

int main(void) {
  int n, i, j, k, m;
  char t[1200], b[1200];
  scanf("%s", t);
  scanf("%s", b);
  n = strlen(t), m = strlen(b);
  ll dp[m][n];
  for (i = 0; i < m; ++i)
    for (j = 0; j < n; ++j)
      dp[i][j] = 0;
  for (i = 0; i < n; ++i)
    if (t[i] == b[0])
      dp[0][i] = 1;
  for (i = 1; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      if (b[i] == t[j])
        for (k = 0; k < j; ++k) {
          dp[i][j] += dp[i - 1][k];
          dp[i][j] %= mod;
        }
    }
  }
  ll ans = 0;
  for (i = 0; i < n; ++i) {
    ans += dp[m - 1][i];
    ans %= mod;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p00336 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0341
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2392336#1
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
#include <string>
using namespace std;

const int MAX_N = 10000;
const long long mod = 1000000007;
string t, b;

int main() { // dp
  cin >> t >> b;
  int dp[b.size() + 1];
  fill(dp, dp + b.size() + 1, 0);
  dp[0]++;

  for (int i = 0; i < t.size(); i++) { // b???j?????????????????°?????°
    for (int j = b.size() - 1; j >= 0; j--) {
      if (t[i] == b[j]) {
        dp[j + 1] += dp[j];
        dp[j + 1] %= mod;
      }
    }
  }

  cout << dp[b.size()] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
#include <string>
using namespace std;

const int MAX_N = 10000;
const long long mod = 1000000007;
int k[10000];
long long c = 0;
string t, b;
stack<char> st;

int main() {
  cin >> t >> b;

  for (int i = 0; i < t.length(); i++) {
    if (t[i] == b[st.size()]) {
      k[st.size()] = i;
      st.push(b[st.size()]);
    }
    if (st.size() == b.length()) {
      c = (c + 1) % mod;
      i = k[st.size() - 1];
      st.pop();
    }
    if (i == t.length() - 1) {
      if (st.size() == 0)
        break;
      i = k[st.size() - 1];
      st.pop();
    }
  }

  cout << c << endl;
  return 0;
}