POJ 3630 Phone List

题意:有n个电话号码,如果号码a是号码b的前缀,那么号码b就无法被拨打,问这n个号码之间会不会有这种冲突。

 

解法:一看就觉得是个字典树……但是不会写字典树orz……于是找到一种办法……先将这些号码按字符串排序,如果a是b的前缀,则a会排在b相邻的位置,所以只需要比较一次相邻字符串就可以了,直接用string搞T了,只好关掉了同步流。

 

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
string s[10005];
int main()
{
    ios :: sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--)
    {
        int n;
        cin >> n;
        for(int i = 0; i < n; i++)
            cin >> s[i];
        sort(s, s + n);
        bool ans = true;
        for(int i = 1; i < n; i++)
        {
            if(s[i - 1].size() <= s[i].size() && s[i - 1] == s[i].substr(0, s[i - 1].size())) ans = false;
        }
        if(ans) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

  

posted @ 2015-09-30 14:19  露儿大人  阅读(128)  评论(0编辑  收藏  举报