pku 3630--Phone List(二分查找,trie树)

Phone ListTime Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10228 Accepted: 3288

Description

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
Emergency 911
Alice 97 625 999
Bob 91 12 54 26

In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output
NO
YES

 

 #include <iostream> using namespace std; char str[10010][12]; int n; int cmp(const void* a,const void *b) { return (strcmp((char *)a,(char*)b)); } int scmp(char *p,char *q) //二分查找的比较函数 { int len1=strlen(p); int len2=strlen(q); int s=len1<len2?len1:len2; for(int i=0;i<s;i++) { if(p[i]>q[i]) return 1; else if(p[i]<q[i]) return -1; } return 0; } bool Tsearch(char *ch,int num) { int low=0,high=n,mid; while(low<=high) { mid=(low+high)/2; if(mid>=n||mid<0) break; int result=scmp(str[mid],ch); if(result==0&&mid==num) //防止找到自己 result=-1; if(result==0) return true; else if(result==1) high=mid-1; else low=mid+1; } return false; } int main() { int t; scanf(“%d”,&t); while(t–) { int i; scanf(“%d”,&n); cin.ignore(); for(i=0;i<n;i++) scanf(“%s”,str[i]); qsort(str,n,sizeof(str[0]),cmp); //排序 for(i=0;i<n;i++) { if(Tsearch(str[i],i)) //逐个查找 { printf(“NO/n”); break; } } if(i>=n) printf(“YES/n”); } }

 

 

//poj 3630 Accepted 2568K 125MS #include<iostream> using namespace std; struct Node{ bool ends; Node* child[10]; }m[100000]; int m_num; class Trie //利用类的自动析构函数 { public: Node root; Trie() { root=m[0]; } bool insert(char* str) { Node *temp = &root; int i = 0; int len = strlen(str); int num; while(str[i]) { num=str[i]-‘0’; if(i==len-1 && temp->child[num]!=NULL ) //遇到结尾已经被占用的 { return false; } if(temp->child[num]==NULL) //结点未被占用 { temp->child[num] = &m[m_num]; m[m_num].ends = false; memset(m[m_num].child,NULL,sizeof(m[m_num].child)); m_num++; } if(temp->child[num]->ends == true) //遇到的是别人的结尾 { return false; } temp = temp->child[num];//传递指针 i++; } temp->ends = true; //标记结尾已用 return true; } }; int main() { int t,n; char str[15]; bool flag; scanf(“%d”,&t); while(t–) { flag=true; m_num=1; scanf(“%d”,&n); Trie temp;//构造一个Trie类 while(n–) { scanf(“%s”,&str); if(flag && (!temp.insert(str)) ) //判断已经匹配出错或者该号码匹配出错,前一个条件可以节约运算次数 flag = false; } if(flag) printf(“YES/n”); else printf(“NO/n”); //至此Trie类析构 } return 0; }

 

 

 

    原文作者:Trie树
    原文地址: https://blog.csdn.net/zsc09_leaf/article/details/6218617
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞