HDU 1671 – Phone List [排序]

Link

传送门

Problem

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.

Mean

给你一些人的电话号码,有些电话号码可能会冲突,比如打“91125426 ”时,拨到“911”时就通了。问你这组电话号码中有没有冲突。

Analysis

直觉就是插入到字典树判断,其实不必。

首先对字符串排序,那么冲突的一定在相邻位置,判断一下即可。AC后发现才140秒,比某些字典树快…

坑就是电话号码可能会以0开头,刚开始直接用long long做的,WA…

Code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxn = 10240;
int N;
string s[maxn];

bool judge() {
    for(int i = 1; i < N; ++i) {
        bool ok = false;
        for(int j = 0; j < s[i].size() && j < s[i-1].size(); ++j)
            if(s[i][j] != s[i-1][j]) {
                ok = true;
                break;
            }
        if(!ok) return false;
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    int T; cin >> T;
    while(T--) {
        cin >> N;
        for(int i = 0; i < N; ++i)
            cin >> s[i];
        sort(s, s + N);
        cout << (judge() ? "YES" : "NO") << endl;
    }
    return 0;
}

欢迎留言