From 684e35b21091b80a0f14452342038992a7693e0a Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Thu, 11 Dec 2025 12:04:50 +0800 Subject: [PATCH] Stack --- Algorithm/Stack/栈模拟.cpp | 35 +++++++++++++++++++ Exercise/TreeToGraph.cpp | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Algorithm/Stack/栈模拟.cpp create mode 100644 Exercise/TreeToGraph.cpp diff --git a/Algorithm/Stack/栈模拟.cpp b/Algorithm/Stack/栈模拟.cpp new file mode 100644 index 0000000..ca0127b --- /dev/null +++ b/Algorithm/Stack/栈模拟.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; +int main(){ + int m, n, k; + cin >> m >> n >> k; + vector st; + vector> data(k, vector(n)); + for(int i = 0; i < k; ++i){ + for(int j = 0; j < n; ++j){ + int a; cin >> a; + data[i][j] = a; + } + } + + //模拟输入 + for(int i = 0; i < k; ++i){ + int p = 0; + bool flag = true; + for(int j = 1; j <= n; ++j){ + st.push_back(j); + if(st.size() > m){ + flag = false; + break; + } + while(data[i][p] == st.back() && !st.empty()) { + st.pop_back(); + p++; + } + } + cout << ((st.empty() && flag == true) ? "YES" : "NO") << endl; + st.clear(); + } + return 0; +} \ No newline at end of file diff --git a/Exercise/TreeToGraph.cpp b/Exercise/TreeToGraph.cpp new file mode 100644 index 0000000..c49338a --- /dev/null +++ b/Exercise/TreeToGraph.cpp @@ -0,0 +1,69 @@ +/* + * @Author: e2hang 2099307493@qq.com + * @Date: 2025-12-10 15:56:43 + * @LastEditors: e2hang 2099307493@qq.com + * @LastEditTime: 2025-12-10 16:50:18 + * @FilePath: \undefinedd:\code\Git-DataStructureAlgorithms\Exercise\TreeToGraph.cpp + * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE + */ +#include +#include +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; +}; + +//我直接写TreeNode -> Graph的内容了 +unordered_map> adj; +unordered_map visited; + +void TreeNodeToGraph(TreeNode* root){ + if(root == nullptr) return; + if(root->left != nullptr){ + adj[root].push_back(root->left); + adj[root->left].push_back(root); + TreeNodeToGraph(root->left); + } + if(root->right != nullptr){ + adj[root].push_back(root->right); + adj[root->right].push_back(root); + TreeNodeToGraph(root->right); + } +} +//target 是 起始点 +deque BFS(TreeNode* target, int step){ + deque q; + q.push_back(target); + int dist = 0; + + while(!q.empty()){ + int cnt = q.size();//每次弹几个 + if(dist == step) return q; + + for(int i = 0; i < cnt; i++){ + auto node = q.front(); + q.pop_front(); + visited[node] = true; + for(auto x : adj[node]){ + if(!visited[x]) { + q.push_back(x); + visited[x] = true; + } + } + } + dist++; + } + return {}; +} + + +int main(){ + + + return 0; +} \ No newline at end of file