Salve ragazzi, ho un problema con il seguente codice c++.
All'università il prof ci ha proprosto questa implementazione della più lunga sottosequenza comune usando l'allocazione dinamica delle matrici. Lui per il progetto d'esame non vuol vedere malloc e calloc, ma esige l'uso di new e delete per allocare dinamicamente. Ora vi posto sia il codice con calloc e free, sia quello che ho "aggiustato" io usando new e delete(che al momento dell'esecuzione non va bene e non capisco il perchè), sperando che qualcuno di voi risolva questo problema. Grazie in anticipo.
codice:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef int ** MatriceInt;
typedef char ** MatriceChar;

string LCS(string X, string Y, MatriceInt &C, MatriceChar &B) {
	int X_len = X.length() + 1; int Y_len = Y.length() + 1; int i,j;
	string lcs =" ";
	C = (MatriceInt) calloc(X_len, sizeof(int*));       
        // Alloca la matrice per memorizzare la lunghezza  devi vari sottoproblemi LCS
          B = (MatriceChar) calloc(X_len, sizeof(char*));
	for (i =0; i< X_len; i++)
	{ C[i] = (int *)calloc(Y_len, sizeof(int)); B[i] = (char *)calloc(Y_len, sizeof(char));}
	B[0][0] = '\\';
	for (i = 1; i< X_len; i++) {
	  for (j = 1; j< Y_len; j++) {
	           if (X[i-1] == Y[j-1])
		     { C[i][j] = 1 + C[i-1][j-1];  B[i][j] = '\\';}
	              else if(C[i-1][j] >= C[i][j-1])
		                  { C[i][j] = C[i-1][j]; B[i][j] = '|';}
		          else {C[i][j]=C[i][j-1]; B[i][j] = '-';}
		}
	         }
	for(i = 1; i< Y_len; i++)                  // Costruisce la stringa di lunghezza LCS
	{ if(C[X_len-1][i] > C[X_len-1][i-1])  lcs = lcs + Y[i-1];}
	return lcs;
}
int main(){
	string X = "FRANCESCO"; string Y = "FWANCICO";
        int X_len = X.length() + 1; int Y_len = Y.length() + 1; int i,j;
	MatriceInt C = NULL; MatriceChar B = NULL;	
	string lcs = LCS(X, Y, C, B);
        cout << "LCS = " << C[X_len-1][Y_len-1] << lcs <<endl<< endl << "   ";
         // Stampa la matrice C che contiene le soluzioni dei vari sottoproblemi
	for(i = 0; i< Y_len;i++)   cout << " " << Y[i];
	for (i =0; i< X_len; i++){
		cout << endl;
		if (i >= 1)    cout << X[i-1] << " ";
		else            cout << "  ";
		for (j = 0; j< Y_len; j++)   {  cout << C[i][j] << " "; }
	}
	cout << endl << endl << "   ";     
 // Stampa la matrice B che contiene i percorsi  dei vari sottoproblemi	
	for(i = 0; i< Y_len; i++)     cout << " " << Y[i];
	for (i =0; i< X_len; i++){
	cout << endl;
	   if (i >= 1)    cout << X[i-1] << " ";
	    else           cout << "  ";
		for (j = 0; j< Y_len; j++) {cout << B[i][j] << " ";}
	}
	if (C != NULL)  // Delloca le matrici
	{
	 for (i=0;i<X.length();i++)  { free(C[i]); free(B[i]);}
	free(C); free(B);
	}
	cout << endl;
	return 0;
}
codice:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef int ** MatriceInt;
typedef char ** MatriceChar;

string LCS(string X, string Y, MatriceInt &C, MatriceChar &B) {
	int X_len = X.length() + 1; int Y_len = Y.length() + 1; int i,j;
	string lcs =" ";
	C = new int*[X_len];       
        // Alloca la matrice per memorizzare la lunghezza  devi vari sottoproblemi LCS
          B = new char*[X_len];
	for (i =0; i< X_len; i++)
	{ C[i]=new int[Y_len]; B[i]=new char[Y_len];}
	B[0][0] = '\\';
	for (i = 1; i< X_len; i++) {
	  for (j = 1; j< Y_len; j++) {
	           if (X[i-1] == Y[j-1])
		     { C[i][j] = 1 + C[i-1][j-1];  B[i][j] = '\\';}
	              else if(C[i-1][j] >= C[i][j-1])
		                  { C[i][j] = C[i-1][j]; B[i][j] = '|';}
		          else {C[i][j]=C[i][j-1]; B[i][j] = '-';}
		}
	         }
	for(i = 1; i< Y_len; i++)                  // Costruisce la stringa di lunghezza LCS
	{ if(C[X_len-1][i] > C[X_len-1][i-1])  lcs = lcs + Y[i-1];}
	return lcs;
}
int main(){
	string X = "FRANCESCO"; string Y = "FWANCICO";
        int X_len = X.length() + 1; int Y_len = Y.length() + 1; int i,j;
	MatriceInt C = NULL; MatriceChar B = NULL;	
	string lcs = LCS(X, Y, C, B);
        cout << "LCS = " << C[X_len-1][Y_len-1] << lcs <<endl<< endl << "   ";
         // Stampa la matrice C che contiene le soluzioni dei vari sottoproblemi
	for(i = 0; i< Y_len;i++)   cout << " " << Y[i];
	for (i =0; i< X_len; i++){
		cout << endl;
		if (i >= 1)    cout << X[i-1] << " ";
		else            cout << "  ";
		for (j = 0; j< Y_len; j++)   {  cout << C[i][j] << " "; }
	}
	cout << endl << endl << "   ";     
 // Stampa la matrice B che contiene i percorsi  dei vari sottoproblemi	
	for(i = 0; i< Y_len; i++)     cout << " " << Y[i];
	for (i =0; i< X_len; i++){
	cout << endl;
	   if (i >= 1)    cout << X[i-1] << " ";
	    else           cout << "  ";
		for (j = 0; j< Y_len; j++) {cout << B[i][j] << " ";}
	}
	if (C != NULL)  // Delloca le matrici
	{
	 for (i=0;i<X.length();i++)  { delete(C[i]); delete(B[i]);}
	delete(C); delete(B);
	}
	cout << endl;
	return 0;
}