no match for ‘operator=’ 等号两端 不匹配
2016-04-14 17:26
603 查看
const auto new_states = state_extend_function(word,dict,visited,end);
unordered_set<string>::iterator itv;
for ( itv=new_states.begin();itv != new_states.end();itv++ ){
string state=*itv;// 操作state
}
报错:
Solution.h:72:15: error: no match for ‘operator=’ (operand types are ‘std
等号两端 不匹配, 可能是auto的C++11版本不同的原因。改成下面的通过:
。。原题 及 修正后的完整代码如下:。。。。。
给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列
比如:
每次只能改变一个字母。
变换过程中的中间单词必须在字典中出现。
注意事项
1.所有单词具有相同的长度。
2.所有单词都只包含小写字母。
样例
给出数据如下:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
返回
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
思路:BFS。
。。。
很多高级语言里引入了lambda表达式的概念,即匿名函数。
。。。
unordered_set<string>::iterator itv;
for ( itv=new_states.begin();itv != new_states.end();itv++ ){
string state=*itv;// 操作state
}
报错:
Solution.h:72:15: error: no match for ‘operator=’ (operand types are ‘std
等号两端 不匹配, 可能是auto的C++11版本不同的原因。改成下面的通过:
unordered_set<string> new_states = state_extend_function(word,dict,visited,end);
。。原题 及 修正后的完整代码如下:。。。。。
给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列
比如:
变换过程中的中间单词必须在字典中出现。
注意事项
1.所有单词具有相同的长度。
2.所有单词都只包含小写字母。
样例
给出数据如下:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
返回
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
思路:BFS。
#include <iostream> #include <vector> #include <limits.h> #include <string> #include <queue> #include <unordered_set> #include <unordered_map> using namespace std; void gen_path(unordered_map<string, vector<string> > &father, vector<string> &path, const string &start, const string &word, vector<vector<string> > &result) { path.push_back(word); if (word == start) { result.push_back(path); reverse(result.back().begin(), result.back().end()); } else { vector<string> ::iterator itfv;// for (const auto& f : father[word]) { for (itfv = (father[word]).begin();itfv !=(father[word]).end();itfv++ ) { auto f=*itfv; gen_path(father, path, start, f, result); } } path.pop_back(); } unordered_set<string> state_extend_function(const string &s, const unordered_set<string> &dict, unordered_set<string> visited,string end) { unordered_set<string> result; for (size_t i = 0; i < s.size(); ++i) { string new_word(s); for (char c = 'a'; c <= 'z'; c++) { if (c == new_word[i]) continue; swap(c, new_word[i]); if ((dict.count(new_word) > 0 || string(new_word) == string(end) ) && !visited.count(new_word)) { result.insert(new_word); } swap(c, new_word[i]); // 恢复该单词 } } return result; } vector<vector<string> > findLadders(string start, string end, const unordered_set<string> &dict) { unordered_set<string> current, next; // 当前层,下一层,用集合是为了去重 unordered_set<string> visited; // 判重 unordered_map<string, vector<string> > father; // 树 bool found = false; auto state_is_target = [&](const string &s) { return s == end; }; current.insert(start); while (!current.empty() && !found) { // 先将本层全部置为已访问,防止同层之间互相指向for(const auto& word:current) unordered_set<string>::iterator it; for ( it=current.begin();it != current.end();it++ ){ string word=*it; visited.insert(word); } unordered_set<string>::iterator itc; for ( itc=current.begin();itc != current.end();itc++ ){ string word=*itc; unordered_set<string> new_states = state_extend_function(word,dict,visited,end); unordered_set<string>::iterator itv; for ( itv=new_states.begin();itv != new_states.end();itv++ ){ string state=*itv; if (state_is_target(state)) found = true; next.insert(state); father[state].push_back(word); // visited.insert(state); // 移动到最上面了 } } current.clear(); swap(current, next); } vector<vector<string> > result; if (found) { vector<string> path; gen_path(father, path, start, end, result); } return result; } void main(){ string start="hit",end="cog"; string sArr[]={"hot","dot","dog","lot","log"}; int len=sizeof(sArr)/sizeof(sArr[0] ); unordered_set<string> dict( sArr ,sArr+len); findLadders(start, end,dict); cout<<"out="<<endl; system("pause"); }
。。。
很多高级语言里引入了lambda表达式的概念,即匿名函数。
。。。
相关文章推荐
- 将ASM里面的文件copy到文件系统
- Windows下配置nginx+php(wnmp)
- Apache CXF REST example
- IplImage、IplImage、Mat之间的转换
- Linux常见命令集锦
- 如何获取到我们博客的访问数据之腾讯分析网站
- Linux常见命令集锦
- OpenCV——PS 图层混合算法 (二)
- eclipse出现每次修改代码报heap over错误,然后必须重启tomcat问题,修改过程
- Linux入侵检查实用指令
- Zookeeper的安装和配置(单机模式)
- java集合框架07——Map架构与源码分析
- Displaying Graphics with OpenGL ES(一)——构建一个OpenGL ES环境
- JavaEE企业应用实战第三章开始源码出错,尤其使用Tomcat7版本
- 一次Linux系统被攻击的分析过程
- docker构建jenkins镜像和容器
- Hadoop安装遇到的各种异常及解决办法
- Win10支持Ubuntu Linux Bash-apache+php+mysql环境搭建
- docker跨主机网络通信实践
- 对zookeeper的认识