求助大佬 一个堆上的二维数组出问题了
可能是数组大小问题
int **p;
p=new int*[n+1];
for(int i=0;i<n+1;i++)
{
p[i]=new int[n+1];
p[i][0]=0;
}
调试的时候在上面的for循环出问题。
n为100000时就闪退了,在100-10000内都是正常的。。
下面是程序源码
#include<iostream>
#include<fstream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
void Lcs(char str0[],char str1[],int n,bool flag)
{
int **p;
p=new int*[n+1];
for(int i=0;i<n+1;i++){
p[i]=new int[n+1];
p[i][0]=0;
}
for(int i=0;i<n+1;i++)p[0][i]=0;
//填表
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(str0[i-1]==str1[j-1]){p[i][j]=p[i-1][j-1]+1;}
else {
p[i][j]=max(p[i-1][j],p[i][j-1]);
}
}
}
}
int main()
{
char *str0,*str1;
str0=new char[1000000];
str1=new char[1000000];
srand(time(NULL));
for(int i=0;i<1000000;i++)
{
char c=65+rand()%26;
str0[i]=c;
}
for(int i=0;i<1000000;i++){
char c=65+rand()%26;
str1[i]=c;
}
Lcs(str0,str1,100000,0);
system("pause");
}