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

POJ 上TLE。。。。HDU上过了。。。为神马。。。

Posted by solo_013 at 2012-08-17 21:13:42 on Problem 3630
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
#define N 10005
int flag,n;
struct tree{
	bool vis;//若该点是尾节点,标记1
	struct tree *next[MAX];
};
tree root;
char ch[15];
void end(tree *root)
{
    for(int i=0;i<10;i++)
    {
        if(root->next[i]!=NULL) end(root->next[i]);
         delete root->next[i];
         root->next[i]=NULL;
     }
}  
void build(char *str){
	int i,j,k,id,len=strlen(str);
	tree *p=&root,*temp;
	for(i=0;i<len;i++){
		id=str[i]-'0';
		if(p->next[id]==NULL){
			temp=(tree *)malloc(sizeof(root));
			for(j=0;j<MAX;j++){
				temp->vis=false;
				temp->next[j]=NULL;
			}
			p->next[id]=temp;
			p=p->next[id];
		}
		else{
			p=p->next[id];
			if(p->vis==true){
				flag=1;
				return;//先前已有比自己短的号码,并且检查到自己的某个位置就是他的结尾,return
			}
		}
	}
	p->vis=true;
	for(i=0;i<MAX;i++)
		if(p->next[i]!=NULL)
		{
			flag=1;
			return ;
		}
}
int main(){
	int i,j,k,t,m;
	scanf("%d",&t);
	getchar();
	while(t--){
		scanf("%d",&n);
		flag=0;
		//getchar();
		while(n--){
			scanf("%s",ch);
			//getchar();
			build(ch);
			if(flag==1)break;
		}
		while(n&&flag){
			scanf("%s",ch);
			n--;
		}
		if(flag==1)
			printf("NO\n");
		else
			printf("YES\n");
		tree *tp=&root;
		end(tp);
	}
	return 0;
}

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