Problem p01249 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

int n;
char s[210];
char subs[210];

struct word {
  char str[110];
  int l;
} w[110];
int len[110];

int dp0[2010];
int dp[2010];

void sub(int st, int ed) {
  int i;
  for (i = st; i <= ed; i++)
    subs[i - st] = s[i];
  subs[i - st] = '\\0';
}

int main() {
  int cou = 1;
  while (~scanf("%d", &n) && n) {
    for (int i = 0; i < n; i++) {
      scanf("%s%d", w[i].str, &w[i].l);
      len[i] = strlen(w[i].str);
    }
    scanf("%s", s);
    int len0 = strlen(s);
    int start = 0;
    int v = 0;
    for (int i = 0; i <= len0; i++)
      dp0[i] = -1 * (0x3f3f3f3f);
    dp0[0] = 0;
    for (int i = 1; i <= len0; i++) {
      for (int j = 0; j < n; j++) {
        int ed = i - len[j];
        sub(ed, ed + len[j] - 1);
        if (ed >= 0 && dp0[ed] >= 0 && strcmp(w[j].str, subs) == 0)
          dp0[i] = max(dp0[i], dp0[ed] + w[j].l);
      }
    }
    v = dp0[len0];
    for (int i = 0; i <= v; i++)
      dp[i] = 0x3f3f3f3f;
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= v; j++) {
        if (w[i].l <= j)
          dp[j] = min(dp[j], dp[j - w[i].l] + len[i]);
      }
    }
    printf("Case %d: %d\\n", cou++, dp[v]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define Maxwor 1001000
#define inf 0x3f3f3f3f
char word[1100], text[202];
int len[11], weig, lt;
int dp[3000], dpl[300];
struct trie {
  int ch[Maxwor][100];
  int sz;
  int val[Maxwor];

  void init() {
    memset(ch[0], 0, sizeof(ch[0]));
    sz = 1;
  }

  int getnum(char c) { return c - 'a'; }

  void insert(char *s, int v) {
    int n = strlen(s), i, u = 0, c;
    for (i = 0; i < n; i++) {
      c = getnum(s[i]);
      if (!ch[u][c]) {
        memset(ch[sz], 0, sizeof(ch[0]));
        ch[u][c] = sz;
        val[sz] = 0;
        sz++;
      }
      u = ch[u][c];
    }
    val[u] = v;
  }

  int find(char *t, int l) {
    if (l >= lt)
      return 0;
    if (dpl[l])
      return dpl[l];
    int m = strlen(t), i, u = 0, c;
    for (i = 0; i < m; i++) {
      c = getnum(t[i]);
      if (!ch[u][c])
        break;
      if (val[ch[u][c]] != 0) {
        dpl[l] = max(dpl[l], val[ch[u][c]] + find(&t[i + 1], l + i + 1));
        //	printf("%d	%d	%d\\n",l,i,dp[l]);
      }
      u = ch[u][c];
    }
    return dpl[l];
  }
} trie;
int main() {
  int n, i, j, upbou, c = 1, l;
  while (~scanf("%d", &n)) {
    if (!n)
      break;
    trie.init();
    for (i = 1; i <= 10; i++)
      len[i] = inf;
    for (i = 0; i < n; i++) {
      scanf("%s %d", word, &weig);
      trie.insert(word, weig);
      l = strlen(word);
      len[weig] = min(len[weig], l);
    }
    scanf("%s", text);
    lt = strlen(text);
    memset(dpl, 0, sizeof(dpl));
    upbou = trie.find(text, 0); // 求上界
    // 完全背包
    for (i = 1; i <= upbou; i++)
      dp[i] = inf;
    dp[0] = 0;
    for (i = 1; i <= 10; i++) {
      if (len[i] == inf)
        continue;
      for (j = 0; j <= upbou - i; j++) {
        if (dp[j] < inf) {
          dp[j + i] = min(dp[j + i], dp[j] + len[i]);
        }
      }
    }
    printf("Case %d: %d\\n", c++, dp[upbou]);
  }
  return 0;
}