博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Phone List
阅读量:4660 次
发布时间:2019-06-09

本文共 2078 字,大约阅读时间需要 6 分钟。

Problem 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 911 97625999 91125426 5 113 12340 123440 12345 98346
 
Sample Output
NO YES
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char ll[10005][11];
struct lmx{
 struct lmx*next[10];
 bool flag;
};
lmx *root,*q,*p;
void insert(char *s)
{
 int i,j;
 p=root;
 for(i=0;i<strlen(s);i++)
 {
  int temp=s[i]-'0';
  if(p->next[temp]==NULL)
  {
   q=(lmx*)malloc(sizeof(lmx));
            for(j=0;j<10;j++)
   {
    q->next[j]=NULL;
   }
   q->flag=false;
   p->next[temp]=q;
   p=q;
  }
  else
  {
   p=p->next[temp];
  }
 }
 p->flag=true;
}
bool finder(char *s)
{
 p=root;
 int pos=0,i;
 for(i=0;i<strlen(s);i++)
 {
  int temp=s[i]-'0';
        p=p->next[temp];
  if(p->flag==true&&i<strlen(s)-1) {pos=1;break;}
 }
 if(pos==1) return true;
 else return false;
}
void delet(lmx *p)
{
 int i;
 for(i=0;i<10;i++)
 {
  if(p->next[i]!=NULL)  delet(p->next[i]);
 }
 delete p;
}
int main()
{
    int n,m,i,j,flg=0;
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  flg=0;
  root=(lmx*)malloc(sizeof(lmx));
  for(j=0;j<10;j++)  root->next[j]=NULL;
  root->flag=false;
  scanf("%d",&m);
        for(j=0;j<m;j++)
  {
   scanf("%s",ll[j]);
   insert(ll[j]);
  }
  for(j=0;j<m;j++)
  {
   if(finder(ll[j]))  {flg=1;break;}
  }
  if(flg==1)  puts("NO");
  else puts("YES");
  delet(root);
 }
 return 0;
}

转载于:https://www.cnblogs.com/ffhuguang/archive/2013/05/05/3060861.html

你可能感兴趣的文章
浅谈几种主要编程语言
查看>>
Linux tcpdump命令详解
查看>>
两个datagrid的数据移动(支持多选)
查看>>
HDU4826 Labyrinth
查看>>
jquery-layer
查看>>
JavaScript 基础
查看>>
iOS学习之六种传值方式
查看>>
EF 外键不显示、如何让外键显示!增、删、改 操作时,外键不显示,只显示导航属性!...
查看>>
美文共享
查看>>
Python面试题目之打乱打乱有序列表
查看>>
牛客网——小白鼠排队(桶排序)
查看>>
smtp 发送邮件实例
查看>>
java多线程系列
查看>>
GCD问题 洛谷P1372 又是毕业季I & P1414 又是毕业季II
查看>>
未在本地计算机上注册“Microsoft.Ace.OleDb.12.0”提供程序解决办法
查看>>
svn 安装与设置
查看>>
数据结构简单学习
查看>>
Oracle分页抽数存储过程
查看>>
paramiko 模块 ---- python2.7
查看>>
CentOS 安装与优化
查看>>