Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*#include<iostream>
#include<algorithm>
using namespace std;
int knapsack(int c, int items, int values[], int w[])
{
        int i = 0;
        int p;
        int dp[4][9];
        for (; i < items; i++)
        {
                for (p = 0; p <= c; p++)
                {
                        if (i == 0 || p == 0)
                                dp[i][p] = 0;
                        else if (w[i - 1] > p)
                                dp[i][p] = max(values[i] + dp[i - 1][p - w[i -
1]], dp[i - 1][p]); else dp[i][p] = dp[i - 1][p];
                }
                cout << "GGGG";
        }
        return dp[c][p];
}
int main()
{
        cout << "GGGG";
        /*map<int, int>m;
        m[10] = 100;
        m[5] = 25;
        m[3] = 9;
        m[4] = 16;
        m.insert(make_pair(10, 700));
        m.insert(make_pair(10, 600));

        for (auto it = m.begin(); it != m.end(); it++)
                cout << it->first << " " << it->second<<endl;*/
/*multimap<int, int>m;
m.insert(make_pair(10, 100));
m.insert(make_pair(20, 200));
m.insert(make_pair(30, 300));
m.insert(make_pair(10, 700));
m.insert(make_pair(20, 100));

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

int values[] = { 30,50,60 };
int w[] = { 3,4,5 };
int tw = 8;
cout << "GGGG";
cout << knapsack(tw, 3, values, w);


}*/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
void lcs(string s, string t, int m, int n) {
  int dp[m + 1][n + 1];
  int i, j;
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  /*for(i=0;i<=m;i++)
  {
      for(j=0;j<=n;j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  //	cout << dp[m][n];
  int index = dp[m][n];
  //	char values[index+1];*/
  i = m;
  j = n;

  vector<char> v;
  //	values[index] = '\0';
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      v.push_back(s[i - 1]);
      // values[index-1]=s[i-1];
      i--;
      j--;
      index--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  for (auto itr = v.end() - 1; itr >= v.begin(); itr--)
    cout << *itr;
  // cout<<values;
}
int main() {
  int m, n;
  string s, t;
  cin >> s >> t;
  m = s.length();
  n = t.length();
  lcs(s, t, m, n);
}

Fixed submission:
/*#include<iostream>
#include<algorithm>
using namespace std;
int knapsack(int c, int items, int values[], int w[])
{
        int i = 0;
        int p;
        int dp[4][9];
        for (; i < items; i++)
        {
                for (p = 0; p <= c; p++)
                {
                        if (i == 0 || p == 0)
                                dp[i][p] = 0;
                        else if (w[i - 1] > p)
                                dp[i][p] = max(values[i] + dp[i - 1][p - w[i -
1]], dp[i - 1][p]); else dp[i][p] = dp[i - 1][p];
                }
                cout << "GGGG";
        }
        return dp[c][p];
}
int main()
{
        cout << "GGGG";
        /*map<int, int>m;
        m[10] = 100;
        m[5] = 25;
        m[3] = 9;
        m[4] = 16;
        m.insert(make_pair(10, 700));
        m.insert(make_pair(10, 600));

        for (auto it = m.begin(); it != m.end(); it++)
                cout << it->first << " " << it->second<<endl;*/
/*multimap<int, int>m;
m.insert(make_pair(10, 100));
m.insert(make_pair(20, 200));
m.insert(make_pair(30, 300));
m.insert(make_pair(10, 700));
m.insert(make_pair(20, 100));

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

int values[] = { 30,50,60 };
int w[] = { 3,4,5 };
int tw = 8;
cout << "GGGG";
cout << knapsack(tw, 3, values, w);


}*/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
void lcs(string s, string t, int m, int n) {
  int dp[m + 1][n + 1];
  int i, j;
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (s[i - 1] == t[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  /*for(i=0;i<=m;i++)
  {
      for(j=0;j<=n;j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  //	cout << dp[m][n];
  int index = dp[m][n];
  //	char values[index+1];*/
  i = m;
  j = n;

  vector<char> v;
  //	values[index] = '\0';
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      v.push_back(s[i - 1]);
      // values[index-1]=s[i-1];
      i--;
      j--;
      index--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  if (v.size() == 0)
    cout << " ";
  else {
    for (auto itr = v.end() - 1; itr >= v.begin(); itr--)
      cout << *itr;
  }
  // cout<<values;
}
int main() {
  int m, n;
  string s, t;
  cin >> s >> t;
  m = s.length();
  n = t.length();
  lcs(s, t, m, n);
}