Hdu 1671 -Phone List (字典树模板)
来源:互联网 发布:北京矩阵联合营销顾问 编辑:程序博客网 时间:2024/05/17 23:39
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1671
题目大意:
给出
分析:
将字符串按长度排序后插入即可,当前字符串插入字典树时,若中途发现该结点为单词,说明该单词的前缀已被插入,返回
注意:如果使用指针构造字典树,交G++可能会爆空间,建议交C++
代码:
#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<cmath>using namespace std;typedef long long ll;struct Trie{ Trie *next[10]; int val; Trie(){ val = 0; for (int i = 0 ; i < 10 ; i ++) next[i] = NULL; }};bool addword(char *str,Trie* node){ if (node->val!=0) return false; if (node->next[str[0]-'0']==NULL) node->next[str[0]-'0'] = new Trie; node = node->next[str[0]-'0']; str++; //printf("%d\t",*str); if (*str) return addword(str,node); else { node->val++; return true; }}int query(char *str ,Trie *node){ // printf("%d\t",*str); if (node->next[*str-'0']==NULL) return 0; else node= node->next[*str-'0']; ++str; if (*str) return query(str,node); else return node->val;}void del(Trie *node){ for (int i = 0 ; i < 10 ; i ++) { if (node->next[i]!=NULL) del(node->next[i]); } delete node; node = NULL; return;}struct String{ char str[20]; int len; void read() { scanf("%s",str); len = strlen(str); } bool operator <(const String& a) { return len<a.len; }}s[12000];int main(){ int T,n; //printf("hello\n"); Trie *head; //printf("world\n"); scanf("%d",&T); while (T--) { scanf("%d",&n); head = new Trie; for (int i = 1; i <= n ; i ++) { s[i].read(); } sort(s+1,s+n+1); int f = 1; //printf("ok\n"); for (int i = 1 ; i <= n ; i ++) { if (!addword(s[i].str,head)) { f = 0; break; } } //printf("fuck\n"); if (!f) printf("NO\n"); else printf("YES\n"); del(head); } 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(字典树)
- 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(字典树)
- HDU 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- [Qt] 利用QtWebKit完成JavaScript访问C++对象
- (卡特兰数,二叉树数量)How Many Trees? &&Count the Trees -- HDOJ
- archive 生成的ipa 比 bulid然后拖到itunes里生成的ipa要小7、8M
- fio使用(windows+linux)
- 【Java】编程过程中遇到的中文编码问题
- Hdu 1671 -Phone List (字典树模板)
- [RK3288][Android6.0] WiFi之与ConnectivityService的交互
- 开发中遇见的问题汇总
- Java中如何遍历Map对象的4种方法
- C++中的string 类 简单介绍
- 'autocomplete="off"'在Chrome中不起作用解决方案
- 基于redis的抽奖解决方案
- jQuery ajax
- fflush