字典树是一般用来处理前缀问题的数据结构,从根出发向下走能找到字典中的词。每个节点包括两个数据,一个是标识词是否到达结尾,一个是标识子节点的map。
720.Longest Word in Dictionary
字典树的模板题
用链表 64 ms 32.1 MB
class Solution {
public:
struct Node{
bool is;
Node* vec[26];
Node(){
is=false;
for(int i=0;i<26;i++){
vec[i]=NULL;
}
}
};
Node* root;
void insert(string x){
Node* rr=root;
for(int i=0;i<x.length();i++){
if((rr->vec[x[i]-'a'])==NULL){
Node* temp=new Node;
rr->vec[x[i]-'a']=temp;
}
rr=rr->vec[x[i]-'a'];
}
rr->is=true;
}
bool search(string x){
Node *no=root;
for(int i=0;i<x.length();i++){
if((no->vec[x[i]-'a'])==NULL) return false;
if((no->vec[x[i]-'a']->is)==false) return false;
no=no->vec[x[i]-'a'];
}
return true;
}
string longestWord(vector<string>& words) {
if(words.size()==0) return "";
root=new Node;
for(auto x:words){
insert(x);
}
int ans=0;
int res=-1;
string ms="";
for(int i=0;i<words.size();i++){
string x=words[i];
if(search(x)&&ans<=x.length()){
bool f=false;
if(ans==x.length()){
for(int j=0;j<x.length();j++){
if(ms[j]==x[j]){
continue;
}if(ms[j]>x[j]){
f=true;
break;
}if(ms[j]<x[j]){
break;
}
}
}
if(f||ans<x.length()){
res=i;
ans=x.length();
ms=x;
}
}
}
if(res==-1) return "";
return words[res];
}
};
用二维数组 184 ms 30.2 MB
class Solution {
public:
int tree[100000][26];
unordered_set<int> e;
int tot=1;
void insert(string x){
int r=1;
for(int i=0;i<x.length();i++){
if(tree[r][x[i]-'a']==0){
tree[r][x[i]-'a']=++tot;
}
r=tree[r][x[i]-'a'];
}
e.insert(r);
}
bool search(string x){
int r=1;
for(int i=0;i<x.length();i++){
r=tree[r][x[i]-'a'];
if(r==0)
return false;
if(e.find(r)==e.end())
return false;
}
return true;
}
bool change(string ms,string s){
bool f=false;
for(int i=0;i<ms.length();i++){
if(ms[i]>s[i]){
f=true;
break;
}
if(ms[i]<s[i]){
break;
}
}
return f;
}
string longestWord(vector<string>& words) {
if(words.size()==0) return "";
for(auto x:words){
insert(x);
}
int ans=0;
int res=-1;
string ms="";
for(int i=0;i<words.size();i++){
string x=words[i];
if(search(x)&&ans<=x.length()){
bool f=false;
if(ans==x.length()){
f=change(ms,x);
}
if(f||ans<x.length()){
res=i;
ans=x.length();
ms=x;
}
}
}
if(res==-1) return "";
return words[res];
}
};
212.Word Search II
DFS+Trie
128 ms 36 MB
class Solution {
public:
struct Node{
bool isWord;
Node* arr[26];
Node(){
isWord=false;
for(int i=0;i<26;i++){
arr[i]=nullptr;
}
};
};
Node* root;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
vector<string> ans;
void add(string x){
Node* temp=root;
for(int i=0;i<x.length();i++){
if(temp->arr[x[i]-'a']==nullptr){
temp->arr[x[i]-'a']=new Node;
}
temp=temp->arr[x[i]-'a'];
}
temp->isWord=true;
}
void DFS(Node* t,string s,int i,int j,vector<vector<char>>& board){
char c=board[i][j];
board[i][j]=' ';
for(int k=0;k<4;k++){
int x=i+dir[k][0],y=j+dir[k][1];
if(x<0||y<0||x>=board.size()||y>=board[0].size()||board[x][y]==' ') continue;
if(t->arr[board[x][y]-'a']==nullptr) continue;
if(t->arr[board[x][y]-'a']->isWord) {
t->arr[board[x][y]-'a']->isWord=false;
ans.push_back(s+board[x][y]);
}
DFS(t->arr[board[x][y]-'a'],s+board[x][y],x,y,board);
}
board[i][j]=c;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
root=new Node;
for(int i=0;i<words.size();i++){
add(words[i]);
}
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
if(root->arr[board[i][j]-'a']==nullptr) continue;
string s="";
if(root->arr[board[i][j]-'a']->isWord) {
ans.push_back(s+board[i][j]);
root->arr[board[i][j]-'a']->isWord=false;
}
DFS(root->arr[board[i][j]-'a'],s+board[i][j],i,j,board);
}
}
return ans;
}
};