leetcode 212 【trie+dfs】
来源:互联网 发布:淘宝首页怎样设计 编辑:程序博客网 时间:2024/04/29 09:17
好多bug。。好醉。
不知道怎么写题解了。。。应该说就是tire树的应用吧,给你一个map,然后在map中找到存在的指定串。。。
但是感觉还有很多的。。地方不能好好优化,因为并不知道这个函数确切的使用情况!!!不开心。。没有数据大小。。。
class Solution {public: class node { public: int have; node *next[26]; node(){for(int i=0;i<26;i++) next[i]=NULL;have=-1;} }; int cmpx[4]={0,0,1,-1}; int cmpy[4]={1,-1,0,0}; void search(int x,int y,node *tmp,map<int,int>&vis,vector<string>&ret,vector<vector<char>>& board,vector<string>&words,map<int,int>&ever) { vis[x*board[x].size()+y]=1; if(tmp->have!=-1&&(!ever[tmp->have])) {ever[tmp->have]=1;ret.push_back(words[tmp->have]);} for(int i=0;i<4;i++) { int xx=x+cmpx[i],yy=y+cmpy[i]; if(xx<0||yy<0||xx>=board.size()||yy>=board[x].size()) continue; int j=board[xx][yy]-'a'; if(tmp->next[j]==NULL||vis[xx*board[x].size()+yy]) continue; search(xx,yy,tmp->next[j],vis,ret,board,words,ever); } vis[x*board[x].size()+y]=0; //这里记得要清零,因为可能本次的搜索是无效的,但是可能下一次会用到,想的时候想到了,但是写的时候忘了。。。 } vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { node *root=new node(); for(int i=0;i<words.size();i++) { node *p=root; for(int j=0;j<words[i].size();j++) { int k=words[i][j]-'a'; if(p->next[k]==NULL) p->next[k]=new node(); p=p->next[k]; } p->have=i; } vector<string>ret; map<int,int>ever; map<int,int>vis; ever.clear(); for(int i=0;i<board.size();i++) { for(int j=0;j<board[i].size();j++) { int k=board[i][j]-'a'; if(root->next[k]==NULL) continue; vis.clear(); search(i,j,root->next[k],vis,ret,board,words,ever); //cout<<"change"<<endl; } } return ret; }};
0 0
- leetcode 212 【trie+dfs】
- LeetCode 212 Word Search II (Trie树+DFS)
- LeetCode Word Search II DFS+Trie树
- Leetcode-Trie
- poj 1816(trie+dfs)
- Poj1816(Trie+DFS)
- poj 1816 (Trie + dfs)
- Trie+DFS(1251)
- Trie树 + DFS
- trie+dfs+贪心 big
- leetcode:Trie:Word Search II(212)
- trie树的DFS遍历
- POJ1816 Wild Words Trie+Dfs
- POJ 3764 DFS+trie树
- leetcode ----Trie/stack专题
- leetcode 208 【trie】
- leetcode 211 【Trie】
- Leetcode-DSF-Trie-Backtrack
- 这一刻进入编程世界
- 深入理解Redis数据淘汰策略
- 我的Cocos2d-x学习笔记(二十)音乐与音效
- 100项PPT制作技术,做出让观众倾倒的演示文档!
- 页面常见问题
- leetcode 212 【trie+dfs】
- 人人都是架构师:面对风险
- android 按钮切换activity和打开网页
- JVM内存配置
- linux系统常用命令
- 中国剩余定理
- Ubuntu14.04 OpenCV编译安装
- POJ - 1719 Shooting Contest 二分图最大匹配
- 解决chrome32以上版本,非商店扩展程序被停用的问题