算法基础
算法基础
海波东
。
展开
-
蛇形矩阵(数组模拟方向, d代表转弯)
撞墙代表着出界或者已经走过了 ,d代表什么时候转弯;原创 2022-06-24 12:29:02 · 109 阅读 · 0 评论 -
菱形打印(曼哈顿距离)
最近lc同学,停在了沙漏打印,所以我总结了一些规律,遇到同样困难的童鞋,可以看看,希望对大家有帮助//以下就是c代表字符,n代表三角的行数 //正三角模板 for(int i=1;i<=n;i++) { for(int j=1;j<=n-i;j++) printf(" "); for(int k=1;k<=2*i-1;k++) cout<<c; cout<<endl; } //倒三角模板 for(int i=1;i<=n原创 2021-09-01 19:10:02 · 158 阅读 · 0 评论 -
求幂级数展开的部分和(阶乘)
本题解只有18分:1:注意阶乘的时候的初始化;(jc函数自己写)2: 还有当某一项小于XXXX 的时候,可以for(int i = 1; ; i ++)加break的形式写出来;#include <bits/stdc++.h>using namespace std;const double eps = 1e-6;int jc(int n){ int res=1; for(int i=2;i<=n;i++){ res=res*i; } return res;}原创 2022-04-16 17:42:26 · 517 阅读 · 0 评论 -
Have Fun with Numbers(高精度乘法)
高精度乘法 模板:#include <iostream>#include <vector>using namespace std;vector<int> mul(vector<int> A, int b){ vector<int> C; int t = 0 ; for(int i = 0 ; i < A.size()|| t ; i ++) { if(i<A.size())t += A[i]*b;//这里注意,.原创 2022-04-16 16:10:16 · 159 阅读 · 0 评论 -
诶氏筛法 求素数
#include <iostream>using namespace std;const int N = 2e6;int primes[N];bool st[N];int cnt;void get_primes(int x){ for(int i= 2; i <= x; i++) { if(!st[i]) { primes[cnt++] = i; for(int j = i ; j <= x ; j+= i) st[j] = tru原创 2022-04-08 21:53:58 · 157 阅读 · 0 评论 -
数的次数统计
统计一个数中0-9的出现次数:#include <bits/stdc++.h>using namespace std;int cnt[10];int main(){ int n; cin >> n; while(n) { int x = n%10; cnt[x]++; n/=10; } for(int i = 0 ; i < 10; i++) cout << i << " " << cnt[i] <原创 2022-04-08 21:45:25 · 184 阅读 · 0 评论 -
二分(stl实现)
lower_bound 是返回大于等于x的那个位置;upper_bound是返回大于x的那个位置;以上是y总的二分模板;数的范围:#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 100100;int a[N];int main(){ int n,m; cin >> n >>.原创 2022-04-08 08:41:09 · 92 阅读 · 0 评论 -
走迷宫(就是(0,0)到(n-1, n-1)的最短距离 ) + 贪吃蛇(任意两个坐标的距离)
#include <bits/stdc++.h>using namespace std;const int N = 110;typedef pair<int , int> PII;int n , m;char g[N][N];int d[N][N];PII q[N*N]; int bfs(){ queue<PII> q; q.push({0,0}); //容易忘 memset(d, -1, sizeof.原创 2022-04-07 16:14:46 · 194 阅读 · 0 评论 -
topsort()
topsort是用邻接表来存储图,然后利用队列存所有入度为0的点, 返回是否等于n-1个点的true还是false;注意:memset(h, -1, sizeof h);一定是在主函数输入之前, 不是输入之后, 要不然会超时的, 老是犯这个错误(本人)#include <bits/stdc++.h>using namespace std;const int N = 100010;int d[N];int idx, ne[N], e[N],h[N];int q[N];int原创 2022-04-02 10:50:46 · 220 阅读 · 2 评论 -
多项式A除以B(纯纯的模拟)
多项式其实就是以被除数的最高项为基础,求出相应的指数和系数;然后乘以B,然后减去更新A(注意这里A式一直变的, B是一直不变的);问题:1: A中如图没有X的3次方怎么办?2: 如何更新A3: 存底数, 指数, 用什么?4 : 怎样统计结果的组数5: 怎么遍历这个多项式, (应该想到以所有的指数从大到小遍历进行运算)6: 每次的减数应该存一下7: 只需要再开一个map存商就对了,余数就直接是A参考的博客:#include<bits/stdc++.h>usi.原创 2022-03-27 09:55:32 · 661 阅读 · 0 评论 -
BFS(求任意两个点的距离, 注意这里的边权是1)
#include <iostream>#include <cstring>#include <algorithm>#include <queue>using namespace std;int n , m;const int N = 1000010;int d[N];int h[N] , ne[N], e[N], idx;queue<int> q;int bfs(int start, int end){ memset原创 2022-03-17 16:00:42 · 334 阅读 · 0 评论 -
包装机(栈和队列的应用)
#include<iostream>#include<queue>#include<stack>using namespace std;const int N=1010,M=1010;int n,m,s,x;string str;queue<char>q[N];stack<char>stk; int main(){ cin>>n>>m>>s; //队列的字符读入,..原创 2022-03-13 23:03:44 · 253 阅读 · 0 评论 -
冰岛人(近亲判断五代以内能否结婚)
参考博客:冰岛人稍微有改动,可以看看题目是啥样的#include<iostream>#include<map>#include<string>using namespace std;struct Peoson { char sex; string father;};map<string, Peoson> people;int judge(string a, string b) { int i = 1, j; for (stri原创 2022-02-27 23:17:35 · 212 阅读 · 0 评论 -
蓝桥B组:货物摆放(三个公因数的多少种选法)
#include <iostream>#include <algorithm>#include <vector>#include <iostream>using namespace std;typedef long long LL;int main(){ LL n = 2021041820210418; vector<LL> seg; for(int i= 1; i <= n/i ; i ++) { if(n%原创 2022-02-22 13:44:58 · 436 阅读 · 0 评论 -
蓝桥B组直线(四层循环枚举两个点的横纵坐标)
#include <iostream>#include <algorithm>#include <cmath>using namespace std;const int N= 200000;int n;struct Line{ double k , b; bool operator < (const Line & t) const { if(k!= t.k) return k < t.k; return b < t.原创 2022-02-22 11:49:22 · 437 阅读 · 0 评论 -
今年暑假不AC(贪心) 和 冒泡做法
#include <iostream>#include <algorithm>using namespace std;const int N = 100010;int n;struct Range{ int l, r; bool operator < (const Range & W)const { return r < W.r; }}range[N];//const Range &W.原创 2022-02-08 21:47:01 · 331 阅读 · 0 评论 -
7-3 朋友圈
朋友圈典型并查集:7 43 1 2 32 1 43 5 6 71 6注意一下连通的时候模板的祖宗结点是函数的后者,如果两者连到一起就可以看作一个整体根据上面两张图,有兴趣的可以想一想谁是祖宗结点,还有要查连通块中的个数的时候是根据祖宗结点查的,不是根据i,#include<iostream>#include <algorithm>using namespace std;const int N = 30000+100;int n,q;int p...原创 2022-01-25 23:41:06 · 287 阅读 · 0 评论 -
dijkstra
#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N = 510;int n,m;int g[N][N];int dist[N];bool st[N];int dijkstra(){ memset(dist , 0x3f3f3f3f, sizeof dist); dist[1] = 0; for..原创 2022-01-24 21:47:30 · 286 阅读 · 0 评论 -
飞行员兄弟
#include <cstring>#include <iostream>#include <algorithm>#include <vector>#define x first //用pair的时候更方便一些,打得字少 #define y secondusing namespace std;typedef pair<int, int> PII;//pair<int ,int> PII各取第一个数的开头 .原创 2022-01-12 22:02:58 · 68 阅读 · 0 评论 -
KMP算法
你应该清楚 KMP 为什么相比于朴素解法更快:1:因为 KMP 利用已匹配部分中相同的「前缀」和「后缀」来加速下一次的匹配。2:因为 KMP 的原串指针不会进行回溯(没有朴素匹配中回到下一个「发起点」的过程)朴素伪码for(int i = 1; i <= n ; i ++) bool flag = true; for(int j = 1; j <= m ; j ++) if(s[i]!=p[j]) { flag = false; break; }KMP完整代码:原创 2021-12-02 00:40:12 · 78 阅读 · 0 评论 -
01背包,完全背包(二维和一维)
//完全背包for(int i=1; i<=n; i ++) for(int j=0; j<=m; j ++) { f[i][j] = f[i-1][j];//这个是不选第i个物品 if(j>=v[i])//选的时候背包体积要大于物体体积 f[i][j]=max(f[i][j],f[i][j-v[i]]+w[i]); }这里是完全背包,注意max中的第二个,也就是当我们选的时候,这里和01背包的区别就是01背包是f[i-1],完全背包是f[i],其他一模一样。//原创 2021-11-06 21:17:15 · 134 阅读 · 0 评论 -
差分与前缀和
一维前缀和#include <iostream>using namespace std;const int N = 100010;int n, m;int a[N], s[N];int main(){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); for (int i = 1; i <= n; i ++ ) s[原创 2021-08-21 16:39:47 · 52 阅读 · 0 评论 -
排列数字(dfs)
#include <iostream>using namespace std;const int N=10;int path[N];bool st[N];void dfs( int u){ if(u==n)//这个是判断是否走到了终点 { for(int i=0;i<n;i++) cout<<path[i]; cout<<endl; return ; } for(int i=1;i<=n;i++) { if(!st[原创 2021-08-10 15:19:29 · 55 阅读 · 0 评论 -
模拟散列表
拉链法:#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N=100010;int e[N],ne[N],h[N],idx;void insert(int x){ int k; k=(x%N+N)%N;//哈希最关键的一步,避免了负数 e[idx]=x,ne[idx]=h[k],h[k]=idx++; }bool qu原创 2021-08-05 22:57:44 · 47 阅读 · 0 评论 -
回车符代表输入结束的标志
#include<stdio.h>#include<math.h>int main(){ char x; int i=0; while(scanf("%c",&x),x!='\n')//回车符作为输入结束的标志 { if(x>='0'&&x<='9') { i++;//记录数字字符个数 } } printf("%d",i); return 0; }输入一个字符串,统计其中数字字符(‘0’……‘9’)的原创 2021-06-02 20:51:24 · 1982 阅读 · 0 评论