ACM模板库

ACM模板库


trie树(Hash表+节时间耗空间)

<pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;cstring&gt; #include &lt;unordered_map&gt; using namespace std; struct Trie{ int exist; unordered_map&lt;int, Trie*&gt; child; Trie():exist(0){} }; int insert(Trie *t, const string &amp;str){ Trie *root = t; for(int i = 0; i &lt; str.size(); i++){ int place = str[i] - 'a'; if(root-&gt;child.find(place) != root-&gt;child.end()) root = root-&gt;child[place]; else{ root-&gt;child[place] = new Trie(); root = root-&gt;child[place]; } if(i == str.size() - 1){ root-&gt;exist++; return root-&gt;exist; } } }</code></pre>

页面列表

ITEM_HTML