Ethereum trie tree coding
I only know that the Holy Ghost in every city points out to me that chains and tribulations are waiting for me<
witness, witness, witness θ]
suf.
[after a cardinal word ending with a vowel y, it forms an ordinal number]:
- eth2 [i] θ]
suf.
[archaism, poetry] [attached to the verb, form a declarative mood, the third person singular present tense]:
I will give you 100000 words no more than 10 in length. For each word, we need to judge whether it appears or not. If it appears, what position does it appear for the first time< Of course, hash can be used to solve this problem, but what I want to introce is trie tree. In some ways, it's more useful. For example, for a word, I would like to ask if its prefix has ever appeared. In this way, hash is not easy to do, and trie is still very simple
now back to the example, if we use the stupidest method, for each word, we have to find whether it is in the word before it. So the complexity of this algorithm is O (n ^ 2). Obviously, it's hard to accept the 100000 range. Now let's think about it in a different way. Suppose the word I want to query is ABCD, then in the words before him, I obviously don't have to consider the words that start with B, C, D, F and so on. And just find out if there is ABCD in a that starts with a. Similarly, in the words beginning with a, we only need to consider the tree model with B as the second letter...
assuming that there are six words B, ABC, abd, BCD, ABCD, EFG and HII, the tree we built is like this
for each node, the process of traversing from root to node is a word. If the node is marked red, it means that the word exists, otherwise it does not exist
then, for a word, I just need to follow it from the heel to the corresponding node, and then see whether the node is marked in red to know whether it has appeared. Marking this node red is equivalent to inserting the word
in this way, we can complete the inquiry and insertion together, and the time is only the length of words. In this example, it is 10
we can see that the number of nodes in each layer of trie tree is 26 ^ I. So to save space. We use dynamic linked list or array to simulate dynamic. The cost of space will not exceed the number of words × Word length
the program is very easy to implement. It's just a few lines, so I won't write it. Let's figure it out for myself
if you still don't understand, please leave a message<
the following provides a scale to find whether a word is in a given Dictionary:
program trie< br />type
rec=record
Got:boolean;< br /> next:array [' a'.. 39; z'] of Longint;< br /> end;< br />var
n,i,j,Now,Tn:Longint;< br /> s:string;< br /> T:array[1..1000] of rec;< br /> flag:boolean; < br />begin
Readln(n);< br /> Tn:=1;< br /> T[1].Got:=False;< br /> fillchar(T[1].next,sizeof(T[1].next),0);< br /> for i:=1 to n do
begin
readln(s);< br /> Now:=1;< br /> for j:=1 to length(s) do
if T[now].Next[s[j]]<& gt; 0 then now:=t[now].next[s[j]] else
begin
Inc(Tn);< br /> T[tn].Got:=false;< br /> fillchar(T[tn].next,sizeof(T[tn].next),0);< br /> T[Now].next[s[j]]:=Tn;< br /> Now:=Tn;< br /> end;< br /> T[now].Got:=true;< br /> end;< br /> readln(s);< br /> while s<& gt; 39; exit' do
begin
Now:=1; flag:=true;< br /> for j:=1 to length(s) do
if T[now].Next[s[j]]<& gt; 0 then now:=t[now].next[s[j]] else
begin
flag:=false;< br /> break;< br /> end;< br /> if flag then
if T[now].Got=false then flag:=false;< br /> if flag then writeln(' the word is in the tree') else
writeln(' can' 39; t find it! 39;);< br /> Readln(s);< br /> end;
end.
a word prefix tree problem, but I used the simplified version of trie tree + BM algorithm to do
password decoding
[problem description]
e to the busy homework recently, Tim forgot his computer password. Fortunately, Tim used a very special method to record the password when designing the computer password. This method is: Tim recorded the password and some other fake passwords in a book. In order to find out the correct password from these strings, Tim wrote a very long string in another book, and the correct password is the one that appears most frequently in this string. For example, the string Ababa, if the password is ABAB and ABA, then the correct password is ABA, because ABA appears twice in this string
now that you have Tim's two books, I hope you can write a program to help Tim find the correct password
[input]
input consists of two parts. The first part is composed of several lines, each line records a password, the length of the password is less than or equal to 255 bits, and is composed of lowercase letters. Then there's an empty line, and the second part records a very long string that ends with ".", which contains only lowercase letters
[output]
the output file name is pass.out. The output file consists of only one line and is an integer indicating the number of times the correct password appears in the string. If the number of occurrences is 0, output "no find"
[example]:
pass. In pass. Out
ab6
ABC
BDC
ABCD
.
program pass< br />const
filein=' pass.in';< br /> fileout=' pass.out';< br />type
rec=record
which:Longint; < br /> N ext:array [' a'.. 39; z'] of Longint;< br /> end;< br />var
o,now,i,Tn,Dn,temp,Ans:Longint;< br /> s:string;< br /> c:char;< br /> T:array[1..1000000] of REc;< br /> data:array [1..5000] of string;< br /> dL ong:array [1..5000] of longint;< br /> use:array [1..5000] of boolean;< br /> d:array[1..3000000] of char;< br /> A ppear:array [' a'.. 39; z'] of Longint;< br /> L ong:Longint; < br /> f:boolean;< br />function Compare(x:Longint):Longint;< br />var
s,i,Now,L, temp:Longint; < br />begin
s:=0;< br /> fillchar(appear,sizeof(appear),0);< br /> L:=length(data[x]);< br /> for i:=1 to L do
Appear[data[x][i]]:=i;< br /> Now:=L;< br /> while NOw<= Long do
begin
if D[now]<& gt; data[x][L] then Inc(now,L-Appear[D[now]]) else
begin
temp:=L-1;< br /> while (temp> 0) and (Data[x][temp]=d[Now-(L-temp)]) do dec(temp);< br /> if temp=0 then Inc(s);< br /> Inc(Now);< br /> end;< br /> end;< br /> Compare:=S;< br />end;< br />procere sort(l,r:Longint);< br />var
i,j,x:Longint;< br /> sy:string;< br /> ly:Longint;< br />begin
i:=l; j:=r; x:=dLong[(l+r) div 2];< br /> repeat
while dLong[i]< x do inc(i);< br /> while dlong[j]> x do dec(j);< br /> if i<= j then
begin
sy:=data[i];< br /> data[i]:=data[j];< br /> data[j]:=sy;< br /> ly:=dlong[i];< br /> dlong[i]:=dlong[j];< br /> dlong[j]:=ly;< br /> inc(i);< br /> dec(j);< br /> end;< br /> until i> j;< br /> if i< r then sort(i,r);< br /> if j> l then sort(l,j);< br />end;< br />begin
fillchar(use,sizeof(use),true);< br /> fillchar(t,sizeof(t),0);< br /> Assign(input,filein);< br /> Assign(output,fileout);< br /> rewrite(output);< br /> reset(input);< br /> tn:=1;< br /> readln(s);< br /> Dn:=0;< br /> while s<& gt; 39; 39; do
begin
Inc(dn);< br /> data[dn]:=s;< br /> dLong[dn]:=length(s);< br /> readln(s);< br /> end;< br /> sort(1,Dn);< br /> for o:=1 to Dn do
begin
s:=data[o];< br /> NOw:=1;< br /> f:=true;< br /> for i:=1 to Length(s) do
if t[now].Next[s[i]]<& gt; 0 then
begin
Now:=t[now].next[s[i]];< br /> if t[now].which<& gt; 0 then
begin
f:=false;< br /> break
end;< br /> end else
begin
Inc(tn);< br /> t[now].next[s[i]]:=tn;< br /> now:=tn;< br /> end;< br /> if f then t[now].which:=o;< br /> if not f then use[o]:=false;< br /> end;< br /> Long:=0;< br /> repeat
read(c);< br /> if c<& gt; 39;. 39; then
begin
Inc(Long);< br /> d[Long]:=c;< br /> end;< br /> until c='. 39;;< br /> for i:=1 to Dn do
begin
if use[i] then
begin
temp:=Compare(i);< br /> if temp> ans then ans:=temp;< br /> end;< br /> end;< br /> if ans=0 then writeln(' No find') else
writeln(Ans);< br /> close(input);< br /> close(output);< br />end.
regular expression, re mole in Python, and python comes with it
pyquery, which needs to be installed separately
Beautiful soup, which needs to be installed separately
for more complex acquisition, the latter two are more convenient to operate, and the former is more efficient.
1. The root node does not contain characters, and every node except the root node contains only one character
2. From the root node to a node, the characters on the path are connected to form the corresponding string of the node
3. All sub nodes of each node contain different characters
advantages:
1. Fast query. For the key value of length m, it only takes O (m) time in the worst case; But BST needs o (M log n) time
2. When storing a large number of strings, trie consumes less space. Because key values are not explicitly stored, they share substrings with other key values
operation:
1. Initialize or empty: traverse trie, delete all nodes and keep only the root node.
Trie, also known as dictionary tree, is an important data structure and the basis of AC automata. Therefore, let's briefly describe the number of dictionaries and list the operations on trie. The form of trie is shown in the following figure:
for each node, the process from root traversal to trie is a word. If the node is marked in red, it means that the word exists, otherwise it does not exist
then, for a word, I just need to follow it from the heel to the corresponding node, and then see whether the node is marked in red to know whether it has appeared. Marking this node red is equivalent to inserting the word
in this way, we can complete the inquiry and insertion together, and the time is only the word length. In this example, it is 10
we can see that the number of nodes in each layer of trie tree is 26 ^ I. So to save space. We use dynamic linked list or array to simulate dynamic. The cost of space will not exceed the number of words × Word length
The basic properties ofcan be summarized as follows:
1. The root node does not contain characters, and every node except the root node contains only one character
2. From the root node to a node, the characters on the path are connected to be the corresponding string of the node
3. All the sub nodes of each node contain different characters
we can use dynamic storage and static array simulation, for these two cases we use poj2001 and poj3630 to explain

in fact, there are two ways to manage the next level nodes in the conventional trie tree: linked list and array. The advantage of linked list is high space utilization, but slow query speed. The advantage of array is fast query speed, but low space utilization. Double array trie can be said to take into account the advantages of both, avoid the disadvantages of both, the cost is: complex implementation.
to be honest, 4-core 2G is much better than yours.
