
算法
文章平均质量分 79
Re1own
coding the world
展开
-
构造+模拟
codeforces1255C思路记录每个数在所有三元组出现的次数、和每个数连了哪些数(和哪些数在一个三元组里),其中出现次数为1的肯定是序列的头或尾,因为序列可以反转(得到同样的三元组),所以随便选一个出现次数为1的数当头,然后这个数连的点中出现次数为2的就是第二个数。后面的数可以通过判断前面两个数连的数是否有相同,如果有,那么这个数就是第三个数,以此类推。代码:#include <...原创 2019-11-30 11:05:43 · 182 阅读 · 0 评论 -
C语言实现带括号的计算器
中缀表达式转后缀表达式的应用我的算法以及思路:首先将输入的东西作为字符串存入到一个字符串数组,然后将中缀表达式转化为后缀表达式(其中关键在于运算符的转化,比较栈顶元素和当前运算符的优先级,如果栈顶元素的优先级大,则压入栈中,否则把栈里的运算符弹出直到为空,并且加入到后缀表达式的字符串中,再压栈,最后将后缀表达式转化为计算结果样例:#include <stdio.h>#inc...原创 2019-11-11 08:03:18 · 5383 阅读 · 3 评论 -
Rabin Karp算法(简易版的KMP算法)
#include <bits/stdc++.h>using namespace std;const int mod = 100000;int quick_power(int a, int x){ if (x == 0) return 1; if (x == 1) return a; int k = quick_power(a,x/2)%mod; ...原创 2019-05-27 20:27:04 · 311 阅读 · 0 评论 -
Kruskal-POJ2421(https://vjudge.net/problem/POJ-2421)
poj2421这题我用了Kruskal算法,算法思想:将目前的结点孤立成每个独立的集合,对边进行贪心算法,不断合并小边,化离散为统一,最后统一完所有结点后就是最小生成树了!具体方法:1、将所有的边存入到一个vector中,然后进行升序排序 2、选择当前最短的边,那么相应这条边所连接的两个节点的父亲就统一为编号小的结点的父亲(寻找父亲和合并父亲用到了并查集) 3、用一个m记录当前自由结点的...原创 2019-08-04 01:31:32 · 180 阅读 · 0 评论 -
Prim+邻接表+优先队列优化---POJ1287
POJ1287#include <cstdio>#include <queue>#include <cstring>#include <vector>using namespace std;struct Node { int to, cost; Node(){}; Node(int a, int b) { ...原创 2019-08-04 10:51:01 · 212 阅读 · 0 评论 -
KMP---HDU1686
HDU1686求s1在s2中出现了多少次#include <bits/stdc++.h>using namespace std;char s1[10010], s2[1000100];int nxt[10010];void get_next(int len) { //求next数组 nxt[0] = -1; //习惯把第一个字符的next值设...原创 2019-08-04 13:51:45 · 143 阅读 · 0 评论 -
Dijkstra-POJ2387邻接表和优先队列的优化
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstring>#include <map>#inc...原创 2019-07-27 10:48:53 · 160 阅读 · 0 评论 -
codeforces 1206C
codeforces1206C题意:给你一个数n(n代表将要连续的个数) 求从1 ~ 2*n 中对于每个连续数两者之差都是小于等于1,如果不存在这样的循环圈就输出NO,否则输出YES并打印出满足条件的循环圈数字思路:循环n次,从第1到第n位置的数赋值,同时也给第n+1到第2n位置的数赋值,先给谁赋值就看此时循环是第几次,这次是奇数还是偶数(找了半天规律)#include <io...原创 2019-08-29 16:26:24 · 199 阅读 · 0 评论 -
把二维数组当一维数组~codeforces1194B
题目:codeforces1194B#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 4e5 + 10;const int INF = 1<<29;char s[maxn]; //因为二维数组...原创 2019-08-30 11:10:15 · 554 阅读 · 0 评论 -
插入排序---重写了一遍,还是有些小问题,以后不会犯了!
#include <cstdio>using namespace std;void insertion_sort(int* a, int n){ for (int i = 1; i < n; i++) { int top = i-1; int k = a[i]; //只需注意这里要赋值数组的元素值而不是角标,否则循环后...原创 2019-05-15 08:07:31 · 159 阅读 · 0 评论 -
快速排序
#include <bits/stdc++.h>using namespace std;int a[1000];void quick_sort(int s, int e) //s表示开头下标,e表示结尾下标{ if (s < e) { int i = s, j = e; //i从左往右,j从右往左 while(...原创 2019-04-29 18:43:58 · 130 阅读 · 0 评论 -
插入排序
#include <iostream>using namespace std;int main(){ int a[5] = {5,6,2,7,1}; int preIndex,current; for (int i = 1; i < 5; i++) { preIndex = i - 1; //相对于current前一个元素...原创 2018-11-10 11:43:26 · 108 阅读 · 0 评论 -
如何用C语言进行蛇形数组填空
#include &amp;amp;lt;stdio.h&amp;amp;gt;#include &amp;amp;lt;stdlib.h&amp;amp;gt;#include &amp;amp;lt;string.h&amp;amp;gt;int main(void){int a[20][20];int n,i = 0;memset(a,0,sizeof(a)); //通过memset函数实现原创 2018-11-05 14:34:45 · 417 阅读 · 0 评论 -
小算法小技巧总结
变量交换(罕见):a = a + b;b = a - b;a = a - b;此时a、b就互换了也可以用指针交换二者变量的地址从而实现输出时数据交换原创 2018-11-05 14:44:14 · 168 阅读 · 0 评论 -
埃拉托斯特尼筛法
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;bool prime[1000000];void Is_primes(int n){ prime[0] = false; prime[1] =...原创 2018-12-03 20:48:03 · 216 阅读 · 0 评论 -
计算世界末日的汉诺塔(当n为64吧好像就是世界末日了)
#include&lt;cstdio&gt;#include&lt;cstdlib&gt;#include &lt;iostream&gt;using namespace std;void hannoi(int n, char a, char b, char c){ if (n == 0) return; hannoi(n-1, a,c,b); //将n个圈圈头上的n-1...原创 2018-12-03 21:14:05 · 1357 阅读 · 0 评论 -
欧几里德算法
首先我们要理解好辗转相除法亦所谓的‘欧几里德算法’两个整数的最大公约数是能够同时整除它们的最大的正整数。辗转相除法基于如下原理:两个整数的最大公约数等于其中较小的数和两数的差的最大公约数。例如,252和105的最大公约数是21;因为 252 − 105 = 21 × (12 − 5) = 147 ,所以147和105的最大公约数也是21。在这个过程中,较大的数缩小了,所以继续进行同样的计算可以不...原创 2018-12-03 21:31:22 · 190 阅读 · 0 评论 -
约瑟夫环问题——紫书的救济金发放题解
#include <bits/stdc++.h>using namespace std;const int maxn = 25;int n, k, m, a[maxn];int go(int p, int e, int t) //p是位置,e是方向,t是步长{ while(t--) { do { ...原创 2019-01-01 21:52:04 · 173 阅读 · 0 评论 -
最小字符串周期
#include <bits/stdc++.h>using namespace std;int main(void){ char s1[100]; scanf("%s",s1); int len, flag, i, j; //flag用来标记是否已经找到最小周期 len = strlen(s1); for (i = 1; i &l...原创 2019-01-02 08:54:59 · 397 阅读 · 0 评论 -
带分治思想的归并排序
#include <bits/stdc++.h>using namespace std;int a[1000];int t[1000];void merge_sort(int x, int y){ if (y - x > 1) //当if不成立时即是递归的出口 { int mid = x + (y-x)/2; in...原创 2019-04-28 21:41:55 · 210 阅读 · 0 评论 -
选择排序
#include <iostream>#include <string.h>#include <cstdio>using namespace std;int main(){ int a[5] = {5,1,3,6,9}; int minIndex,j,s; minIndex = 0; int len = 5; f...原创 2018-11-10 10:18:11 · 118 阅读 · 0 评论