POJ3630(Trie树)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26385 | Accepted: 7957 |
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
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
Sample Output
NO YES
静态建树,动态建树TLE.
#include"cstdio" #include"cstdlib" #include"cstring" using namespace std; const int MAXN=600005; const int N=10; bool flag; struct node{ bool val; node* next[N]; node() { val=false; for(int i=0;i<N;i++) next[i]=NULL; } }; node memory[MAXN]; int ant; node *root; void insert(char *s) { //?Xn?Yn??? node* p=root; for(int i=0;s[i];i++) { int k=s[i]-'0'; if(p->next[k]==NULL) p->next[k]=&memory[ant++];//now node(); p=p->next[k]; if(p->val==true)//Xn?Yn???? { flag=true; } } p->val=true; for(int i=0;i<N;i++) { if(p->next[i]!=NULL)//?Yn?Xn??????????next[i]??NULL { flag=true; break; } } } /* void del(node *p) { for(int i=0;i<N;i++) { if(p->next[i]!=NULL) { del(p->next[i]); } } delete p; } */ int main() { int T; scanf("%d",&T); for(int t=1;t<=T;t++) { ant=0;memset(memory,0,sizeof(memory)); flag=false; root=&memory[ant++];//new node(); int n;scanf("%d",&n); char phone[15]; while(n--) { scanf("%s",phone); if(!flag) insert(phone); } if(flag) printf("NO\n"); else printf("YES\n"); //del(root); } return 0; }
Java版:
import java.util.Scanner; class Node { boolean val; Node[] net = new Node[10]; } public class Main { Scanner in = new Scanner(System.in); int n; Node root; boolean insert(String str) { Node p = root; for(int i = 0, size = str.length(); i < size; ++i) { int k = str.charAt(i) - '0'; if(p.net[k] == null) { p.net[k] = new Node(); } else { Node q = p.net[k]; int time = 0; for(int j = 0; j < 10; ++j) { if(q.net[j] == null) { time++; } else break; } if(time == 10) { return true; } } p = p.net[k]; p.val = true; } for(int i = 0; i < 10; i++) { if(p.net[i] != null) { return true; } } return false; } public Main() { int T = in.nextInt(); String str; boolean tag; while(T-- != 0) { root = new Node(); n = in.nextInt(); tag = false; while(n-- != 0) { str = in.next(); if(tag) continue; if(insert(str)) { tag = true; } } if(tag) System.out.println("NO"); else System.out.println("YES"); } } public static void main(String[] args) { new Main(); } }