UVa 11362 - Phone List
来源:互联网 发布:淘宝上卖的假索尼电视 编辑:程序博客网 时间:2024/06/10 06:40
题目:给你一组电话号码,判断是否有一些号码是其他的前缀(或相等)。
分析:字符串,字典树。利用字典树储存查询即可,注意两种情况处理:
1.先短后长(前缀在前);2.先长后短(前缀在后)。
说明:第580题了,目标600╮(╯▽╰)╭。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;#define nodesize 100001 //节点个数 #define dictsize 128 //字符集大小 //trietypedef struct node1 { int flag; //值域 node1* next[dictsize]; }tnode; tnode dict[nodesize]; class Trie { private: int size; tnode* root; public: Trie() {initial();} void initial() { memset(dict, 0, sizeof(dict)); size = 0; root = newnode(); } tnode* newnode() {return &dict[size ++];} int insert(char* word) { tnode* now = root; for (int i = 0 ; word[i] ; ++ i) { if (!now->next[word[i]]) now->next[word[i]] = newnode(); now = now->next[word[i]]; if (now->flag) return 1; }now->flag = 1; for (int i = 0 ; i < dictsize ; ++ i) if (now->next[i]) return 1; return 0; }}trie; //trie endint main(){int t,n; char buf[12];while (~scanf("%d",&t))while (t --) {trie.initial();scanf("%d",&n);int flag = 0;for (int i = 0 ; i < n ; ++ i) {scanf("%s",buf);if (trie.insert(buf))flag = 1;}if (flag)printf("NO\n");else printf("YES\n");} return 0;}
0 0
- UVa 11362 - Phone List
- UVA 11362 - Phone List 的优化历程
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- 佟刚老师《Spring4视频教程》学习笔记(7)
- P027第五题
- UBUNTU手工自动挂载其他硬盘分区
- java this关键字的用法
- 第二周项目一:旱冰场造价
- UVa 11362 - Phone List
- html学习记录1
- mark
- 3.2 Piecewise Linear Interpolation(乱写的)
- hadoop学习(二)-----配置hadoop单机伪分布式模式
- POJ1065---Wooden Sticks
- 排序算法--从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- eclipse创建android项目出现error libz.so.1: cannot open shared object file:No such file or directory
- c++_ch02_01