标签:
5 aba abb w aba z
2
释放内存与不释放内存的区别:
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Trie
{
int flag;
struct Trie *next[26];
}*temp;
struct Trie *newnode()
{
Trie *p;
int i;
p=new struct Trie;
for(i=0; i<26; i++)
{
p->next[i]=NULL;
}
p->flag=0;
return p;
}
int max=0;
void Insert(struct Trie *root, char *s )
{
int i;
int len=strlen(s);
struct Trie *p; p=root;
for(i=0; i<len; i++)
{
int t=s[i]-‘a‘;
if(p->next[t]==NULL )
{
temp = newnode();
p->next[t]=temp;
}
p=p->next[t];
}
p->flag++;
if(p->flag > max )
{
max = p->flag;
}
}
void Shanchu(struct Trie *p)
{
int i;
for(i=0; i<26; i++)
{
if(p->next[i]!=NULL )
{
Shanchu(p->next[i]); //递归进行内存清理删除
}
}
free(p);
}
int main()
{
int n;
int i;
char s[6];
scanf("%d%*c", &n);
Trie *trie;
trie = newnode();
while(n--)
{
scanf("%s", s);
Insert(trie, s);
}
printf("%d\n", max );
//清理释放内存
Shanchu(trie);
return 0;
}
SDUT OJ 2892 A (字典树问题-输出出现次数最多的字符串的出现次数,60ms卡时间,指针+最后运行完释放内存)
标签:
原文地址:http://www.cnblogs.com/yspworld/p/4239962.html