字符串--Phone List

来源:互联网 发布:百变空间制作软件 编辑:程序博客网 时间:2024/06/07 03:34
A -Phone List
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatus

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:
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.
 

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
1
9
9
17625999
91125426
4
51131230123440
6
12345983
4
 

Sample Output

NO
YE
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define N 100010using namespace std; char list[N][15];char temp[N][15]; void merge(int s1,int e1,int s2,int e2){     int i,j,k;     i=s1;j=s2;k=0;    while(i<=e1&&j<=e2)     {         if(strcmp(list[i],list[j])<0) strcpy(temp[k++],list[i++]);         else strcpy(temp[k++],list[j++]);     }     while(i<=e1)  strcpy(temp[k++],list[i++]);     while(j<=e2)  strcpy(temp[k++],list[j++]);     for(k=0,i=s1;i<=e2;i++,k++)    strcpy(list[i],temp[k]);} void mergesort(int s,int e){   int m=0;    if(s<e)    {        m=(s+e)>>1;        mergesort(s,m);       mergesort(m+1,e);        merge(s,m,m+1,e);    }}bool judge(char *s1,char *s2){    int i=0,j=0,len1,len2;    len1=strlen(s1);len2=strlen(s2);    while(i<len1&&j<len2)    {        if(s1[i]==s2[j])         {             i++;j++;        }         else  return false;    }    if(i==len1||j==len2) return true;} int main(){    int test;    scanf("%d",&test);    while(test--)    {        int n;        scanf("%d",&n);        int i;        for(i=0;i<n;i++)        {            scanf("%s",list[i]);        }        mergesort(0,n-1);        bool yes=true;        for(i=0;i<=n-2;i++)        {            if(judge(list[i],list[i+1]))            {                yes=false;break;            }        }        if(yes) printf("YES\n");        else printf("NO\n");    }    return 0;}

S
0 0
原创粉丝点击