寻找公共前缀的程序
#include<stdio.h>#include<string.h>
#define MAX 10
typedef struct Trienode
{
int nCount;
struct Trienode* next[MAX];
}TrieNode;
TrieNode Memory[100000];
int allocp = 0;
TrieNode* CreateTrieNode()
{
int i;
TrieNode *p;
p = &Memory[allocp++];
p->nCount = 1;
for (i = 0; i<MAX; i++)
{
p->next[i] = NULL;
}
return p;
}
void InsertTrie(TrieNode *pRoot, char *s)
{
int i, k, h ;
h=-1;
TrieNode*p;
p = pRoot;
if (!p)
{
p = pRoot = CreateTrieNode();
}
i = 0;
while (s[i])
{
k = s[i++] - '0';
p->next[k] = CreateTrieNode();
p = p->next[k];
}
p->nCount = -1;
}
int SearchTrie(TrieNode *pRoot, char *s)
{
int length;
length=strlen(s);
int i,k;
TrieNode *p = pRoot;
for (i = 0; i < length; i++)
{
k = s[i] - '0';
p=p->next[k];
if (p == NULL)
return 0;
if (p->nCount == -1)
return -1;
}
return 1;
}
int main(void)
{
int num, i,j;
scanf("%d",&num);
getchar();
char pnum[num][20];
TrieNode *pRoot[num - 1];
for(i=0;i<num-1;i++)
pRoot[i]=NULL;
while (num != 0)
{
for (i = 0; i < num; i++)
gets(pnum[i]);
for (i = 0; i < num - 1;i++)
InsertTrie(pRoot[i], pnum[i]);
for (i = 0; i < num - 1; i++)
{
j=SearchTrie(pRoot[i],pnum[num-1]);
if (j == -1 || j == 1)
{
printf("NO");
break;
}
}
if (j == 0)
printf("YES");
scanf("%d",&num);
getchar();
}
return 0;
}
这段程序有什么问题?反复检查多次,编译可以通过,但是运行时 ,出现.exe程序停止工作 对话框,是在win7下运行的