码迷,mamicode.com
首页 > 其他好文 > 详细

E - Phone List(字典序,string类型使用)

时间:2015-09-10 15:50:45      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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: 
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. 
 

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
 
 
 
题目意思就是说输入的号码中不能有任意一个号码是另一个号码的前缀
思路:所有号码按照字典序进行排序,(sort函数可以实现字典排序),然后遍历所有数,看前一个数是否是后一个数的前缀
 
代码
 
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string s[10010];
bool judge(int n)
{
    int len, i, j;
    for(i = 0; i < n-1; i++)
    {
        len = s[i].size();
        //printf("\n%d\n", len);
        for(j = 0; j < len; j++)
        {
            if(s[i][j] != s[i+1][j])
            {
               // cout << s[i][j] << ‘ ‘ << s[i+1][j];
                break;
            }
        }
        //printf("\n%d\n", j);
        if(j == len)
            return true;
    }
    return false;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            cin >> s[i];
        sort(s, s+n);
        //for(int i = 0; i < n; i++)
           // cout << s[i] << " ";
        if(judge(n))
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

 

 

E - Phone List(字典序,string类型使用)

标签:

原文地址:http://www.cnblogs.com/rain-1/p/4797861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!