Left Fortune Law Section VII lesson: prefix tree, greedy strategy (string array splicing, cutting bullion, the cost of profit), the number of bits in the data stream, the venue hosted the largest number of lectures

1. What is the prefix tree? TrieTree

In computer science, trie, also known as the prefix tree or trie, is an ordered tree, save for associative array, where the keys are usually strings. Binary search tree with different keys are not stored directly in the node, but is determined by the position of the nodes in the tree. We have the same prefix all the descendants of a node, i.e. the node corresponding to the character string, and the root node corresponds to an empty string. Generally, not all of the nodes have a value corresponding to only part of the internal nodes and leaf nodes corresponding to the keys have associated values.

the trie keys are usually strings, but may be other configurations.

For example:

When there are four strings:. "Abc", "bce", "abd", "bef" in the figure below: is the edge of characters, numbers in the tree is the end of the current character string how many. For example, the addition of "bef", f to the number after the 0 -> 1, then add the "BE", the value 'e' becomes 0 at a 1 to a; requirements: the number of string " be "as a prefix? Directly to "be" at the 'e' can be digital.

 

 

 

 

 

 

 

 

  1  / * 
  2  * prefix tree, see the two array elements string;
   . 3   * / 
  . 4  public  class Code_01_TrieTree {
   . 5  
  . 6      public  static  class TrieNode {
   . 7          public  int path; // number of passes of the node 
  . 8          public  int End; / / number of nodes to the end of the string; 
  . 9          public TrieNode [] nextS;
 10          public TrieNode () {
 . 11              path = 0 ;
 12 is              end = 0 ;
 13 is              nextS = new newTrieNode [26 is]; // 26 is the next letter in the subscript 0--25 
14          }
 15      }
 16      
. 17      public  static  class Trie {
 18 is          Private TrieNode the root; // root 
. 19          public Trie () {
 20 is              the root = new new TrieNode () ;
 21 is          }
 22 is          
23 is          public  void INSERT (String Word) {
 24              IF (Word == null ) {
 25                  return ;
 26 is              }
 27              char[] chs = word.toCharArray();
 28             TrieNode node = root;
 29             int index = 0;
 30             for (int i = 0; i < chs.length; i++) {
 31                 index = chs[i]-'a';//将字母对应成下标
 32                 if (node.nexts[index] == null) {
 33                     node.nexts[index] = new TrieNode();
 34                 }
 35                 node = node.nexts[index];
 36                 node.path++;
 37             }
 38             node.end++;    
 39         }
 40         
 41         public void delete(String word) {
 42             if (search(word) != 0) {
 43                 char[] chs = word.toCharArray();
 44                 int index = 0;
 45                 TrieNode node = root;
 46                 for (int i = 0; i < chs.length; i++) {
 47                     index = chs[i] - 'a';
 48                     if (--node.nexts[index].path == 0) {
 49                         node.nexts[index] = null;
 50                         return;
 51                     }
 52                     node = node.nexts[index];
 53                 }
 54                 node.end--;
 55             }
 56         }
 57         
 58         public int search(String word) {
 59             if (word == null) {
 60                 return 0;
 61             }
 62             char[] chs = word.toCharArray();
 63             int index = 0;
 64             TrieNode node = root;
 65             for (int i = 0; i < chs.length; i++) {
 66                 index = chs[i] - 'a';
 67                 if (node.nexts[index] == null) {
 68                     return 0;
 69                 }
 70                 node = node.nexts[index];
 71             }
 72             return node.end;            
 73         }
 74     }
 75     
 76     
 77     
 78     public static void main(String[] args) {
 79         test();
 80 
 81     }
 82 
 83 
 84 
 85     private static void test() {
 86         Trie trie = new Trie();
 87         System.out.println(trie.search("zuo"));
 88         trie.insert("zuo");
 89         System.out.println(trie.search("zuo"));
 90         trie.delete("zuo");
 91         System.out.println(trie.search("zuo"));
 92         trie.insert("zuo");
 93         trie.insert("zuo");
 94         trie.delete("zuo");
 95         System.out.println(trie.search("zuo"));
 96         trie.delete("zuo");
 97         System.out.println(trie.search("zuo"));
 98         trie.insert("zuoa");
 99         trie.insert("zuoac");
100         trie.insert("zuoab");
101         trie.insert("zuoad");
102         trie.delete("zuoa");
103         System.out.println(trie.search("zuoa"));
104         System.out.println(trie.search("zuoac"));
105         System.out.println(trie.search("zuoab"));
106         System.out.println(trie.search("zuoad"));
107     }
108 
109 }

2. splicing array of strings - greedy strategy

(Measured by the number of samples is small, do not tangle proof)

An array of strings, each string concatenation, so that the lowest lexicographic order. Such as: "ab", "cd", "ef", spliced ​​into "abcdef" minimal, other than the way it big. Here may think of the sort, and then stitching, there is a case, "b" and "ba" stitching, after ordering a "b", "ba", after the stitching is "b ba", but according to "b ab" mosaic of words , smaller, just so the string is wrong splicing dictionaries sorted. So ordering policy needs to change.

To str1, str2 Sort:

The original mosaic strategy: str1 <= str2, str1 before release, or put before str2;

The improved splicing strategy: str1 + str2 <str2 + before = str1, str1 put, or put before str2;

proving process:

Proof of delivery ordering give the: ab b represents a post-splicing, the splicing can be seen at a high level, b is in the low, ab is moved to the left represents a digit length of b plus b. On ab can be regarded as k-ary spliced, i.e. K * A B length + b, so that

K B length = m (b), so that ab = a * m (b) + b;

 

Oswald

 

Guess you like

Origin www.cnblogs.com/gjmhome/p/11354034.html