POJ 3630 Phone List
来源:互联网 发布:淘宝卖家诈骗800 编辑:程序博客网 时间:2024/05/19 03:28
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
#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;
}
- POJ 3630 Phone List
- POJ 3630 Phone List
- poj 3630 Phone List
- poj 3630 Phone List
- POJ 3630 Phone List
- Poj 3630 Phone List
- POJ 3630 Phone List
- poj 3630 Phone List
- poj 3630 phone list
- poj 3630 Phone List
- POJ-3630 Phone List
- POJ 3630 Phone List
- poj 3630(phone list)
- poj 3630 Phone List
- POJ 3630 Phone List
- POJ 3630 Phone List
- POJ【3630】Phone List
- POJ 3630 Phone List
- POJ 3984 迷宫问题
- HDU 2612 Find a way
- 字典树(trie 树)
- intellij idea15 破解
- UVa 11732 - strcmp() Anyone?
- POJ 3630 Phone List
- HDU 1251 统计难题
- 报名|「OneAPM x DaoCloud」技术公开课:Docker性能监控!
- HDU 1247 Hat’s Words
- AC自动机 白书模板
- HDU 2896 病毒侵袭
- HDU 3065 病毒侵袭持续中
- 【转载】直线分平面
- Infinite Expressions for Pi