POJ 3630 Phone List

来源:互联网 发布:淘宝卖家诈骗800 编辑:程序博客网 时间:2024/05/19 03:28
http://poj.org/problem?id=3630
Phone List
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 23794Accepted: 7314

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

2391197625999911254265113123401234401234598346

Sample Output

NOYES

Source

Nordic 2007
题目大意就是给你n个字符串,如果某个字符串是另外的字符串的前缀就输出NO 否者YES。PS;要不就打错电话了
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
int t,n;
struct node
{
int next[11];
int v;
void init()
{
v=0;
memset(next,-1,sizeof next);
}
};
node L[1000500];
int tot=0;
int add(char s[],int len)
{
int mark=0;
int now=0;
for(int i=0;i<len;i++)
{
int t=s[i]-'0';
int next=L[now].next[t];
if(next==-1)
{
mark=1;
if(L[now].v==1) return 0;
next=++tot;
L[next].init();
L[now].next[t]=next;
}
now=next;
}
if(!mark)return 0;
L[now].v=1;

return 1;
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
scanf("%d",&t);
char s[20];
while(t--)
{
L[0].v=0;
L[0].init();
int i,flag=0;
scanf("%d",&n);
for( i=1;i<=n;i++)
{
scanf("%s",s);
if(add(s,strlen(s)))continue;
else flag=1;
}
if(flag)printf("NO\n");
else printf("YES\n");
}
return 0;
}

0 0
原创粉丝点击