|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace String; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// A class representing the bor data structure |
| 7 | +/// The bor can store Latin alphabet characters, numbers |
| 8 | +/// </summary> |
| 9 | +public class Bor |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// // A class representing the bor data structure |
| 13 | + /// </summary> |
| 14 | + private class Node |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Array of vertices to move from one vertex to another |
| 18 | + /// The bor can store Latin alphabet characters, numbers |
| 19 | + /// </summary> |
| 20 | + public Node?[] next = new Node['z']; |
| 21 | + |
| 22 | + // A field for storing information about whether a character is the end of a string |
| 23 | + public bool isTerminal { get; set; } |
| 24 | + |
| 25 | + // A field for storing information about the number of strings containing a certain prefix |
| 26 | + public int numberOfLinesContainingThePrefix { get; set; } |
| 27 | + } |
| 28 | + |
| 29 | + private Node root = new Node(); |
| 30 | + |
| 31 | + // Bor size |
| 32 | + private int size; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Function for adding a string |
| 36 | + /// </summary> |
| 37 | + /// <param name="element"> Element to add </param> |
| 38 | + /// <returns> Was there an element string before calling Add </returns> |
| 39 | + public bool Add(string element) |
| 40 | + { |
| 41 | + Node? node = root; |
| 42 | + int counter = 0; |
| 43 | + for(int i = 0; i < element.Length; i++) |
| 44 | + { |
| 45 | + if(element[i] > 'z') |
| 46 | + { |
| 47 | + throw new Exception("The bor can store Latin alphabet characters, numbers"); ; |
| 48 | + } |
| 49 | + if (node != null && node.next[element[i]] == null) |
| 50 | + { |
| 51 | + node.next[element[i]] = new Node(); |
| 52 | + size++; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + counter++; |
| 57 | + } |
| 58 | + if (node != null) |
| 59 | + { |
| 60 | + node = node.next[element[i]]; |
| 61 | + if(node != null) |
| 62 | + { |
| 63 | + node.numberOfLinesContainingThePrefix++; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + if (counter == element.Length && node != null && node.isTerminal) |
| 68 | + { |
| 69 | + Node? newNode = root; |
| 70 | + for (int i = 0; i < element.Length; i++) |
| 71 | + { |
| 72 | + newNode = newNode?.next[element[i]]; |
| 73 | + if(newNode != null) |
| 74 | + { |
| 75 | + newNode.numberOfLinesContainingThePrefix--; |
| 76 | + } |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + if (node != null) |
| 81 | + { |
| 82 | + node.isTerminal = true; |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Is the string contained in the Bor |
| 89 | + /// </summary> |
| 90 | + /// <param name="element"> Element to search </param> |
| 91 | + /// <returns> True if there is such a string. False if there is no such string </returns> |
| 92 | + public bool Contains(string element) |
| 93 | + { |
| 94 | + Node? node = root; |
| 95 | + for (int i = 0; i < element.Length; i++) |
| 96 | + { |
| 97 | + node = node.next[element[i]]; |
| 98 | + if (node == null) |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + return node.isTerminal; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Function for finding the number of strings starting with a prefix |
| 108 | + /// </summary>Количество строк содеражащих префикс |
| 109 | + /// <returns> The number of strings starting with the prefix </returns> |
| 110 | + public int HowManyStartWithPrefix(string prefix) |
| 111 | + { |
| 112 | + Node? node = root; |
| 113 | + for (int i = 0; i < prefix.Length;i++) |
| 114 | + { |
| 115 | + if (node?.next[prefix[i]] != null) |
| 116 | + { |
| 117 | + node = node.next[prefix[i]]; |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + return 0; |
| 122 | + } |
| 123 | + } |
| 124 | + return node == null ? 0 : node.numberOfLinesContainingThePrefix; |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Function for deleting string from a Bor |
| 129 | + /// </summary> |
| 130 | + /// <param name="element"> Element to delete </param> |
| 131 | + /// <returns> as there an element string before calling Remove </returns> |
| 132 | + public bool Remove(string element) |
| 133 | + { |
| 134 | + if(!this.Contains(element)) |
| 135 | + { |
| 136 | + return false; |
| 137 | + } |
| 138 | + Node? node = root; |
| 139 | + Node? nodeWithMaxPrefix = root; |
| 140 | + for (int i = 0; i < element.Length; i++) |
| 141 | + { |
| 142 | + node = node?.next[element[i]]; |
| 143 | + if(node != null) |
| 144 | + { |
| 145 | + node.numberOfLinesContainingThePrefix--; |
| 146 | + } |
| 147 | + if (i == element.Length - 1) |
| 148 | + { |
| 149 | + nodeWithMaxPrefix.isTerminal = false; |
| 150 | + } |
| 151 | + } |
| 152 | + node = root; |
| 153 | + for (int i = 0; i < element.Length; i++) |
| 154 | + { |
| 155 | + nodeWithMaxPrefix = node; |
| 156 | + node = node?.next[element[i]]; |
| 157 | + if (nodeWithMaxPrefix?.next[element[i]]?.numberOfLinesContainingThePrefix == 0) |
| 158 | + { |
| 159 | + nodeWithMaxPrefix.next[element[i]] = null; |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Function for returning the number of elements |
| 168 | + /// </summary> |
| 169 | + /// <returns> Number of elements contained in the bor </returns> |
| 170 | + public int Size() |
| 171 | + { |
| 172 | + return this.size; |
| 173 | + } |
| 174 | + |
| 175 | + static void Main(string[] args) |
| 176 | + { |
| 177 | + return; |
| 178 | + } |
| 179 | +} |
0 commit comments