hdu 1671( 动态链表注意释放空间!!!)

/*
	题意:判断字符串之间是否存在完全覆盖
	搞了一下午,一直MLE,动态的表需要释放
	内存空间,弄成静态的就不用了

*/


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct node 
{
	struct node * child[10];
	int num,count;
};
struct node * root ;

bool Insert(char s[])
{
	struct node *cur ;
	cur = root;
	cur ->count = 0;
	cur ->num = 1;
	int len = strlen(s);
	for(int i = 0; i < len; i++)
	{
		if(cur->child[s[i] - '0'] != 0)
		{
			cur->child[s[i] - '0']->num ++;
			cur ->count ++;
			cur = cur->child[s[i] - '0'];
			if(i == len - 1)
				return 1;
		}
		else
		{
			struct node * newnode = new struct node ;
			cur->child[s[i] - '0'] = newnode;
			for(int j = 0; j < 10; j++)
			{
				newnode->child[j] = 0;
			}
			newnode->num = 1;
			newnode->count = 0;
			cur->count += 1;
			if(cur->count != cur -> num)
				return 1;
			cur = newnode;
		}
	}
	return 0;
}

/*****内存释放啊****/
void release(struct node *root)
{
	int i;
	if(root == NULL) return;
	for(i=0;i<10;i++)
	{
		if(root->child[i]!=NULL)
			release(root->child[i]);
	}
	free(root);
	return ;
}

int main()
{
	int n,cas;
	int i,flag;
	char s[11];
	scanf("%d", &cas);
	while(cas--)
	{
	//init
		root = new struct node;
		for( i = 0; i < 10; i++)
		{
			root->child[i] = 0;
		}
		flag=0;
		scanf("%d", &n);
		for( i = 0; i < n; i++)
		{
			scanf("%s",s);
			if(Insert(s)) flag = 1;
		}
		if(flag) printf("NO\n");
		else printf("YES\n");

		/*release*/
		release( root );//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
	return 0;
}

转载于:https://www.cnblogs.com/cykun/archive/2011/01/17/1937549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值