这一部分代码在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