Phone List (prefix judgment-two-point lookup)

Source: Internet
Author: User
Tags printf sort first row

Description Given A list of phone numbers, determine if it is consistent in the sense this no number is the prefix of Anot Her. 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.

Input the 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.

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

Main topic:

give a few sets of strings to determine if there is a string in each set of strings is the prefix of another string, if so, output no, if not, output yes.

Input:

The first line, an integer t (1<=t<=40), indicates how many sets of test data, the next T-group data, the first row of each group of data is an integer n (1<=n<=10000), which represents the number of data in the group, followed by a string of n rows per line.

Problem Solving Ideas:

The subject can be solved with a dictionary tree, but I use a simple lookup. For each set of data, the string is sorted by dictionary order, and then only by comparing each of the two adjacent strings to determine if they have a prefix relationship.

The code is as follows: C + + code

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
String s[10005];
Determines whether the adjacent two strings after sorting is a string that is the prefix of another string
bool judge (int n)
{
    int i,j,m;
    for (i=0;i<n-1;i++)
    {
        m=s[i].size ();
        for (j=0;j<m;j++)
        {
            if (S[i][j]!=s[i+1][j]) break
                ;
        }
        if (j==m)
            return false;
    }
    return true;
}
int main ()
{
    int t,n,i;
    scanf ("%d", &t);
    while (t--)
    {
        scanf ("%d", &n);
        for (i=0;i<n;i++)
            cin>>s[i];//can only be entered with CIN, not scanf input
        sort (s,s+n);//Sort by Dictionary
        if (judge (n))
           printf ("yes\n");
        else
           printf ("no\n");
    }
    return 0;
}


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.