Description
Given a list of phone numbers, determine if it is consistent in the sense of this no number is the prefix of another. Let ' s say the phone catalogue listed numbers:emergency 911 Alice 625 999 Bob 91 12 54 26
In this case, it's not possible to call Bob, because the "would direct your call to" emergency line as soon as Y Ou had dialled the three digits of Bob ' s phone number. So this list would is consistent.
Input
The A single integer, 1≤t≤40, the number of input gives, the number of test cases. Each test case is starts with N, the number of phone numbers, in 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
The main effect of the topic:
Give a few sets of phone numbers, in a group of numbers, if there is a number of other numbers prefix, then output no, otherwise output yes.
Trie Tree application Edge Insert Edge Search
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < queue> #include <vector> #include <cmath> #include <stack> #include <string> #include <map
> #include <set> #define PI ACOs ( -1.0) #define LL long #define ULL unsigned long long #define INF 0x3f3f3f3f #define INF 1e18 #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 #define DEBUG (a) printf ("---%d---\ n", A) #define MEM0 (a) memset (a, 0, sizeof (a)) #define MEMI (a) memset (a, INF, sizeof (a)) #define MEM1 (a) memset (a,-1, sizeof (
A) using namespace Std;
typedef pair<int, int> P;
Const double EPS = 1e-10;
const int MAXN = 1e6 + 5;
const int mod = 1E8;
struct Node {int flag;
node* NEXT[10];
}TREE[MAXN];
int node, OK;
node* creat () {node* p = &tree[node++];
P->flag = 0;
for (int i = 0; i < i++) p->next[i] = NULL;
return p; void Insert (node* root, char* s)//edge Insert edge look for {
node* p = root;
for (int i = 0; I < strlen (s); i++) {int k = s[i]-' 0 ';
if (p->next[k] && i = = strlen (s)-1) {//Before this judgment has a number its prefix for the current number OK = 1;
Return
} if (P->next[k]) {if (P->next[k]->flag) {//whether there is a number before this judgment he is the prefix of the current number OK = 1;
Return
} else P->next[k] = creat ();
p = p->next[k];
} P->flag = 1;
int main (void) {//Freopen ("C:\\users\\wave\\desktop\\null.exe\\null\\in.txt", "R", stdin);
int T, n, I;
Char s[15];
scanf ("%d", &t);
while (t--) {scanf ("%d", &n);
node = OK = 0;
node* root = creat ();
for (i = 1; I <= n; i++) {scanf ("%s", &s);
if (!ok) Insert (root, s);
if (OK) puts ("NO");
Else puts ("YES");
return 0; }