
算法系列
超翔之逸
可关注超翔之逸微信公众号哦!!!
展开
-
实现模拟链表算法
【代码】实现模拟链表算法。原创 2022-09-23 10:07:21 · 198 阅读 · 0 评论 -
给出N个非负整数,要用它们构建一棵完全二叉树排序树。输出这棵完全二叉排序树的层序遍历序列。
有N个人,每个人喜欢若干项活动,如果两个人有任意一个活动相同,那么就称它们处于同一个社交网络,求这N个人总共形成了多少个社交网络。给出一个初始序列,可以对它使用插入排序或堆排序法进行排序。给出N个正整数,将它们依次插入一棵初始为空的AVL树上,求插入后根结点。N个整数的序列,需要把这N个整数填入二叉树的结点中,使得二叉树成为。试判断它是由插入排序还是堆排序产生的,并输出下一步将会产生的序列。给出N个非负整数,要用它们构建一棵完全二叉树排序树。二叉树有N个结点,给出每个结点的左右孩子结点的编号。原创 2022-09-18 13:32:07 · 542 阅读 · 0 评论 -
给出N个正整数来作为一棵二叉排序树的结点插入顺序,问这串序列是否是该二叉排序树的先序序列或是该二叉排序树的镜像树的先序序列。
给出N个正整数来作为一棵二叉排序树的结点插入顺序,问这串序列是否是该二叉排序树的先序序或是该二叉排序树的镜像树的先序序列。原创 2022-09-18 13:26:09 · 220 阅读 · 0 评论 -
螺旋矩阵问题+Queueing at Bank
【代码】螺旋矩阵问题+Queueing at Bank。原创 2022-09-18 13:24:23 · 133 阅读 · 0 评论 -
反转链表题解
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。要求:空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n)。经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。数据范围: 0\leq n\leq10000≤n≤1000。如当输入链表{1,2,3}时,原创 2022-09-14 13:41:50 · 202 阅读 · 0 评论 -
火星数字与地球数字的转换
火星数字与地球数字的转换。原创 2022-09-09 23:26:46 · 148 阅读 · 0 评论 -
Stack问题
【代码】Stack问题。原创 2022-09-09 23:25:27 · 117 阅读 · 0 评论 -
Longest Symmetric String
【代码】Longest Symmetric String。原创 2022-09-09 23:24:14 · 133 阅读 · 0 评论 -
Find More Coins
i=1;i--){}}}原创 2022-09-07 13:20:45 · 111 阅读 · 0 评论 -
最长回文子串
i原创 2022-09-07 13:19:38 · 141 阅读 · 5 评论 -
Favorite Color Stripe
LIS解法:i原创 2022-09-07 13:18:33 · 120 阅读 · 0 评论 -
最大连续子序列和
int n;i原创 2022-09-07 13:17:19 · 88 阅读 · 0 评论 -
Forwards on Weibo
【代码】Forwards on Weibo。原创 2022-09-07 13:14:33 · 118 阅读 · 0 评论 -
Head of a Gang
【代码】Head of a Gang。原创 2022-09-07 13:12:57 · 114 阅读 · 0 评论 -
Deepest Root
Deepest Root。原创 2022-09-07 13:11:23 · 135 阅读 · 0 评论 -
Travel Plan
Travel Plan原创 2022-09-04 23:38:32 · 138 阅读 · 0 评论 -
经典算法题题解
利用栈解决hdu 1062“Text Reverse”问题:#include<cstdio>#include<stack>using namespace std;int main() { int n; char ch; scanf_s("%d", &n); getchar(); while (n--) { stack<int>s; while (true) { ..原创 2022-03-03 13:53:26 · 184 阅读 · 0 评论 -
Vector容器解决Hdu4841“圆桌问题”
#include<iostream>#include<vector>using namespace std;int main() { vector<int>table; int n, m; while (cin >> n >> m) { table.clear(); for (int i = 0; i < 2 * n; i++) table.push_ba.原创 2022-03-03 13:52:05 · 143 阅读 · 0 评论 -
BFS算法解决hdu 1312 “red and black”问题:
#include<iostream>#include<queue>using namespace std;char room[23][23];int dir[4][2] = { {-1,0}, {0,-1}, {1,0}, {0,1}};int Wx, Hy, num;#define CHECK(x,y){x<Wx&&x>=0&&y>=0&&y<Hy}struct.原创 2022-03-03 13:51:02 · 169 阅读 · 0 评论 -
DFS算法解决N皇后问题
#include<iostream>#include<cmath>#include<cstring>using namespace std;int n, tot = 0;int col[12] = { 0 };bool check(int c, int r) { for (int i = 0; i < r; i++) if (col[i] == c || (abs(col[i] - c) == abs(i - r))) {//q.原创 2022-03-03 13:49:34 · 120 阅读 · 0 评论 -
模拟退火法求函数最值:
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const double eps = 1e-8;double y;double func(double x) { return 6 * pow(x, 7.0) + 8 * pow(x, 6.0) + 7 * pow(x, 3.0) + 5 * pow(x, 2.0) - y * x;}double solve.原创 2022-03-03 13:48:30 · 169 阅读 · 0 评论 -
快速排序算法求解逆序对问题:
#include<cstdio>const int N = 10010;int data[N];#define swap(a,b){int temp=a;a=b;b=temp;}int partition(int left, int right) { int i = left; int temp = data[right]; for (int j = left; j < right; j++) if (data[j] < temp) .原创 2022-03-02 23:38:19 · 587 阅读 · 1 评论 -
输入两个长度均不超过1000的字符串,求它们的最长公共子串的长
#include<iostream>#include<cstdio>#include<string>#include<vector>#include<map>#include<algorithm>using namespace std;typedef long long LL;const LL MOD=1000000007;const LL P=10000019;const LL MAXN=1010;L...原创 2022-03-02 23:36:37 · 175 阅读 · 0 评论 -
给出N个只有小写字母的字符串,求其中不同的字符串的个数。
#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;const int MOD=1000000007;const int P=10000019;vector<int>ans;long long hashFunc(string str){ long long H=0; for(int...原创 2022-03-02 23:31:42 · 968 阅读 · 0 评论 -
0/1背包问题算法实现:
#include<cstdio>#include<algorithm>using namespace std;const int maxn=100;const int maxv=1000;int w[maxn],c[maxn],dp[maxv];int main(){ int n,V; scanf("%d%d",&n,&V); for(int i=0;i<n;i++){ scanf("%d",&w[i.原创 2022-03-02 23:30:01 · 102 阅读 · 0 评论 -
最长回文子串动态规划实现
#include<cstdio>#include<cstring>const int maxn=1010;char S[maxn];int dp[maxn][maxn];int main(){ gets(S); int len=strlen(S),ans=1; memset(dp,0,sizeof(dp)); for(int i=0;i<len;i++){ dp[i][i]=1; if(i<len.原创 2022-03-02 23:29:01 · 63 阅读 · 0 评论 -
最长公共子序列动态规划实现
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=100;char A[N],B[N];int dp[N][N];int main(){ int n; gets(A+1); gets(B+2); int lenA=strlen(A+1); int lenB=strlen(B+1); for(i.原创 2022-03-02 23:27:51 · 79 阅读 · 0 评论 -
最长不下降子序列动态规划实现:
#include<cstdio>#include<algorithm>using namespace std;const int N=100;int A[N],dp[N];int main(){ int n; scanf_s("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&A[i]); } int ans=-1; for(int i=1;i<.原创 2022-03-01 22:55:11 · 143 阅读 · 0 评论 -
最大连续子序列和动态规划实现:
#include<cstdio>#include<algorithm>using namespace std;const int maxn=10010;int A[maxn],dp[maxn];int main(){ int n; scanf_s("%d",&n); for(int i=0;i<n;i++){ scanf_s("%d",&A[i]); } dp[0]=A[0]; for(.原创 2022-03-01 22:54:10 · 83 阅读 · 0 评论 -
数塔问题动态规划实现算法:
#include<cstdio>#include<algorithm>using namespace std;const int maxn=1000;int f[maxn][maxn],dp[maxn][maxn];int main(){ int n; scanf_s("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ scanf.原创 2022-03-01 22:52:47 · 153 阅读 · 0 评论 -
Fibonacci动态规划实现算法
#include<cstdio>using namespace std;const int MAXN=1000000000;int dp[MAXN];int F(int n){ if(n==0||n==1) return 1; if(dp[n]!=-1) return dp[n]; else { dp[n]=F(n-1)+F(n-2); return dp[n]; }}原创 2022-03-01 22:51:48 · 158 阅读 · 0 评论 -
Bellman-Ford算法(邻接表法
struct Node{ int v,dis;};vector<Node>Adj[MAXV];int n;int d[MAXV];bool Bellman(int s){ fill(d,d+MAXV,INF); d[s]=0; for(int i=0;i<n-1;i++){ for(int u=0;u<n;u++){ for(int j=0;j<Adj[u].size();j++){ .原创 2022-03-01 22:50:31 · 278 阅读 · 0 评论 -
有N个城市(编号为0~N-1)、M条道路(无向边),并给出M条道路的距离属性与花费属性。现在给定起点S与终点D,求从起点到终点的最短路径、最短距离及花费。注意:如果有多条最短路径,则选择花费最小的那条
题解1(Dijkstra):#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXV=510;const int INF=1000000000;int n,m,st,ed,G[MAXV][MAXV],cost[MAXV][MAXV];int d[MAXV],c[MAXV],pre[MAXV];bool vis[MAXV]={fal...原创 2022-03-01 13:58:30 · 1372 阅读 · 0 评论 -
用Dijkstra算法求pre数组
vector<pre[MAXV];void Dijkstr(int s){ fill(d,d+MAXV,INF); d[s]=0; for(int i=0;i<n;i++){ int u=-1,MIN=INF; for(int j=0;j<n;j++){ if(visit[j]==false&&d[j]<MIN){ u=j; .原创 2022-03-01 13:43:15 · 227 阅读 · 0 评论 -
给出N个城市,M条无向边。每个城市中都有一定数目的救援小组,所有边的边权已知。现在给出起点和终点,求从起点到终点的最短路径条数及最短路径上的救援小组数目之和。如果有多条最短路径,则输出数目之和最大的
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXV=510;const int INF=1000000000;int n,m,st,ed,G[MAXV][MAXV],weight[MAXV];int d[MAXV],w[MAXV],num[MAXV];bool visit[MAXV]={false};void Dijkst...原创 2022-02-28 23:46:02 · 160 阅读 · 0 评论 -
亚历山大题解
#include<cstdio>#include<algorithm>using namespace std;const int MAXV=1000;const int INF=1000000000;int n ,m,s,G[MAXV][MAXV];int d[MAXV];bool visit[MAXV]={false};void Dijkstra(int s){ fill(d,d+MAXV,INF); d[s]=0; for(int i=0.原创 2022-02-28 23:43:29 · 117 阅读 · 0 评论 -
Dijkstra算法
Dijkstra(G,d[],s){初始化; for(循环n次){ u=使d[u]最小的还未被访问的顶点的标号; 记u已被访问; for(从u出发能到达的所有顶点v){ if(v未被访问&&以u为中介点使s到顶点v的最短距离d[v]更优){ 优化d[v]; } } }}Dijckstra算法伪代码邻接矩阵版:const i.原创 2022-02-28 23:42:31 · 113 阅读 · 0 评论 -
PAT A 1076 Forwards on Weibo题解
#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int MAXV=1010;struct Node{ int id; int layer;};vector<Node>Adj[MAXV];bool inq[MAXV]={false};int BFS(int s,int L){...原创 2022-02-28 23:38:55 · 99 阅读 · 0 评论 -
BFS算法
邻接矩阵版BFS:int n,G[MAXV][MAXV];bool inq[MAXV]={false};void BFS(int u){ queue<int>q; q.push(u); inq[u]=true; while(!q.empty()){ int u=q.front(); q.pop(); for(int v=0;v<n;v++){ if(inq[v]==false...原创 2022-02-28 23:30:36 · 133 阅读 · 0 评论 -
已知是连通图,用BFS遍历算法伪代码:
BFS(u){ queue q; 将u入队; inq[u]=true; while(q!=NULL){ 取出q的队首元素u进行访问; for(从u出发可达的所有顶点v); if(inq[v]==false){ 将v入队; inq[v]=true; } }}BFSTrave(G){ for(G的所有d'du) if(inq[u]==fal.原创 2022-02-27 13:49:25 · 309 阅读 · 0 评论