I am solving the well known Edit Distance Dynamic Programing Problem.Actually the problem is given two strings string1 and string2 and given the cost for deletion,insertion and replacement of the character,I have to convert string1 to string2 in minimum cost.For DP I have to use a two dimensional array.For a small string (size<=10000) my code is working but for a larger input(size>=100000) the compiler says "array size is too large". If the problem has to be solved using dynamic programing(for input size=100000) then please tell me how should I handle this error.Here is my code.
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <algorithm>
#include <map>
#include <queue>
#include <iomanip>
#include <string>
#include <math.h>
#include <limits>
#include <map>
#include <float.h>
#include <limits.h>
#include <string.h>
using namespace std;
#define rep(i,a,N) for(int i=a;i<N;++i)
int DP[10000][10000],delete_cost,add_cost,replace_cost;
string first,second;
int Min(int x,int y,int z){
int min=x<y?x:y;
min=min<z?min:z;
return min;
}
int Transform(int i,int j){
if(DP[i][j]!=-1){
//printf("DP is set\n");
return DP[i][j];
}
if(i==first.size())
return (second.size()-j)*add_cost;
if(j==second.size())
return (first.size()-i)*delete_cost;
if(first.at(i)!=second.at(j)){
int add,del,rep;
add=Transform(i,j+1)+add_cost;
del=Transform(i+1,j)+delete_cost;
rep=Transform(i+1,j+1)+replace_cost;
return DP[i][j]=Min(add,del,rep);
}
else
return DP[i][j]=Transform(i+1,j+1);
}
int main(){
int T,a,b,k,ans;
scanf("%d",&T);
while(T--){
memset(DP,-1,sizeof(DP));
cin>>first;
cin>>second;
scanf("%d %d %d",&a,&b,&k);
add_cost=a;
delete_cost=b;
replace_cost=k;
//ans=Transform(0,0);
//if(ans<=k)
printf("%d\n",ans );
//else
// printf("%d\n",-1);
}
return 0;
}