直接一个单词一个单词的傻搜肯定会超时,所以先将vector<string> 存入前缀树中,然后每个点在前缀树中搜索,这样能大大剪枝叶。
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct Node{
int id;
Node* next[26];
Node(){
id = -1;
for(int i=0;i<26;i++) next[i] = NULL;
}
};
class Solution {
public:
Node* root = new Node();
vector<vector<bool>> st;
vector<string> res;
unordered_set<string> hashset;
void insert(string word, int id) {
auto p = root;
for(char c:word){
if(p->next[c-'a']==NULL){
p->next[c-'a'] = new Node();