字典树,深度优先搜索

本文探讨了一段代码在LeetCode中关于前缀树(Trie)实现的改进,针对字符串查找和起始单词检测问题。作者分享了原始代码的问题,并展示了优化后的Trie类,提高了搜索效率。通过实例展示了如何使用新Trie结构解决找寻在给定矩阵中由连续字符组成的单词问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一部分代码在Leetcode上没有通过,然后又有了下述代码:

#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<unordered_map>

using namespace std;
class Trie {
private:
	bool isEnd;
	Trie* next[26];
public:
	Trie() {
		isEnd = false;
		memset(next, 0, sizeof(next));
	}

	void insert(string word) {
		Trie* node = this;
		for (char c : word) {
			if (node->next[c - 'a'] == NULL) {
				node->next[c - 'a'] = new Trie();
			}
			node = node->next[c - 'a'];
		}
		node->isEnd = true;
	}

	bool search(string word) {
		Trie* node = this;
		for (char c : word) {
			node = node->next[c - 'a'];
			if (node == NULL) {
				return false;
			}
		}
		return node->isEnd;
	}

	bool startsWith(string prefix) {
		Trie* node = this;
		for (char c : prefix) {
			node = node->next[c - 'a'];
			if (node == NULL) {
				return false;
			}
		}
		return true;
	}
};

//class Trie {
//public:
//	/** Initialize your data structure here. */
//	Trie() {
//
//	}
//
//	/** Inserts a word into the trie. */
//	void insert(string word) {
//		trie.emplace_back(word);
//	}
//
//	/** Returns if the word is in the trie. */
//	bool search(string word) {
//		for (int i = 0; i < trie.size(); i++) {
//			if (word == trie[i]) {
//				return true;
//			}
//		}
//		return false;
//	}
//
//	/** Returns if there is any word in the trie that starts with the given prefix. */
//	bool startsWith(string prefix) {
//		for (int i = 0; i < trie.size(); i++) {
//			if (trie[i].size() >= prefix.size()) {
//				int count = 0;
//				for (int j = 0; j < prefix.size(); j++) {
//					if (trie[i][j] == prefix[j]) {
//						count++;
//					}
//					else {
//						break;
//					}
//				}
//				if (count == prefix.size()) {
//					return true;
//				}
//			}
//		}
//		return false;
//	}
//public:
//	vector<string> trie;
//};

class Solution {
public:
	void stringGroup(vector<vector<char>> vecs, int i, int j, string str, vector<vector<bool>> &flag) {
		if (i < 0 || j < 0 || i >= vecs.size() || j >= vecs[0].size()) {
			return;
		}
		else {
			if (flag[i][j]) {
				return;
			}
			str += vecs[i][j];
			flag[i][j] = true;
			if (!trie->startsWith(str)) {
				return;
			}
			if (trie->search(str)) {
				result.emplace_back(str);
				//return;
			}
			if (i>0 && flag[i - 1][j] == false) {
				stringGroup(vecs, i - 1, j, str, flag);
				flag[i - 1][j] = false;
			}
			if (i< (vecs.size()-1) && flag[i + 1][j] == false) {
				stringGroup(vecs, i + 1, j, str, flag);
				flag[i + 1][j] = false;
			}
			
			if (j>0 && flag[i][j - 1] == false) {
				stringGroup(vecs, i, j - 1, str,flag);
				flag[i][j-1] = false;
			}
			if (j<(vecs[0].size()-1) && flag[i][j + 1] == false) {
				stringGroup(vecs, i, j + 1, str, flag);
				flag[i][j + 1] = false;
			}
		}
	}
	vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
		trie = new Trie();
		for (int i = 0; i < words.size(); i++) {
			trie->insert(words[i]);
		}
		for (int i = 0; i < board.size(); i++) {
			for (int j = 0; j < board[i].size(); j++) {
				vector<vector<bool>> flag(board.size(), vector<bool>(board[0].size()));
				stringGroup(board, i, j, "", flag);
			}
		}
		result.erase(unique(result.begin(), result.end()),result.end());
		return result;
	}
public:
	vector<string> result;
	Trie* trie = NULL;
};
int main()
{
	Solution sol;
	//vector<vector<char>> vecs = { {'a','b'},{'c','d'}};
	//vector<string> words = { "abcb" };
	vector<vector<char>> vecs = { { 'o','a','a','n'},{'e','t','a','e'},{'i','h','k','r'},{'i','f','l','v'} };
	vector<string> words = { "oath","pea","eat","rain","oathi","oathk","oathf","oate","oathii","oathfi","oathfii"};
	//vector<vector<char>> vecs = { { 'o','a','a','n'},{'e','t','a','e'},{'i','h','k','r'},{'i','f','l','v'} };
	//vector<string> words = { "oath","pea","eat","rain" };
	//vector<string> words = {"eat","oath", "pea"};
	//vector<vector<char>> vecs = { {'a','a'}};
	//vector<string> words = { "aa" };
	vector<string> result = sol.findWords(vecs, words);
	for (int i = 0; i < result.size(); i++) {
		cout << result[i] << " ";
	}
	//vector<bool> flag(5);
	//for (int i = 0; i < flag.size(); i++) {
	//	cout << flag[i] << " ";
	//}
	system("pause");
}

前缀树

https://blog.youkuaiyun.com/weixin_39778570/article/details/81990417

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值