| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
POJ 上TLE。。。。HDU上过了。。。为神马。。。#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator