(hdu step 5.2.3)Phone List(在一堆號碼中,判斷是否有號碼是其它號碼的首碼),hdu5.2.3

來源:互聯網
上載者:User

(hdu step 5.2.3)Phone List(在一堆號碼中,判斷是否有號碼是其它號碼的首碼),hdu5.2.3


題目:

Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 235 Accepted Submission(s): 92
 
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怎麼解決這道題。在一堆號碼中,

要判斷一個號碼是否是其他號碼的首碼。我們可以先對這對號碼按字典序進行一下排序。然後依次判斷相鄰兩個號碼中是否有號碼是其它號碼的首碼即可。整體的時間複雜度要比暴力做法的O(n^2)要好很多。


這裡還需要介紹一下的是strncmp這個函數。

       strncmp(): 這個函數用來比較s1和s2字串,這個函數將返回一個值, 它的符號與第一對不同的字元的比較結果相關。如果兩個字串相等的話,strncmp將返回0。如果s1是s2的一個子串的話,s1小於s2。此外還有,函數 int strncmp (const char *s1, const char *s2, size_t size) 此函數與strcmp極為類似。不同之處是,strncmp函數是指定比較size個字元。也就是說,如果字串s1與s2的前size個字元相同,函數傳回值為0。


代碼如下:

/* * c.cpp * *  Created on: 2015年3月7日 *      Author: Administrator */#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 10001;char num_str[maxn][11];//用於儲存號碼字串/** * 讓號碼字串按字典序升序排列 */int cmp(const void* a,const void* b){return strcmp((char*)a,(char*)b);}int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);int i;for(i = 0 ; i < n ; ++i){//依次讀入n個號碼scanf("%s",num_str[i]);}qsort(num_str,n,sizeof(num_str[0]),cmp);//將n個號碼按字典序排列bool flag = false;//用於標記是否有號碼是其他號碼的首碼for(i = 0 ; i < n-1 ; ++i){//遍曆所有的號碼字串 //如果有號碼的前n個字元與其他號碼的前n個字元是一樣的.if(strncmp(num_str[i],num_str[i+1],strlen(num_str[i] )) == 0){flag = true;//.則表明該號碼是另外一個號碼的首碼break;//跳出迴圈}}if(flag == true){//如果由號碼是其它號碼的首碼printf("NO\n");//列印NO}else{//如果沒有號碼是其它號碼的首碼printf("YES\n");//列印YES}}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.