Position: Home page » Ethereum » Ethereum trie tree coding

Ethereum trie tree coding

Publish: 2021-04-30 03:59:22
1. Save that the Holy Ghost in every city with to me, saying: that bands and conflicts wait for me at Jerusalem.
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]:
2. The spelling of sow in Old English
3. Trie tree is character tree, and its core idea is space for time. The information I used before is very little about this dictionary tree on the Internet.
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.
4. Come on, complexity advantage
5. Also known as the word search tree, trie tree, is a tree structure, is a variant of hash tree. The typical application is used for statistics, sorting and saving a large number of strings (but not limited to strings), so it is often used for text word frequency statistics by search engine system.
6. There are three methods:
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.
7. Properties:
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.
8.

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 of

can 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

9. Regardless of the extreme situation, generally it is. At the design level, each node in the chain trie uses a linked list to manage its next level sub nodes. For example, suppose that a trie stores three words, baby, badge and infant. The node that stores character a will have three sub nodes to store characters B, D and C respectively, and their pointers will be stored in the linked list. When we need to query, the node that stores character a will have three sub nodes, Every time you have to traverse the entire list to determine whether the character you are looking for exists or not. This time is proportional to the length of the list. And double array list, in the design to ensure that you go to query the next character can be found in one operation
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.
10. The only way to play is to lower the effect. It's better to exceed the down frequency
to be honest, 4-core 2G is much better than yours.
Hot content
Inn digger Publish: 2021-05-29 20:04:36 Views: 341
Purchase of virtual currency in trust contract dispute Publish: 2021-05-29 20:04:33 Views: 942
Blockchain trust machine Publish: 2021-05-29 20:04:26 Views: 720
Brief introduction of ant mine Publish: 2021-05-29 20:04:25 Views: 848
Will digital currency open in November Publish: 2021-05-29 19:56:16 Views: 861
Global digital currency asset exchange Publish: 2021-05-29 19:54:29 Views: 603
Mining chip machine S11 Publish: 2021-05-29 19:54:26 Views: 945
Ethereum algorithm Sha3 Publish: 2021-05-29 19:52:40 Views: 643
Talking about blockchain is not reliable Publish: 2021-05-29 19:52:26 Views: 754
Mining machine node query Publish: 2021-05-29 19:36:37 Views: 750