Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

字典树+DFS,WA得快哭了,我把OJ上所有的测试数据都找来了

Posted by xz816111 at 2014-10-29 22:15:16 on Problem 1002
下面的代码是交给UVA上的,格式和这有点不同,但是算法是一样的。
我把所有的测试数据跑了一遍,大致看了下没找到错误,uva上跑了500毫秒左右WA掉,跪求错误数据或BUG

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct	trie
{
	struct	trie	* next[10];
	int		count;
};
struct	trie	* ROOT = NULL;
char		BOX[50];
int		FLAG;

void	insert(char * s);
void	dfs(struct trie * temp,int len);
int	main(void)
{
	//freopen("txt.txt","r",stdin);
	int	t,n,len;
	char	s[50],number[50];

	scanf("%d",&t);
	while(t --)
	{
		FLAG = 1;
		ROOT = (struct trie *)malloc(sizeof(struct trie));
		for(int i = 0;i < 10;i ++)
			ROOT -> next[i] = NULL;
		ROOT -> count = 0;

		scanf("%d",&n);
		while(n --)
		{
			len = 0;
			
			scanf("%s",s);
			for(int i = 0;s[i];i ++)					//转化成纯数字形式插入字典树
			{
				if(s[i] >= '0' && s[i] <= '9')
					number[len ++] = s[i];
				else	if(s[i] == 'A' || s[i] == 'B' || s[i] == 'C')
					number[len ++] = '2';
				else	if(s[i] == 'D' || s[i] == 'E' || s[i] == 'F')
					number[len ++] = '3';
				else	if(s[i] == 'G' || s[i] == 'H' || s[i] == 'I')
					number[len ++] = '4';
				else	if(s[i] == 'J' || s[i] == 'K' || s[i] == 'L')
					number[len ++] = '5';
				else	if(s[i] == 'M' || s[i] == 'N' || s[i] == 'O')
					number[len ++] = '6';
				else	if(s[i] == 'P' || s[i] == 'R' || s[i] == 'S')
					number[len ++] = '7';
				else	if(s[i] == 'T' || s[i] == 'U' || s[i] == 'V')
					number[len ++] = '8';
				else	if(s[i] == 'W' || s[i] == 'X' || s[i] == 'Y')
					number[len ++] = '9';
			}
			number[len] = '\0';
			insert(number);
		}
		dfs(ROOT,0);
		if(FLAG)
			puts("No duplicates.");
		if(t)
			puts("");
	}

	return	0;
}

void	insert(char * s)				//字典树插入函数
{
	struct	trie	* temp = ROOT;

	for(int i = 0;s[i];i ++)
		if(temp -> next[s[i] - '0'])
			temp = temp -> next[s[i] - '0'];
		else
		{
			temp -> next[s[i] - '0'] = (struct trie *)malloc(sizeof(struct trie));
			for(int j = 0;j < 10;j ++)
				temp -> next[s[i] - '0'] -> next[j] = NULL;
			temp -> next[s[i] - '0'] -> count = 0;

			temp = temp -> next[s[i] - '0'];
		}
	temp -> count ++;

	return	;
}

void	dfs(struct trie * temp,int len)			//深搜
{
	for(int i = 0;i < 10;i ++)
		if(temp -> next[i])
		{
			BOX[len] = i + '0';
			len ++;
			if(temp -> next[i] -> count >= 2)
			{
				FLAG = 0;
				BOX[len] = '\0';
				for(int j = 0;BOX[j];j ++)
				{
					if(j == 3)
						putchar('-');
					printf("%c",BOX[j]);
				}
				printf(" %d\n",temp -> next[i] -> count);
				continue;
			}
			dfs(temp -> next[i],len);
			len --;
		}

	return	;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator