class Solution {
public:
struct node
{
bool is;
node *next[26];
};
vector<string>res;
vector<vector<char>>board;
node *root;
set<string> st;
int row;
int col;
void buildtrie(string s)
{
node *head=root;
int len=s.length();
for(int i=0;i<len;i++)
{
if(head->next[s[i]-'a']==NULL)
{
node *p=(node*)malloc(sizeof(node));
p->is=false;
for(int i=0;i<26;i++)
p->next[i]=NULL;
head->next[s[i]-'a']=p;
}
head=head->next[s[i]-'a'];
}
head->is=true;
}
bool find(string s)
{
int len=s.length();
if(len==0)
return false;
node *ptr=root;
int i=0;
while(i<len&&ptr->next[s[i]-'a'])
{
ptr=ptr->next[s[i]-'a'];
i++;
}
if(i==len&&ptr!=NULL&&ptr->is==true)
return true;
return false;
}
bool isprefix(string s)
{
int len=s.length();
if(len==0)
return true;
node *ptr=root;
int i=0;
while(i<len&&ptr->next[s[i]-'a'])
{
ptr=ptr->next[s[i]-'a'];
i++;
if(ptr==NULL)
break;
}
if(i==len)
return true;
return false;
}
void dfs(vector<vector<bool>> visit,string s,int x,int y)
{
int k[]={-1,0,1,0};
int l[]={0,-1,0,1};
visit[x][y]=true;
if(isprefix(s))
{
for(int i=0;i<4;i++)
{
int xx=x+k[i];
int yy=y+l[i];
if(xx>=0&&xx<row&&yy>=0&&yy<col&&visit[xx][yy]==false)
{
s.push_back(board[xx][yy]);
if(find(s)) st.insert(s);
if(isprefix(s))
dfs(visit,s,xx,yy);
s.pop_back();
}
}
}
}
vector<string> findWords(vector<vector<char>>& board1, vector<string>& words)
{
board=board1;
root=(node*)malloc(sizeof(node));
root->is=false;
for(int i=0;i<26;i++)
root->next[i]=NULL;
for(int i=0;i<words.size();i++)
buildtrie(words[i]);
row=board.size();
col=board[0].size();
vector<vector<bool>> visit(row,vector<bool>(col,false));
string s="";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
s.push_back(board[i][j]);
if(find(s)) st.insert(s);
if(isprefix(s))
dfs(visit,s,i,j);
s.pop_back();
}
}
set<string>::iterator it;
for( it=st.begin();it!=st.end();it++)
res.push_back(*it);
return res;
}
};
class Solution {
public:
struct node
{
bool is;
node *next[26];
};
vector<string>res;
vector<vector<char>>board;
node *root;
set<string> st;
int row;
int col;
void buildtrie(string s)
{
node *head=root;
int len=s.length();
for(int i=0;i<len;i++)
{
if(head->next[s[i]-'a']==NULL)
{
node *p=(node*)malloc(sizeof(node));
p->is=false;
for(int i=0;i<26;i++)
p->next[i]=NULL;
head->next[s[i]-'a']=p;
}
head=head->next[s[i]-'a'];
}
head->is=true;
}
bool find(string s)
{
int len=s.length();
if(len==0)
return false;
node *ptr=root;
int i=0;
while(i<len&&ptr->next[s[i]-'a'])
{
ptr=ptr->next[s[i]-'a'];
i++;
}
if(i==len&&ptr!=NULL&&ptr->is==true)
return true;
return false;
}
bool isprefix(string s)
{
int len=s.length();
if(len==0)
return true;
node *ptr=root;
int i=0;
while(i<len&&ptr->next[s[i]-'a'])
{
ptr=ptr->next[s[i]-'a'];
i++;
if(ptr==NULL)
break;
}
if(i==len)
return true;
return false;
}
void dfs(vector<vector<bool>> visit,string s,int x,int y)
{
int k[]={-1,0,1,0};
int l[]={0,-1,0,1};
visit[x][y]=true;
if(isprefix(s))
{
for(int i=0;i<4;i++)
{
int xx=x+k[i];
int yy=y+l[i];
if(xx>=0&&xx<row&&yy>=0&&yy<col&&visit[xx][yy]==false)
{
string tt=s+board[xx][yy];
//s.push_back(board[xx][yy]);
if(find(tt)) st.insert(tt);
if(isprefix(tt))
dfs(visit,s+board[xx][yy],xx,yy);
}
}
}
}
vector<string> findWords(vector<vector<char>>& board1, vector<string>& words)
{
board=board1;
root=(node*)malloc(sizeof(node));
root->is=false;
for(int i=0;i<26;i++)
root->next[i]=NULL;
for(int i=0;i<words.size();i++)
buildtrie(words[i]);
row=board.size();
col=board[0].size();
vector<vector<bool>> visit(row,vector<bool>(col,false));
string s="";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
s.push_back(board[i][j]);
if(find(s)) st.insert(s);
if(isprefix(s))
dfs(visit,s,i,j);
s.pop_back();
}
}
set<string>::iterator it;
for( it=st.begin();it!=st.end();it++)
res.push_back(*it);
return res;
}
};
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
leetcode代码200题c++ (202个子文件)
Word Search II.txt 4KB
Unique Binary Search Trees II .txt 3KB
Minimum Window Substring.txt 3KB
Copy List with Random Pointer .txt 2KB
Merge Intervals.txt 2KB
Best Time to Buy and Sell Stock IV .txt 2KB
Median of Two Sorted Arrays.txt 2KB
Restore IP Addresses .txt 2KB
Surrounded Regions .txt 2KB
Maximal Rectangle .txt 2KB
Insert Interval .txt 2KB
Basic Calculator .txt 2KB
Basic Calculator II.txt 2KB
Word Ladder II .txt 2KB
Populating Next Right Pointers in Each Node II .txt 2KB
Dungeon Game.txt 2KB
Flatten Binary Tree to Linked List.txt 1KB
Valid Sudoku .txt 1KB
Word Ladder .txt 1KB
Populating Next Right Pointers in Each Node .txt 1KB
Minimum Depth of Binary Tree .txt 1KB
Merge k Sorted Lists.txt 1KB
Summary Ranges .txt 1KB
Add and Search Word - Data structure design.txt 1KB
Implement Stack using Queues .txt 1KB
Different Ways to Add Parentheses.txt 1KB
Binary Tree Zigzag Level Order Traversal .txt 1KB
Maximum Gap.txt 1KB
Binary Tree Paths .txt 1KB
Clone Graph .txt 1KB
Next Permutation .txt 1KB
Wildcard Matching .txt 1KB
4Sum.txt 1KB
Set Matrix Zeroes .txt 1KB
The Skyline Problem .txt 1KB
Linked List Cycle II .txt 1KB
Validate Binary Search Tree .txt 1KB
Majority Element II .txt 1KB
Reverse Words in a String.txt 1019B
Sliding Window Maximum .txt 1015B
Max Points on a Line .txt 1015B
Word Break II .txt 1014B
Binary Tree Level Order Traversal II .txt 1014B
Evaluate Reverse Polish Notation .txt 1013B
Intersection of Two Linked Lists.txt 1001B
Lowest Common Ancestor of a Binary Tree.txt 1001B
Convert Sorted List to Binary Search Tree .txt 994B
Count Complete Tree Nodes.txt 993B
Candy.txt 992B
Divide Two Integers.txt 992B
Construct Binary Tree from Preorder and Inorder Traversal .txt 985B
Construct Binary Tree from Inorder and Postorder Traversal .txt 982B
Sort List .txt 974B
Sudoku Solver .txt 970B
N-Queens.txt 969B
Partition List .txt 966B
Word Search.txt 959B
Binary Search Tree Iterator .txt 952B
Search in Rotated Sorted Array II .txt 943B
Search for a Range.txt 934B
Palindrome Partitioning .txt 925B
Search in Rotated Sorted Array.txt 912B
Palindrome Partitioning II.txt 908B
3Sum.txt 906B
String to Integer (atoi) .txt 903B
Largest Number .txt 898B
Maximal Square.txt 898B
Palindrome Linked List .txt 893B
Path Sum II .txt 892B
Largest Rectangle in Histogram .txt 888B
Best Time to Buy and Sell Stock III .txt 881B
Binary Tree Level Order Traversal.txt 877B
Implement strStr() .txt 876B
Implement Queue using Stacks .txt 865B
Regular Expression Matching .txt 860B
Recover Binary Search Tree .txt 849B
Simplify Path.txt 846B
Count and Say .txt 834B
Remove Duplicates from Sorted List II .txt 833B
Spiral Matrix .txt 802B
Insertion Sort List .txt 795B
Binary Tree Maximum Path Sum.txt 793B
Product of Array Except Self .txt 788B
Lowest Common Ancestor of a Binary Search Tree.txt 781B
Symmetric Tree .txt 772B
Binary Tree Inorder Traversal .txt 768B
House Robber II.txt 764B
Swap Nodes in Pairs .txt 761B
Scramble String.txt 759B
Valid Palindrome .txt 755B
Binary Tree Postorder Traversal .txt 751B
Course Schedule II .txt 747B
Add Binary .txt 743B
Single Number II .txt 735B
Longest Common Prefix .txt 727B
3Sum Closest .txt 701B
Substring with Concatenation of All Words.txt 697B
Same Tree .txt 693B
Edit Distance .txt 680B
Binary Tree Preorder Traversal.txt 677B
共 202 条
- 1
- 2
- 3
资源评论
wlgcce
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于C51带字库LCD12864(ST7920)的keil工程源码,只支持8位并口通讯(不支持串口),可显示中文.zip
- 基于SI4463射频模块433MD-SMA无线模块软硬件技术资料及(SI4463)IC技术资料文档.zip
- (GPS+北斗+GSM)HLK-GS2503模块软硬件开发资料包硬件参考设计(原理图PCB)+技术文档资料.zip
- 基于BERT+Biaffine结构的关系抽取模型源码+文档说明.zip
- 利用c语言编写的冒泡排序代码
- 基于Ansoft-HFSS知识总结hfss中文教程HFSS培训教材等技术资料合集(50个).zip
- 基于Python+OpenCV的材料缺陷检测程序项目源码课程设计.zip
- 基于c语言实现的二叉树代码
- pip利用清华镜像源下载matplotlib代码
- Python的pyqt5写的图书管理系统期末大作业源码带文档设计.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功