(Hdu step 5.2.3) Phone list (in a bunch of numbers, determine if a number is a prefix for other numbers)

Source: Internet
Author: User
Tags number strings strcmp


Topic:

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 this 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-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.
Inputthe first line of input gives a single integer, 1 <= t <=, the number of test cases. Each test case is starts with N, the number of the phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with a unique phone number on each line. A phone number is a sequence of at the most ten digits.
Outputfor each test case with 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


Topic Analysis:

This problem belongs to Trie's entry-level topic. But here first introduce how to solve this problem without trie. In a bunch of numbers,

to determine if a number is a prefix for another number. We can sort the numbers first by the dictionary order. Then you can determine whether the number in the adjacent two numbers is the prefix of other numbers. The overall time complexity is much better than the n^2 of violence.


There is also a strncmp to be introduced here.

strncmp (): This function is used to compare S1 and S2 strings, and this function returns a value whose symbol is related to the first comparison of different characters. If two strings are equal, STRNCMP will return 0. If S1 is a substring of S2, S1 is less than S2. Also, function int strncmp (const char *S1, const char *S2, size_t size) This function is very similar to strcmp. The difference is that the STRNCMP function is a specified comparison of size characters. That is, if the string S1 is the same as the first size character of S2, the function returns a value of 0.


The code is as follows:

/* * c.cpp * *  Created on:2015 March 7 *      author:administrator * * #include <iostream> #include <cstdio> #i Nclude <cstring>using namespace Std;const int maxn = 10001;char num_str[maxn][11];//used to store number strings/** * Let number strings in ascending order of dictionary order * /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) {//read into n numbers sequentially scanf ( "%s", Num_str[i]);} Qsort (Num_str,n,sizeof (num_str[0]), CMP);//The N numbers are arranged in dictionary order bool flag = false;//is used to mark if a number is a prefix for other numbers for (i = 0; i < n-1; ++i) {/ /Traverse all number strings//If the first n characters of a number are the same as the first n characters of other numbers. if (strncmp (Num_str[i],num_str[i+1],strlen (num_str[i)) = = 0) {flag = true;/ /. Indicates that the number is a prefix of another number break;//jump out of the loop}}if (flag = = True) {//If the number is a prefix of other numbers printf ("no\n");//Print no}else{// If no number is a prefix for other numbers printf ("yes\n");//print Yes}}return 0;}




















(Hdu step 5.2.3) Phone list (in a bunch of numbers, determine if a number is a prefix for other numbers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.