hdu 1671 Phone List (字典树)
来源:互联网 发布:厦门seo技术 编辑:程序博客网 时间:2024/06/06 19:23
先按长度从大到小排序。
然后判断短的是不是长的的前缀就好。
注意要回收内存
第二篇为静态分配内存。。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;struct node{ struct node *br[10]; int num;};node *root;void Trie_init(){ root=new node; root->num=0; for(int i=0;i<10;i++)root->br[i]=NULL;}void Trie_insert(char str[]){ int len=strlen(str); node *s=root; for(int i=0;i<len;i++) { int id=str[i]-'0'; if(s->br[id]==NULL) { node *t=new node; for(int j=0;j<10;j++)t->br[j]=NULL; t->num=0; s->br[id]=t; } s=s->br[id]; s->num++; }}int Trie_find(char str[]){ int len=strlen(str); node *s; s=root; int res; for(int i=0;i<len;i++) { int id=str[i]-'0'; if(s->br[id]==NULL)return 0; else { s=s->br[id]; res=s->num; } } return res;}void Trie_del(node *p){ for(int i=0;i<10;i++) { if(p->br[i]!=NULL) Trie_del(p->br[i]); } free(p);}struct t{ char str[12]; int len; bool operator < (const t &cmp)const { return len>cmp.len; }}save[10005];int main(){ int T; scanf("%d",&T); while(T--) { Trie_init(); int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",save[i].str); save[i].len=strlen(save[i].str); } sort(save+1,save+1+n); bool flg=true; for(int i=1;i<=n;i++) { if(Trie_find(save[i].str)){flg=false;break;} else Trie_insert(save[i].str); } if(flg)printf("YES\n"); else printf("NO\n"); Trie_del(root); } return 0;}
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;struct node{ struct node *br[10]; int num;};node trid[100000];node *root;int cnt;void Trie_init(){ cnt=0; root=new node; root->num=0; for(int i=0;i<10;i++)root->br[i]=NULL;}void Trie_insert(char str[]){ int len=strlen(str); node *s=root; for(int i=0;i<len;i++) { int id=str[i]-'0'; if(s->br[id]==NULL) { node *t=&trid[cnt++]; for(int j=0;j<10;j++)t->br[j]=NULL; t->num=0; s->br[id]=t; } s=s->br[id]; s->num++; }}int Trie_find(char str[]){ int len=strlen(str); node *s; s=root; int res; for(int i=0;i<len;i++) { int id=str[i]-'0'; if(s->br[id]==NULL)return 0; else { s=s->br[id]; res=s->num; } } return res;}struct t{ char str[12]; int len; bool operator < (const t &cmp)const { return len>cmp.len; }}save[10005];int main(){ int T; scanf("%d",&T); while(T--) { Trie_init(); int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",save[i].str); save[i].len=strlen(save[i].str); } sort(save+1,save+1+n); bool flg=true; for(int i=1;i<=n;i++) { if(Trie_find(save[i].str)){flg=false;break;} else Trie_insert(save[i].str); } if(flg)printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- hdu 1671 Phone List 字典树
- [字典树 ] hdu 1671 phone list
- hdu 1671 Phone List(字典树)
- 【字典树】hdu 1671 Phone List
- Hdu 1671 Phone List 字典树
- hdu 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树Trie)
- HDU 1671 Phone List 字典树
- 字典树 HDU- 1671 Phone List
- HDU-#1671 Phone List(字典树)
- hdu 1671 Phone List (字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树Trie)
- hdu 1671 Phone List 字典树
- HDU 1671 Phone List 字典树
- hdu 1671 Phone List 字典树
- hdu 1671 phone list(字典树)
- Java关键字this、super使用总结
- Android学习之TextView的使用
- 纯析构函数理解
- 批处理设置Java及Tomcat的环境变量
- 棋盘模拟——Emag eht htiw Em Pleh
- hdu 1671 Phone List (字典树)
- J3续航测试
- (十一)洞悉linux下的Netfilter&iptables:iptables命令行工具源码解析【上】
- hdu_1022(栈问题)&杭电oj_1022
- hdu 1166 敌兵布阵 线段树 单点更新
- 微信中MMAlert(半透明底部弹出菜单)的使用介绍
- php防止乱码
- 登陆 Mac OS SSH 的用户身份验证设置
- java高新技术第一天——反射