how should i do with this program to avoid memory limit exceed...problem 1002
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MWORD 50010
#define MNUMB 102
struct node
{
char *code;
char *word;
};
long n;
char number[MNUMB];
char tmplook[MNUMB];
struct node tofind[MWORD];
char change[30]="22233344115566070778889990";
int mic[MNUMB][MNUMB];
int way[MNUMB][MNUMB];
int sort_function( const void *a, const void *b)
{
return( strcmp( (*(struct node *)a).code , (*(struct node*)b).code ) );
}
void clear(void)
{
long tmp;
memset(mic,0,(long)MNUMB*MNUMB*sizeof(int));
memset(way,0,(long)MNUMB*MNUMB*sizeof(int));
for(tmp=0;tmp<n;tmp++)
{
free(tofind[tmp].word);
free(tofind[tmp].code);
}
}
long lookup(void)
{
long min=0,max=n-1,med,tmp;
while(min<=max)
{
med=(min+max)/2;
tmp=strcmp( tofind[med].code , tmplook );
if(tmp<0) min=med+1;
else if(tmp>0) max=med-1;
else return med;
}
return -1;
}
long search(long i,long j)
{
long xxx,tmp,min,tmp2;
if(mic[i][j]==0)
{
//if string is word
for(tmp=i,xxx=0;tmp<=j;tmp++,xxx++) tmplook[xxx]=number
[tmp];
tmplook[xxx]=0;
xxx=lookup();
if( xxx!=-1) { mic[i][j]=1; way[i][j]=xxx; return 1; }
mic[i][j]=-1;
if(i==j) { return -1; }
//end
//else
min=32767;
for(xxx=i;xxx<j;xxx++)
{
tmp=search(i,xxx);
if(tmp!=-1)
{
tmp2=search(xxx+1,j);
if(tmp2!=-1&&tmp2+tmp<min) { min=tmp+tmp2; way[i]
[j]=xxx; }
}
}
if(min==32767) { mic[i][j]=-1; return -1; }
else { mic[i][j]=min; return mic[i][j]; }
//
}
else if(mic[i][j]==-1) return -1;
return mic[i][j];
}
void answer(long i,long j)
{
long tmp,xxx;
if( mic[i][j]==1 )
{
printf("%s ",tofind[ way[i][j] ].word);
}
else
{
answer(i,way[i][j]);
answer(way[i][j]+1,j);
}
}
int main()
{
long i,len,j,limit;
char str[MNUMB];
while(1)
{
scanf("%s",number);
if(strstr(number,"-1")==NULL)
{
//input word
scanf("%ld",&n);
for(i=0;i<n;i++)
{
scanf("%s",str);
{
len=strlen(str);
tofind[i].word=(char *)malloc((len+1)*sizeof
(char));
tofind[i].code=(char *)malloc((len+1)*sizeof
(char));
for(j=0;j<len;j++) tofind[i].code[j]=change[str
[j]-'a'];
tofind[i].code[j]=0;
strcpy( tofind[i].word , str );
}
}
qsort((void *)tofind, n, sizeof(struct node), sort_function);
//end input word
//process
limit=strlen(number)-1;
search(0,limit);
if(mic[0][limit]!=-1)
{
answer(0,limit);
printf("\n");
}
else printf("No solution.\n");
//end process
//clear
clear();
//end clear
}
else break;
}
return 0;
}
You don't need to store all the words (+)
First of all, maybe some words don't match, don't store them, and for
example if you have a word of length 2 which fits at starting
position 2 all the words that only match there don't need to be
stored. Luck :)
thank you for ur advice ,i got AC (+)
thank you for ur advice i got AC
and i have added u to my icq and msn contact list
do u always play these