ACM篇:POJ 3630 -- Phone Number

来源:互联网 发布:淘宝当季新品怎么设置 编辑:程序博客网 时间:2024/06/04 19:45

对电话号码排序。

如果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;}
0 0
原创粉丝点击