Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23475 Accepted Submission(s): 7888
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.
网上给的题解是字典树 我用java实现了一遍 然后ac了 emm 别的就没有了 以下是代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int p = 0; p < t; p++) {
int n = scanner.nextInt();
Node root = new Node(0);
int q = 1;
for (int i = 0; i < n; i++) {
String str = scanner.next();
int index = 0;
Node k = root;
while (index < str.length() && q == 1) {
if (k.arr[str.charAt(index)-'0'] == null) {
k.arr[str.charAt(index)-'0'] = new Node(index == str.length()-1 ? 1 : 0);
k = k.arr[str.charAt(index)-'0'];
}else {
if (k.arr[str.charAt(index)-'0'].flag == 1 || index == str.length()-1) {
q = 0;
break;
}
k = k.arr[str.charAt(index)-'0'];
}
index ++;
}
}
System.out.println(q == 1 ? "YES":"NO");
}
}
}
class Node {
int flag;
Node [] arr;
public Node(int flag) {
arr = new Node[10];
this.flag = flag;
}
}