AC解 - Phone List(HDOJ#1671) 前缀树的一个应用
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1671
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem 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:
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.
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
分析:
这题和"统计带某个前缀的单词数量 "(HDOJ#1251)类似,也是用前缀树。不过本题对内存的要求比较高(32768K),所以需要优化key值的存储方式,而不能简单地用String类型了。
由于字母表对应0..9共十个数字, 又有2^3 < 10 <
2^4,因此可以考虑用4个bits存储一个数字。但是计算机中最小的存储单元是字节,因此一个字节需要存储两个数字,同时还要考虑数字个数不为偶数的情
况,如果数字个数为奇数,那么有一个字节中另外4
bits没有用来存储数字,缺省时它的值等于0,但是0跟电话号码中的数字0又会混淆,解决办法是用0xF作为padding区分(因为数字0..9跟
0xF不相同)。因此设计这样的压缩存储方式:
1)如果一个Trie结点中数字个数为偶数,不需要padding。
2)如果结点中数字个数为奇数,那么在存储的第一个字节中前4位用来做padding,而且padding值为0xF。
代码:
import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** * * http://acm.hdu.edu.cn/showproblem.php?pid=1671 * * @author ljs * 2011-06 * */ public class Main { private class PatriciaTrieNode { private byte[] key; //1111-xxxx-xxxx-xxxx(e.g. 234) or xxxx-xxxx-xxxx-xxxx(e.g. 1234) private List<PatriciaTrieNode> children = new LinkedList<PatriciaTrieNode>(); //use "#" for terminal char private boolean terminal; public PatriciaTrieNode(){ key = new byte[0]; } public PatriciaTrieNode(byte[] key){ this.key = packing(key); } //parameter key is in unpacked form; //return the packed form private byte[] packing(byte[] key){ byte[] result = null; if(key.length %2 == 0){ //no padding result = new byte[key.length/2]; for(int i=0,j=0;i<key.length;i+=2,j++){ result[j] = (byte)((((key[i] & 0x0F) << 4) + (key[i+1] & 0x0F)) & 0x0FF); } }else{ //with padding result = new byte[(key.length+1)/2]; result[0] = (byte)(((0x0F << 4) + (key[0] & 0x0F)) & 0x0FF); for(int i=1,j=1;i<key.length;i+=2,j++){ result[j] = (byte)((((key[i] & 0x0F) << 4) + (key[i+1] & 0x0F)) & 0x0FF); } } return result; } //parameter key is in packed form //return the unpacked form private byte[] unpacking(byte[] key){ boolean hasPadding = ((key[0] & 0x0F0) == 0x0F0);//1111-xxxx- byte[] result = null; if(hasPadding){ result = new byte[key.length*2 - 1]; result[0] = (byte)(key[0] & 0x0F); for(int i=1,j=1;i<key.length;i++,j+=2){ result[j] = (byte)(((key[i] & 0x0F0) >> 4) & 0x0F); result[j+1] = (byte)(key[i] & 0x0F); } }else{ result = new byte[key.length*2]; for(int i=0,j=0;i<key.length;i++,j+=2){ result[j] = (byte)(((key[i] & 0x0F0) >> 4) & 0x0F); result[j+1] = (byte)(key[i] & 0x0F); } } return result; } //get the number of real keys (when in unpacked form) public int getKeyLength(){ int childkeyLen = key.length*2; if((key[0] & 0x0F0) == 0x0F0){ childkeyLen--; } return childkeyLen; } //return the real key at position idx public byte getKey(int idx){ if((key[0] & 0x0F0) != 0x0F0){ if(idx%2 == 0) return (byte)(((key[idx/2] & 0x0F0) >> 4) & 0x0F); else return (byte)(key[idx/2] & 0x0F); }else{ if(idx%2 == 0) return (byte)(key[(idx+1)/2] & 0x0F); else return (byte)(((key[(idx+1)/2] & 0x0F0) >> 4) & 0x0F); } } //return the suffix(right) in unpacked form public byte[] splitKeyAt(int idx){ byte[] temp = this.unpacking(this.key); byte[] left = new byte[idx]; byte[] right = new byte[temp.length-idx]; System.arraycopy(temp, 0, left, 0, idx); System.arraycopy(temp, idx, right, 0, temp.length-idx); this.key = this.packing(left); return right; } public String toString(){ byte[] result = this.unpacking(key); StringBuilder sb = new StringBuilder(); for(int k=0;k<result.length;k++){ sb.append(String.valueOf(result[k])); } return sb.toString() + (this.terminal?"#":"") + "(" + children.size() +")"; } } private PatriciaTrieNode root; private void insert(PatriciaTrieNode currNode,byte[] key) throws Exception{ boolean done = false; for(int i=0;i<currNode.children.size();i++){ PatriciaTrieNode child = currNode.children.get(i); int childKeyLength = child.getKeyLength(); int len = childKeyLength<key.length?childKeyLength: key.length; int j = 0; for(;j<len;j++){ if(key[j] != child.getKey(j)){ break; } } if(j==0){//this child doesn't match any character with the new key //order keys by lexi-order if(key[0]<child.getKey(0)){ //e.g. child="e" (currNode="abc") // abc abc // / \ =========> / | \ // e f insert "c" c# e f PatriciaTrieNode node = new PatriciaTrieNode(key); currNode.children.add(i,node); node.terminal = true; done = true; break; }else{ //key.charAt(0)>child.key.charAt(0) //don't forget to add the largest new key after iterating all children continue; } }else{//current child's key partially matches with the new key; 0<j<=len if(j==len){ if(key.length==childKeyLength){ if(child.terminal){ throw new Exception("Duplicate Key is found when insertion!"); }else{ //e.g. child="ab" // ab ab# // / \ =========> / \ // e f insert "ab" e f child.terminal = true; } }else if(key.length>childKeyLength){ //e.g. child="ab#" // ab# ab# // / \ ==========> / | \ // e f insert "abc" c# e f //String subkey = key.substring(j); byte[] subkey = new byte[key.length - j]; System.arraycopy(key, j, subkey, 0, key.length - j); //recursion insert(child,subkey); }else{ //key.length()<child.key.length() //e.g. child="abc#" // abc# ab# // / \ =========> / // e f insert "ab" c# // / \ // e f //String childSubkey = child.key.substring(j); //c byte[] childSubkey = child.splitKeyAt(j); PatriciaTrieNode subChildNode = new PatriciaTrieNode(childSubkey); subChildNode.terminal = child.terminal; subChildNode.children = child.children; //inherited from parent //child.key = key; //ab child.terminal = true; //ab# child.children = new LinkedList<PatriciaTrieNode>(); child.children.add(subChildNode); } }else{//0<j<len //e.g. child="abc#" // abc# ab // / \ ==========> / \ // e f insert "abd" c# d# // / \ // e f //split at j //String childSubkey = child.key.substring(j); //c //String subkey = key.substring(j); //d byte[] childSubkey = child.splitKeyAt(j); byte[] subkey = new byte[key.length - j]; System.arraycopy(key, j, subkey, 0, key.length - j); PatriciaTrieNode subChildNode = new PatriciaTrieNode(childSubkey); subChildNode.terminal = child.terminal; subChildNode.children = child.children; //inherited from parent //update child's key //child.key = child.key.substring(0,j); //child is not terminal now due to split, it is inherited by subChildNode child.terminal = false; //Note: no need to merge subChildNode PatriciaTrieNode node = new PatriciaTrieNode(subkey); node.terminal = true; child.children = new LinkedList<PatriciaTrieNode>(); if(subkey[0]<childSubkey[0]){ child.children.add(node); child.children.add(subChildNode); }else{ child.children.add(subChildNode); child.children.add(node); } } done = true; break; } } if(!done){ PatriciaTrieNode node = new PatriciaTrieNode(key); node.terminal = true; currNode.children.add(node); } } public void insert(byte[] key) throws Exception{ if(key == null || key.length == 0) return; if(root==null){ root = new PatriciaTrieNode(); } insert(root,key); } private boolean traverse(PatriciaTrieNode currNode){ boolean isConsistent = true; for(int i=0;i<currNode.children.size();i++){ PatriciaTrieNode child = currNode.children.get(i); if(child.terminal){ if(child.children.size()>0){ isConsistent = false; break; } }else{ isConsistent = traverse(child); if(!isConsistent){ break; } } } return isConsistent; } public boolean traverse(){ return traverse(this.root); } //for test purpose only public void printTree(){ this.print(0, this.root); } private void print(int level, PatriciaTrieNode node){ for (int i = 0; i < level; i++) { System.out.format(" "); } System.out.format("|"); for (int i = 0; i < level; i++) { System.out.format("-"); } StringBuilder sb = new StringBuilder(); if(node.key.length>0){ byte[] result = node.unpacking(node.key); for(int k=0;k<result.length;k++){ sb.append(String.valueOf(result[k])); } } if (node.terminal) System.out.format("%s#%n", sb.toString()); else System.out.format("%s%n", sb.toString()); for (PatriciaTrieNode child : node.children) { print(level + 1, child); } } public static void main(String[] args) { Scanner cin = new Scanner(System.in); String line = cin.nextLine(); line = line.trim(); int testCasesNum = Integer.parseInt(line); String[][] testCases = new String[testCasesNum][]; for (int i = 0; i < testCasesNum; i++) { line = cin.nextLine(); line = line.trim(); int phoneNum = Integer.parseInt(line); String[] phones =new String[phoneNum]; for(int j=0;j<phoneNum;j++){ line = cin.nextLine(); line = line.trim(); phones[j] = line; } testCases[i] = phones; } for(int i=0;i<testCasesNum;i++){ String[] phones = testCases[i]; Main main = new Main(); for (int j = 0; j < phones.length; j++) { try { byte[] phoneArr = new byte[phones[j].length()]; for(int k=0;k<phones[j].length();k++){ phoneArr[k] = (byte)(phones[j].charAt(k)-'0'); } main.insert(phoneArr); } catch (Exception e) { } } //main.printTree(); boolean consistent = main.traverse(); if(consistent){ System.out.println("YES"); }else{ System.out.println("NO"); } } } }
#END#