Phone List(排序二维字符串)

版权声明:莉莉莉 https://blog.csdn.net/qq_41700151/article/details/83385126

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
如果一个号码是另一个的前缀就要输出no反之输出yes
1.按ASC码排序二维数组,只需比较当前的和后面紧挨着的那个
2.字典树(还没学)
code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e4+10;
using namespace std;
char s[MAX][1000];
bool cmp(const char *a,const char *b)
{
    if(strcmp(a,b)>0)
        return false;
    return true;
}
int main()
{
    int t,n,flag;
    cin>>t;
    while(t--)
    {
        cin>>n;
        char *ps[MAX]= {NULL};
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
            ps[i]=s[i];
        }
        sort(ps,ps+n,cmp);
      //以上是二维数组排序
        flag=0;
        for(int i=0; i<n-1; i++)
        {
            int len1=strlen(ps[i]);
            if(strncmp(ps[i],ps[i+1],len1)==0)
            {
                flag=1;
                break;
            }
        }
        if(!flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }

    return 0;
}

还有另一种二维数组排序的方式(这一种比上面的时间复杂度高)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e4+10;
using namespace std;
struct node
{
    char s[1000];
}nod[MAX];
bool cmp(const node &a,const node &b)
{
    if(strcmp(a.s,b.s)<0)
        return true;
    return false;
}
int main()
{
    int t,n,flag;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0; i<n; i++)
        {
            scanf("%s",nod[i].s);
        }
        sort(nod,nod+n,cmp);
      //  for(int i=0;i<n;i++)
       //  printf("%s\n",nod[i].s);
        flag=0;
        for(int i=0; i<n-1; i++)
        {
            int len1=strlen(nod[i].s);
            if(strncmp(nod[i].s,nod[i+1].s,len1)==0)
            {
                flag=1;
                break;
            }
        }
        if(!flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41700151/article/details/83385126
今日推荐