hdu 1671 Phone List

来源:互联网 发布:罗斯体测数据 编辑:程序博客网 时间:2024/05/06 11:09

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1671

 

其实这题可以直接排序过的。。。一开始没有想到,因为在做trie树的专题,导致了思维定式。。

让后用trie树做也出现了很多错误、没有释放内存,算法方面也没有考虑到位。导致错了很多次。

还有在对照打模版时。。。居然直接打成:cur=str[i]-'a',   应该是cur = str[i]-'0';也是一种思维定式。

 

 

#include<iostream>#include<cstring>using namespace std;typedef struct t_node{struct t_node * next[10];bool ch;}t_node,* Node;Node root;bool flag;void insert(char *str){Node index,newnode;int len,i,cur;index=root;//printf("%d ",index->ch);len=strlen(str);for(i=0;i<len;i++){cur=str[i]-'0'; if(index->ch==1||(i==len-1&&index->next[cur]!=NULL)){flag=0;return ;}if(index->next[cur]!=NULL){index=index->next[cur];}else{newnode=(Node)calloc(1,sizeof(t_node));newnode->ch=0;index->next[cur]=newnode;index=newnode;}}index->ch=1;}void release(Node root)            //释放内存空间!!!!!!!!!!!!!{int i;if(root == NULL) return;for(i=0;i<10;i++){if(root->next[i]!=NULL)release(root->next[i]);}free(root);return ;}int main(){int t,n,i;char input[11];scanf("%d",&t);while(t--){scanf("%d",&n);flag=1;root=(Node)calloc(1,sizeof(t_node));root->ch=0;for(i=1;i<=n;i++){scanf("%s",input);if(!flag)continue;insert(input);}if(flag)printf("YES\n");elseprintf("NO\n");release( root );}return 0;}


 

原创粉丝点击