A(Phone Number)

1.字典树

 

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n;
bool F;
char tu[1010][20];
int cmp(const void *a,const void *b)
{
    return strcmp((char *)b,(char *)a);
}
typedef struct Node
{
    int f;
    struct Node *next[15];
}Node,*Tree;
void Creat(Tree &T)
{
    T=(Tree)malloc(sizeof(Node));
    for(int i=0;i<15;i++)
    {
        T->next[i]=NULL;
    }
    T->f=0;
}
void in(Tree &T,char *s)
{
    int k=strlen(s);
    int t;
    Tree p=T;
    for(int i=0;i<k;i++)
    {
       t=s[i]-'0';
       if(p->next[t]==NULL)
        Creat(p->next[t]);
       p=p->next[t];
       p->f++;
    }
    if(p->f>1)
        F=true;
}
int main()
{
    Tree T;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        Creat(T);
        F=false;
        for(int i=0;i<n;i++)
        {
            scanf("%s",tu[i]);

        }
        qsort(tu,n,sizeof(tu[0]),cmp);
        for(int i=0;i<n;i++)
        {
            //cout<<tu[i]<<endl;
              in(T,tu[i]);
        }
        if(F)
        {
            printf("NO\n");
        }
        else printf("YES\n");
    }
    return 0;
}

2.暴力  

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

int cmp(const void *a,const void *b)
{
 return strcmp((char *)a,(char *)b);
}
char a[1001][1001];
int main()
{
 int N,len,tt;
 while(scanf("%d",&N),N)
 {
 tt=0;
 for(int i = 0;i<N;i++)
 scanf("%s",a[i]);
 int flag = 1;
 qsort(a,N,sizeof(a[0]),cmp);
 for(int i = 0;i<N&&flag;i++)
 {
 len = strlen(a[i]);
 for(int j = 0;j<len&&flag;j++)
 {
 for(int k = i+1;k<N;k++)
 {
 if(a[i][j]==a[k][j])
 {
 tt++;
 break;
 }
 else tt=0;
 }
 if(tt==len)
 flag=0;
 }
 }
 printf("%s",flag?"YES":"NO");
 printf("\n");
 }
 return 0;
}
View Code

相关文章:

  • 2021-07-05
  • 2022-12-23
  • 2021-08-06
  • 2021-09-19
  • 2022-01-11
  • 2021-09-22
  • 2021-11-14
  • 2021-04-12
猜你喜欢
  • 2021-11-08
  • 2021-06-14
  • 2021-09-04
  • 2021-05-15
  • 2021-08-21
  • 2021-05-19
  • 2022-01-09
相关资源
相似解决方案