poj 3630 Phone List 贪心

Phone List

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23722   Accepted: 7289

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:

  • Emergency 911
  • Alice 97 625 999
  • 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

题意是,给你N个数字,然后问你里面有没有一些数字是另外一些数字的前缀,如果是的话,就输出NO,不是的话就输出YES

唔,正版做法我觉得应该是字典树

但是这道题可以取巧,直接按照字典序大小,sort一下,然后比较a[i]和a[i+1],看看a[i]是不是a[i+1]的前缀啥的~

string a[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        cin>>n;
        vector<string> s;
        string ss;
        for(int i=0;i<n;i++)
        {
            cin>>ss;
            s.push_back(ss);
        }
        sort(s.begin(),s.end());
        int flag=1;
        for(int i=0;i<n-1;i++)
        {
            if(s[i+1].find(s[i])==0)
                flag=0;
        }
        if(flag==0)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
}
时间: 2024-10-15 10:49:47

poj 3630 Phone List 贪心的相关文章

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3085 Quick Change (贪心)

Quick Change Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5801   Accepted: 4175 Description J.P. Flathead's Grocery Store hires cheap labor to man the checkout stations. The people he hires (usually high school kids) often make mistak

poj 3630 Phone List (字典树 +静态字典树)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22235   Accepted: 6885 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 catalogu

POJ 1659 Frogs&#39; Neighborhood (贪心)

题意:中文题. 析:贪心策略,先让邻居多的选,选的时候也尽量选邻居多的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>

POJ - 3630 代码

Trie的简单应用,只涉及插入字符串的操作. 需要注意的是,输入数据有T组,在处理每一组数据之前都要初始化root,由于忽视了这一点WA了n次. 还有一点就是,在发现一组数据答案为“NO”之后,仍然要读完这组数据的字符串.在这一点上也WA了好多次= = 另外,本题大概需要建立4000000个节点,如果采用动态空间建点会TLE. 看来malloc操作确实是很慢啊. 所以提前开一个大的数组空间,静态的就好了. 代码如下: /* Author : Magician Van */ #include <i

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

POJ 3190 Stall Reservations贪心

POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obvi