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;

}

你可能感兴趣的:(HDU)