我是靠谱客的博主 笑点低雨,这篇文章主要介绍玛雅人的密码,现在分享给大家,希望可以做个参考。

玛雅人的密码

POINT:
1.对于访问过的数据需要做出标记的问题中
之前使用的是bool visit[MAXN]; 这种方法比较适合使用数据是整型的时候
而此题需要用到的是string类型的,此时再用上述的方法就不行了,所以改用map类型的就方便很多,也在一定程度上节约空间,提升效率
2.map中查询某个关键字是否存在,
map.count(关键字)==0 //not exist
map.count(关键字)!=0 //... exist
2.C++中查找字符串:str.find(“string”) //切记字符串是" ",误写成’ ',后果很严重

附上代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<cstdio> #include<iostream> #include<string> #include<queue> #include<map> using namespace std; queue<string> myQueue; map<string,int> myMap; int BFS(string str) { //将str入队 myQueue.push(str); myMap[str] = 0; while (!myQueue.empty()) { string a = myQueue.front(); myQueue.pop(); if (a.find("2012") != -1) { return myMap[a]; } for (int i = 0; i + 1 < str.size(); i++) { string b = a; swap(b[i], b[i + 1]); if (myMap.count(b) != 0) continue; myMap[b] = myMap[a] + 1; myQueue.push(b); } } return -1; } int main() { string str; int n; while (scanf("%d", &n) != EOF) { cin >> str; while (!myQueue.empty()) { myQueue.pop(); } //通过宽度优先搜索战略 cout << BFS(str) << endl; } return 0; }

最后

以上就是笑点低雨最近收集整理的关于玛雅人的密码的全部内容,更多相关玛雅人内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部