有一个单线程CPU 正在运行一个含有 n 道函数的程序。每道函数都有一个位于 0 和 n-1 之间的唯一标识符 函数调用存储在一个调用栈 上 :当一个函数调用开始时,它的标识符将会推入栈中。而当一个函数调用结束时,它的标识符将会从栈中弹出。标识符位于栈顶的函数是 当前正在执行的函数
一、DFS之连通性模型 1112. 迷宫 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110; int T,n; char g[N][N]; int sx,sy,ex,ey; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; bool st[N][N],f; void d
对比前序遍历的"中左右",后序遍历是"左右中",颠倒一下就是"中右左",所以可以参照前序遍历的迭代法来写迭代遍历。 #include <algorithm> #include <stack> #include <vector> using std::stack; using std::vector; class Solution { public: vector<int> postord
题面传送门 首先我们发现,一定不会有低于\(h_1\)的参与操作的过程。 然后考虑一个\(x\)与比它大的\(y<z\),则发现一定是先\((x,y)\),再\((\frac{x+y}{2},z)\)更好。 因为这样是\(\frac{4}{x+y}+\frac{z}{2}\),而一起做是\(\frac{x+y+z}{3}\),显然更优。 而每个节点一定只会和一号节点联
树形DP,顾名思义,就是在树上设计动态规划。 一般树形DP的DP数组的第一维表示节点编号,代表以此节点为根的子树作为的阶段。除此之外,可能的第二维乃至第三维与题目具体要求相关。 在树上进行动态规划时,一般先用深度优先搜索(\(DFS\))来遍历该树,定义出每个节点的深度与以该节点为根的子树
#include<bits/stdc++.h> using namespace std; string read(){ string s,st; char c; int n; while(cin>>c){ if(c=='['){ cin>>n; st=read(); while(n--) s
https://codeforces.com/contest/1627/problem/C 给你一个n个顶点的树,顶点从1到n,边从1到n-1。树是没有圈的连通无向图。你必须给树的每条边分配整数权重,这样得到的图就是一个素数树。 素数树是指由一条或两条边组成的每条路的重量都是素数的树。一条路径不应该访问任何顶点两次。
利用stack(栈)这一数据结构,当前字符与栈顶字符相等时,pop(),最后把栈中的字符还原成字符串,注意栈是LIFO的,因此还原字符串时要注意顺序。 #include <stack> #include <string> using std::stack; using std::string; class Solution { public: string removeDuplicates(string
利用stack,括号匹配时pop()。 #include <stack> #include <string> using std::stack; using std::string; class Solution { public: bool isValid(string s) { stack<char> st; char temp; for (char c : s) { if (c =
简介 ST 表是用于解决可重复贡献问题(满足 \(x\) 操作 \(x=x\),如 \(max(x,x)=x\))的数据结构,它在区间查询最值时可以做到 \(O(n \log n)\) 预处理,\(O(1)\)查询,是种优秀的数据结构。 ST表 思路: ST 表基于倍增思想,我们可以先按普通的倍增想法,每次跳 \(2^i\) 步,但这样查询的复杂度是
题意 序列含有'{}', '()', '[]', 判断其是否有效 方法 stack 代码 bool isValid(string s) { int N = s.size(); if (N & 1) return false; stack<char> st; for (int i = 0; i < N; i++) { if (s[i] == '('
给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字。要求所有可能组合出来的2位数字的和。例如给定2、5、8,则可以组合出:25、28、52、58、82、85,它们的和为330。 输入格式: 输入在一行中先给出N(1<N<10),随后是N个不同的非0个位数字。数字间以空格分隔。 输出格式: 输出所
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: tis "ece" which its length is 3. Example 2: Input: "ccaabbb" Output
题面传送门 我们先来考虑一棵树怎么做。显然先将边排序,然后从大到小加边,每次加边\((x,y)\)以后会使\(f_x=f_y=f_x+f_y\)。 但是很遗憾这个做法并不能直接搬到仙人掌上因为有些点会被算重。我们计算的是路径的数量而要求的是点的数量。 还是延续这个思路,但是当加到一个环的最后一条
链接 https://ac.nowcoder.com/acm/contest/38457/C 题意是说,给你n个形如a时b分 c时d分的条件限制,表示不能选取,给出m个询问某个值是否可以选取 思路 1.可以把x时y分转化成一个值 ( x*m+y ) ,这样就可以把原条件看成n个区间的限制,用差分思想可做 点击查看代码 #include<bits/stdc++
做完这些我才感觉我的后缀自动机入门了之前写的东西就是一坨屎 对于后缀自动机的学习,我总结了以下三句话: 千万不要死磕模板!!! 千万不要死磕模板!!! 千万不要死磕模板!!! 谁死磕模板谁&#*%#(@# 这次就主要是我对于后缀自动机的理解,只是纯纯的自动机,不包含如何构造,因为那东西实在不行就背背
调用存储过程 --出参 ?出参名$类型$out,后面跟入参 CALL 存储过程名(?ret$integer$out,?msg$varchar$out,'XXX',1,NULL); 创建存储过程 create or replace procedure 存储过程名( OUT o_ret int, OUT o_info VARCHAR(100), IN i_Param1 VARCHAR(100), IN i
选一个起点,到其他点的最短距离之和最小 堆优化 dijkstra (太慢) 复杂度 \(O(n \cdot log(m) \cdot p) = 500 \times log(1450) \times 800 = 1.2 \times 10^7\) 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> PII;
华为机试 记录两个与栈相关的算法题,折腾了一下午 需要注意dfs前后对称的操作 利用栈结构去处理存在明显先后顺序的问题,比如四则运算等 HJ77 火车进站 栈+dfs回溯 #include<bits/stdc++.h> using namespace std; int N; vector<vector<int>> res; vector<int> path; void dfs(sta
相邻质数距离 https://www.acwing.com/problem/content/198/ #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 1000010; int primes[N], cnt; bool st[N]; void init(int n) {
4274. 后缀表达式 #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 25; int n; string v[N]; int l[N], r[N]; bool st[N]; void dfs(int u) { cout << '('; if(l[u] != -1
题意 旅行商问题,经过每个点一次,问从原点出发走回原点的最小距离 P对NP问题是Steve Cook于1971年首次提出。“P/NP问题”,这里的P指多项式时间(Polynomial),一个复杂问题如果能在多项式时间内解决,那么它便被称为P问题,这意味着计算机可以在有限时间内完成计算;NP指非确定性多项式时间(nond
链接 https://acm.hdu.edu.cn/showproblem.php?pid=7196 思路 读题后发现,由于每个人的到达时间不同,且可以唯一确定加入的队伍,所以每个人选择加入的队伍是确定的 那么只需要(按事件发生的时间顺序)模拟即可,使用优先队列 定义三元组<time,type,id> 把所有入队、出队操作看成操作序
需要处理输入的 Flood Fill 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n, m; int g[N][N]; bool st[N][N]; int dx[] = {0, -1, 0, 1}; int dy[] = {-1, 0, 1,
Flood Fill 问题 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n, m; char g[N][N]; bool st[N][N]; void bfs(int x, int y) { queue<pair<int,int>