ACM篇:POJ 3630 -- Phone Number

對電話號碼排序。

如果A是B的前綴,
那麼A恰好在B前面一位。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 10000;

int n;
char s[MAXN+1][10+1];
char *ps[MAXN+1];
void read_phone()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%s", s[i]);
        ps[i] = s[i];
    }
}
bool _cmp(char *s, char *t)
{
    return strcmp(s, t) < 0;
}
bool is_consistant()
{
    for (int i = 0; i < n - 1; i++)
        if (!strncmp(ps[i], ps[i+1], strlen(ps[i])))
            return false;
    return true;
}
int main()
{
    int T;
    int kase = 0;
    scanf("%d", &T);
    while (++kase <= T)
    {
        read_phone();
        sort(ps, ps+n, _cmp);
        printf("%s\n", is_consistant() ? "YES" : "NO");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章