
算法笔记
没有胡子的猫
这个作者很懒,什么都没留下…
展开
-
写给自己,算法学习踩坑记录
一定要注意细节啊,看看你都犯了什么沙雕错误!原创 2020-03-20 11:08:55 · 185 阅读 · 0 评论 -
CCF计算机软件能力认证试题练习 文章内容均为100分
CCF计算机软件能力认证试题练习201912-1 报数201912-2 垃圾站选址201912-3 化学方程式原创 2020-03-10 14:30:15 · 286 阅读 · 0 评论 -
PAT乙级真题题解(B题+部分A题)+ codeup真题 题解 汇总(附运行结果)备考CCF CSP——长期更新
PAT B题【PAT B1001】 害死人不偿命的(3n+1)猜想【PAT B1009】说反话【PAT B1011】 A+B和C【PAT B1016】 部分A+B【PAT B1022】D进制的A+B【PAT B1032】 挖掘机技术哪家强【PAT B1036】跟奥巴马一起学编程【PAT B1046】 划拳PAT A题【PAT A1025】PAT RANKINGCodeUp【...原创 2020-01-06 12:38:37 · 370 阅读 · 0 评论 -
KMP算法
在串的模式匹配中KMP算法效率较高,并且是无递归的。时间复杂度O(m+n)。//求解next数组//next[j] 表示j处发生不匹配,j重新指向next[j]位置开始重新匹配//下标从0开始int Next[100];int *getNext(const char substr[], int len) { int i = 0, j = -1; Next[0] = -1;//-1时从当前位置下一位置开始比较 while (i < len) { if原创 2020-08-04 16:10:49 · 135 阅读 · 0 评论 -
深度优先搜索(DFS) 广度优先搜索(BFS) 区别
深度优先搜索(DFS):栈的思想广度优先搜索(BFS):队列的思想原创 2020-07-07 00:11:53 · 300 阅读 · 0 评论 -
回溯算法使用场景
判断回溯:拿到一个问题,你感觉如果不穷举一下就没法知道答案,那就可以开始回溯了。一般回溯的问题有三种:Find a path to success 有没有解Find all paths to success 求所有解求所有解的个数求所有解的具体信息Find the best path to success 求最优解回溯法是一个剪枝了的二叉树。我们要得到的结果是可以 good le...原创 2020-04-19 19:58:39 · 514 阅读 · 0 评论 -
使用vector容器的erase()方法时避免踩坑(leetcode 27.移除元素)
对于字符串的操作最常用的是双指针。问题想到vector容器的erase()方法可以删除元素,但是实践中踩了一个大坑——erase()不能删除连续相同的两个元素原因在于——erase操作传入迭代器,迭代器所指位置在删除前后不发生改变,改变的只是容器中元素值。删除该元素后,被删元素后面的所有元素复制到被删元素位置上,尾部迭代器也移动到新的尾部位置。解决当 nums.erase(it)之...原创 2020-04-08 12:24:29 · 983 阅读 · 1 评论 -
一维坐标区间位置的7种关系
//区间位置的7种情况A[a,b] B[c,d] if (b <= c) //不相交 else if (a > c && b < d) { //B包含A } else if (c > a && d < b) {...原创 2020-03-23 13:31:23 · 547 阅读 · 0 评论 -
C/C++实现四舍五入保留指定小数位
C/C++实现四舍五入保留指定小数位number:传入的原始数bits:小数点后保留的维数double round(double number, unsigned int bits) { LL integerPart = number; number -= integerPart; for (unsigned int i = 0; i < bits; ++i...原创 2020-03-19 15:07:51 · 4294 阅读 · 0 评论 -
C++ string库常用函数 字符串处理
实验C++ string库的常用函数#include <bits/stdc++.h>using namespace std;int main() { string str="abc"; cout << str.back()<<endl;//读取末位字符串 cout << str.front()<<endl...原创 2020-03-18 14:54:44 · 392 阅读 · 0 评论 -
序列遍历问题——环形问题
经常会遇到判断相邻几个数之间的关系,那么怎么找出相邻的几个数呢?对于序列: {1, 2, 3, 4, 5, 6} 找出相邻3个数构成的组合 int seq[10] = {1, 2, 3, 4, 5, 6}; int length = 6;//序列长度 int num = 3;//遍历区间长度 if (length >= num) for (i...原创 2020-03-18 12:25:44 · 323 阅读 · 0 评论 -
动态规划5:01背包问题和完全背包问题不同空间复杂度的解法
1.空间复杂度O(V) 时间复杂度O(nV)/* * 01背包问题 * 件数 5 最大重量 8 * 3 5 1 2 2 * 4 5 2 1 3 * */#include "cstdio"#include "cstring"#include "algorithm"#include "vector"using namespace std;const int maxn = 10...原创 2020-03-02 14:31:49 · 4097 阅读 · 0 评论 -
动态规划4:最长回文串(使用状态转移方程优化时间复杂度)
/* * 最长回文子串 * */#include <stdio.h>#include <cstring>const int maxn = 10010;char S[maxn];int dp[maxn][maxn]; //dp[i][j] 表示i到j表示的是不是回文串 1为是int ans = 1;//存放最长长度int main() { fget...原创 2020-02-29 15:51:01 · 287 阅读 · 0 评论 -
C语言fgets()函数的用法总结
从c++11之后取消了gets()函数,用fgets()代替,用法有所不同,这里详细介绍下fgets()该如何使用。char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)第一个参数为数据存放的数组,第二个参数为最大长度,第三个参数为输入源,我们从键盘读入,参数为stdin。#include <s...原创 2020-02-27 12:47:34 · 7698 阅读 · 0 评论 -
动态规划3:最长公共子序列(不连续)
//最长公共子序列--LCS#include <stdio.h>#include "cstring"#include "vector"#include "algorithm"using namespace std;const int N = 100;char A[N], B[N];int dp[N][N];//dp[i][j] A的i号位置与B的j号位置之前的LCS...原创 2020-02-27 11:45:07 · 402 阅读 · 0 评论 -
动态规划2:最长不下降子序列--连续序列+不连续序列
//最长不下降子序列--连续+不连续#include <cstdio>#include "vector"#include "algorithm"using namespace std;const int maxn = 10010;int A[maxn], dp[maxn];//A存放数字序列 dp存放以A[i]结尾的连续序列长度vector<int> a...原创 2020-02-27 00:46:12 · 586 阅读 · 0 评论 -
动态规划1:状态转移方程-求最大子序列和
时间复杂度O(n)序列:-2 11 -4 13 -5 -2//最大连续子序列和//使用到状态转移方程#include <cstdio>#include "algorithm"using namespace std;const int maxn =10010;int A[maxn],dp[maxn];//A存放数字序列 dp存放以A[i]结尾的连续序列最大和int ...原创 2020-02-26 23:13:55 · 1028 阅读 · 0 评论 -
求解菲波那切数列算法耗时比较
#include <ctime>#include <cstdio>#include "algorithm"using namespace std;int dp[1000];clock_t start, finish;double duration;int F1(int n) { if (n == 1 || n == 0) retur...原创 2020-02-25 21:47:53 · 180 阅读 · 0 评论 -
Kruskal算法
/* * Kruskal算法求解最小生成树 * 适用于点多边少的情况 * */#include "cstdio"#include "algorithm"using namespace std;const int MAXV = 110;const int MAXE = 10010;//边集定义部分struct edge { int u, v;//边的两个端点编号 ...原创 2020-02-24 20:35:40 · 166 阅读 · 0 评论 -
并查集 初始化 查找 合并 路径压缩
/* * 并查集 * * */#include <stdio.h>int father[7];//初始化void init(){ for(int i=1;i<7;i++) { father[i]=i; }}//查找根节点int findFather(int x){ if(x==father[x]) ...原创 2020-02-10 13:39:52 · 206 阅读 · 0 评论 -
C语言细节要点易错点学习总结——长期更新
这篇博客主要总结C语言学习中容易出错,理解不到位的一些知识点——学习过程中总结,长期更新。1.指针定义int* p; 等价于 int p;int* p=&a; 等价于 int p; p=&a; 指针p指向变量a的地址。2.数组指针1.数组名可以作为数组首地址使用2.对与数组a[10], a+i 等价于 &a[i]3.a[i] 等价于 *(a+i)3....原创 2020-01-02 14:07:50 · 520 阅读 · 0 评论 -
C语言不常用函数用法总结——长期更新
虽然不常用,万一用到找起来就方便多了。1.memset函数:对数组中的每一个元素赋相同的值(字节赋值——每个字节赋相同的值)memset(数组名,值,sizeof(数组名))建议赋值:0/-1。因为memset函数是按字节赋值的,0的补码全0,-1的补码全1,不容易出错。#include"stdio.h"#include "string.h"int main() { prin...原创 2019-12-31 16:59:26 · 296 阅读 · 0 评论 -
排序算法总结(长期更新)
1.最基础的排序算法冒泡排序思想:每次使用交换的方式将剩余元素中较大的元素放到一端,直到剩余元素为0的时候排序结束。#include<stdio.h>int main() { int a[5] = {1, 3, 5, 10, 7}; printf("排序前:"); for (int i = 0; i < 5; i++) print...原创 2019-12-31 16:42:00 · 209 阅读 · 0 评论