hdu1671( 字典树)
来源:互联网 发布:windows核心编程怎么样 编辑:程序博客网 时间:2024/05/16 13:55
第一次独立些字典树的代码,虽然还是百度了一下下,但是大致思想都已懂了,这是一道很基础的字典树题,代码不是写的很好
一开始把题目理解错了 ,wa了好多次
代码如下:
#include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<time.h>#include<math.h>#define eps 1e-9#define N 15#define pi acos(-1.0)#define P system("pause")using namespace std;struct node{ node *next[10]; int flag; node() //初始化每个节点 { flag=0; for(int i=0;i<10;i++) this->next[i]=NULL; } };node *root;int c;void creat(string s){ int i,len=s.length(); node *p=root; for(i=0;i<len;i++) { int k=s[i]-'0'; if(p->next[k]==NULL) { node *q=new node; p->next[k]=q; p=q; } else if(p->next[k]->flag==1){ c=1;return;} else { p=p->next[k]; if(i==len-1) for(int j=0;j<10;j++) if(p->next[j]) { c=1;return; } } } p->flag=1;}void free(node* root){ for(int i=0;i<10;i++) if(root->next[i]) free(root->next[i]); delete root; }int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout); int t; scanf("%d",&t); string s; while(t--) { int n; cin>>n; c=0; root=new node; for(int i=0;i<n;i++) { cin>>s; creat(s); } if(c) printf("NO\n"); else printf("YES\n"); free(root); } // P; return 0; }
0 0
- hdu1671( 字典树)
- hdu1671 PhoneList (字典树)
- hdu1671(trie树(字典树))
- Trie树(字典树)poj3630+hdu1671
- HDU1671 Phone List(字典树)
- hdu1671 Phone List (字典树)
- hdu1671 Phone List (字典树)
- hdu1671 Phone List(字典树)
- HDU1671:Phone List(字典树)
- HDU1671字典树
- hdu1671之字典树
- hdu1671 字典树 入门
- 字典树HDU1671
- hdu1671 字典树
- 字典树(hdu1671)
- HDU1671 字典树
- HDU1671(字典树)
- hdu1671 字典树
- sql server 2008 安装时的检查提示"重启计算机失败"
- Iis写权限漏洞认识 2010-10-13 10:47:20
- CABasicAnimation animationWithKeyPath Types
- CKEditor+CKFinder的jsp实例(附下载包)
- [C#]委托和事件(详细讲解)
- hdu1671( 字典树)
- NYOJ636 世界末日 抽屉原理
- Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
- ios即时通讯客户端开发之-mac上搭建openfire服务器
- 何去何从
- Jelastic!Java PaaS 云平台入门使用
- [unity3d]unity与3dmax美工部分总结
- spring使用体会——sesionFactory注入的空指针异常(nullpointException)
- POJ 1797 Heavy Transportation