(hdu step 5.2.3)Phone List(Trie實現:在一堆號碼中,判斷是否有號碼是其他號碼的首碼),hdutrie

來源:互聯網
上載者:User

(hdu step 5.2.3)Phone List(Trie實現:在一堆號碼中,判斷是否有號碼是其他號碼的首碼),hdutrie


題目:

Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 239 Accepted Submission(s): 96
 
Problem DescriptionGiven 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:
1. Emergency 911
2. Alice 97 625 999
3. 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.
 
InputThe 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. 
OutputFor each test case, output “YES” if the list is consistent, or “NO” otherwise. 
Sample Input
2391197625999911254265113123401234401234598346
 
Sample Output
NOYES
 
 
Source2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3) 
Recommendlcy 

題目分析:

               前一篇部落格介紹了這道題的非Trie做法。那麼這一篇介紹一下這道題的設計初衷:Trie的基本使用——在首碼匹配中的應用。

以下介紹一下Trie的基本知識點(個人理解,請各位批判地接受)。


1、性質

1)根節點不包含任何字元

2)某一結點的所有子節點包含的字元都不相同。

3)從根節點開始到某一結點所經過的路徑就是到該節點為止所能形成的單詞。


2、基本操作

1)添加:

假設存在字串str,Trie樹的根結點為root。i=0,p=root。

  1)取str[i],判斷p->next[str[i]-97]是否為空白,若為空白,則建立結點temp,並將p->next[str[i]-97]指向temp,然後p指向temp;若不為空白,則p=p->next[str[i]-97];

  2)i++,繼續取str[i],迴圈1)中的操作,直到遇到結束符'\0',此時將當前結點p中的isStr置為true。

void insert(Trie* root,char* s){if(root == NULL || s == '\0'){return ;}Trie* p = root;while(*s != '\0'){if(p->next[*s-'a'] == NULL){Trie* temp = malloc(sizeof(Trie));int i;for(i = 0 ; i < MAX ; ++i){temp->next[i] = NULL;}temp->isStr = false;p->next[*s-'a'] = temp;p = p->next[*s-'a'];}else{p = p->next[*s-'a'];}s++;}p->isStr = true;}



2)查詢:

假設要尋找的字串為str,Trie樹的根結點為root,i=0,p=root

  1)取str[i],判斷判斷p->next[str[i]-97]是否為空白,若為空白,則返回false;若不為空白,則p=p->next[str[i]-97],繼續取字元。

  2)重複1)中的操作直到遇到結束符'\0',若當前結點p不為空白並且isStr為true,則返回true,否則返回false。

bool search(Trie* root,char* s){Trie* p = root;while(p != NULL && *s != '\0'){p = p->next[*s-'a'];s++;}return (p != NULL && p->isStr);}



3)刪除:

            以遞迴的形式進行刪除。

void del(Trie* root){int i;for(i = 0 ; i < MAX ; ++i){if(root->next[i] != NULL){del(root->next[i]);}}free(root);}



3、實現的基本思路

1)從根節點開始一次搜尋

2)去除關鍵詞的第一個字元,並根據該字元選擇相應的子樹,然後轉到相應的子樹中去繼續搜尋

3)不斷地重複2)操作指導完成搜尋

4)在某個節點處,如果關鍵詞的所有字元已經被取出。則讀出該節點的附加資訊。



代碼如下:

/* * c2.cpp * *  Created on: 2015年3月7日 *      Author: Administrator */#include <iostream>#include <cstdio>using namespace std;const int MAX = 10;//每個節點的孩子節點數bool flag;//用於標記是否存在一個號碼是另外一個號碼的首碼的情況typedef struct TrieNode{//字典樹Trie結點bool isStr;//用於標記到該節點是否形成單詞struct TrieNode* next[MAX];//每個節點的孩子節點}Trie;/** * 字典樹(首碼樹)Trie的插入操作 * 將字串s插入根為root的字典樹中 */void insert(Trie* root,char* s){if(root == NULL || s == '\0'){//如果這個字典樹為空白||需要插入的字串為空白return ;//直接返回}Trie* p = root;while(*s != '\0'){//順序遍曆要插入的這個字串if(p->next[*s-'0'] == NULL){//如果目前這個孩子節點還不存在//則建立該節點Trie* temp = (Trie*)malloc(sizeof(Trie));int i;for(i = 0 ; i < MAX ; ++i){temp->next[i] = NULL;}temp->isStr = false;p->next[*s-'0'] = temp;//將p中的孩子節點指向建立的這個temp結點p = p->next[*s-'0'];//更新當前在字典樹中尋找到的結點}else{p = p->next[*s-'0'];//更新當前在字典樹中尋找到的結點}if(p->isStr == true){//如果當前尋找到的結點是之前某一個單詞的結束結點,則表明存在某一個號碼是另外一個號碼的首碼的情況flag = true;//將flag標記為true.表明存在某一個號碼是另外一個號碼的首碼的情況return ;//已經能得到結果.返回}//如果能繼續執行以下代碼,表明到當前結點為止,不存在號碼是另外一個號碼的首碼的情況.s++;//繼續遍曆下一個節點}p->isStr = true;//字串已經插入完畢.將該節點的isStr標記為true,表明在這裡可以形成一個號碼int i;for(i = 0 ; i < MAX ; ++i){//遍曆該節點的所有子節點if(p->next[i] != NULL){//如果該子節點存在.flag = true;//說明該號碼是另外一個號碼的首碼.將flag標記為truebreak;//跳出迴圈}}}/** * 字典樹(首碼樹)Trie的尋找操作 * 如果以root為根的字典樹中存在s字串.則返回true, * 否則返回false */bool search(Trie* root,char* s){Trie* p = root;while(p != NULL && *s != '\0'){//如果當前遍曆到的結點不為NULL&&字串s還沒有遍曆完//則繼續遍曆p = p->next[*s-'0'];s++;}return (p != NULL && p->isStr);//判斷結束結點是否存在&&是否在該節點出形成一個號碼}/** * 字典樹(首碼樹)Trie的刪除操作 * */void del(Trie* root){int i;for(i = 0 ; i < MAX ; ++i){//遍曆該節點的所有孩子節點if(root->next[i] != NULL){//如果該孩子節點不為NULLdel(root->next[i]);//遞迴刪除該孩子節點的孩子節點}}free(root);//釋放空間}int main(){int t;scanf("%d",&t);char s[11];while(t--){flag = false;//初始化操作Trie* root = (Trie*)malloc(sizeof(Trie));root->isStr = false;int i;for(i = 0 ; i < MAX ; ++i){root->next[i] = NULL;}int n;scanf("%d",&n);for(i = 0 ; i < n ; ++i){scanf("%s",s);if(flag == false){//如果當前還不存在某一個號碼是另外一個號碼的首碼的情況insert(root,s);}}if(flag == true){printf("NO\n");}else{printf("YES\n");}del(root);//釋放空間。如果在這裡不釋放空間,會導致記憶體激增而最後MLE}return 0;}














聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.