Пример #1
0
        void Recur(char[,] board, Trie tr, int curi, int curj, bool[,] map, int maxm, int maxn, List<string> re, string str)
        {
            if (curi >= maxm || curi < 0 || curj >= maxn || curj < 0 || map[curi, curj])
                return;

            string newstr = str + board[curi, curj];

            if (!tr.StartsWith(newstr))
                return;

            if (tr.Search(newstr) && !re.Contains(newstr))
            {
                re.Add(newstr);
            }

            map[curi, curj] = true;

            Recur(board, tr, curi + 1, curj, map, maxm, maxn, re, newstr);

            Recur(board, tr, curi - 1, curj, map, maxm, maxn, re, newstr);

            Recur(board, tr, curi, curj + 1, map, maxm, maxn, re, newstr);

            Recur(board, tr, curi, curj - 1, map, maxm, maxn, re, newstr);

            map[curi, curj] = false;

            return;
        }
Пример #2
0
        public static void Run()
        {
            Trie trie = new Trie();
            trie.Insert("somestring");
            Console.WriteLine(trie.Search("key"));
            trie.Insert("key1");
            Console.WriteLine(trie.Search("key"));
            Console.WriteLine(trie.Search("key1"));
            Console.WriteLine(trie.StartsWith("some"));

            trie.Insert("abc");
            Console.WriteLine(trie.Search("abc"));
            Console.WriteLine(trie.Search("ab"));
            trie.Insert("ab");
            Console.WriteLine(trie.Search("ab"));
            trie.Insert("ab");
            Console.WriteLine(trie.Search("ab"));
            
        }