
★、算法笔记
算法笔记
Estrellas_
I am the king of the world.
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
pat a1057 栈【分块思想】
题目:https://www.nowcoder.com/pat/5/submission-detail/64563013https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592一、问题描述Stack is one of the most fundamental data struc...原创 2020-02-02 22:54:46 · 312 阅读 · 0 评论 -
待续....
字符串专题:1、最长回文串;2、KMP算法原创 2020-02-02 17:55:44 · 196 阅读 · 0 评论 -
最长公共子串的长度
输入两个长度均不超过1000的字符串,求它们的最长公共子串的长度。#include<bits/stdc++.h>using namespace std;typedef long long LL;const LL mod=1000000007;const LL p=10000019;const LL maxn=1010; //字符串的最长长度 LL powP...原创 2020-02-01 20:49:39 · 876 阅读 · 0 评论 -
字符串hash
给出N个只有小写字母的字符串,求其中不同的字符串的个数。#include<iostream> #include<string>#include<vector>#include<algorithm>using namespace std;const int mod=1000000007;const int p=10000019...原创 2020-02-01 16:58:19 · 171 阅读 · 0 评论 -
完全背包【动态规划】
注意区分与01背包的不同,v的遍历为正向遍历。for(int i=1;i<=n;i++){ for(int v=w[i];v<=V;v++) { dp[v]=max(dp[v],dp[v-w[i]]+c[i]); } }原创 2020-02-01 16:03:15 · 257 阅读 · 0 评论 -
01背包【动态规划】
1、二维数组解法dp[i][j] : 表示前i件物品放入一个容量为j的背包,可以获得的最大价值。// 初始化dp[0][],dp[][0]为0 for(int i=1;i<=n;i++){ for(int j=1;j<=V;j++) { if(j<w[i]) { dp[i][j]=dp[i-1][j]; } else { ...原创 2020-02-01 15:19:43 · 274 阅读 · 0 评论 -
DAG最长路【动态规划】
1、求解最长路径长度const int maxn=100010;int dp[maxn];memset(dp,0,sizeof(dp));int DP(int i) { if(dp[i]>0) return dp[i]; for(int j=0;j<n;j++) { if(G[i][j]!=inf) { int temp=G[i][j]+DP(...原创 2020-01-31 14:58:01 · 600 阅读 · 0 评论 -
最长回文子串
注意:1、dp[i][j] = 1 :表示 i~j 的子串是回文串。2、从边界出发计算。边界长度为1,长度为2的子串。3、双层遍历:①、遍历长度;②、遍历左端点位置。#include<cstdio> #include<cstring>const int maxn=1010;char S[maxn];int dp[maxn][max...原创 2020-01-29 18:54:14 · 202 阅读 · 0 评论 -
最长公共子序列
dp[i][j] 数组:记录字符串A的i号位,以及字符串B的j号位之前(包含)的最长公共子序列的长度。从边界出发,双重循环,逐个计算。#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const ...原创 2020-01-29 16:22:45 · 198 阅读 · 0 评论 -
最长不下降子序列
用dp【i】数组记录以A【i】结尾的最长不下降子序列的长度。即将每个子问题的结果记录下来。#include<cstdio> #include<algorithm>using namespace std;const int maxn=100;int A[maxn]; //A[i]:第i个位置的元素 int dp[maxn]; //dp[i]:以...原创 2020-01-29 15:24:48 · 309 阅读 · 0 评论 -
最长子序列
1、动态规划解法两个关键:①、无后效性的状态;②、状态转移方程#include<cstdio> #include<algorithm>using namespace std;const int maxn=10010;int A[maxn];int dp[maxn];int main(){ int n; scanf("%d",&am...原创 2020-01-28 22:27:50 · 262 阅读 · 0 评论 -
动态规划
1、动态规划的递归写法求解斐波那契数列。//记忆化搜索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]; }}2、动态规划的递推写法数塔问题。自下...原创 2020-01-28 22:24:22 · 266 阅读 · 0 评论 -
关键路径【有向无环图的最长路径】
1、ve数组的求解ve:顶点(事件)的最早开始时间(时刻)。//拓扑序列 stack<int> topOrder;//拓扑排序,顺便求ve数组 bool topologicalSort(){ queue<int> q; for(int i=0;i<n;i++) //遍历所有点,将入度为0的点加入队列 { if(inDegree[...原创 2020-01-28 19:30:34 · 1344 阅读 · 0 评论 -
拓扑排序
注意:1、队列的使用。//拓扑排序vector<int> G[maxv];int n,m,inDegree[maxv];bool topologicalSort(){ int num=0; queue<int> q; for(int i=0;i<n;i++) //将所有入度为0的点入队 { if(inDegree[i]==0)...原创 2020-01-28 14:51:20 · 200 阅读 · 0 评论 -
kruskal最小生成树算法
注意:1、并查集的巧妙应用。一、大致框架1、边的数据结构定义struct edge{ int u; //u、v: 边的两个端点 int v; int cost;}E[maxe];2、比较函数bool cmp(edge a,edge b){ return a.cost<b.cost;}3、kruskal函数int father[...原创 2020-01-28 14:13:53 · 213 阅读 · 0 评论 -
prim最小生成树算法
const int maxv=1000;const int inf=1000000000;1、邻接矩阵版primint G[maxv][maxv];int n;int d[maxv];bool vis[maxv]={false};int prim(){ fill(d,d+maxv,inf); d[0]=0; int ans=0; for(int i=0;...原创 2020-01-27 22:48:08 · 224 阅读 · 0 评论 -
弗洛伊德算法【全源最短路径】
弗洛伊德算法全源最短路径#include<cstdio>#include<algorithm>using namespace std;const int inf=1000000000;const int maxv=200;int n,m;int dis[maxv][maxv];void Floyd(){ for(int k=0;k<n...原创 2020-01-27 21:06:26 · 280 阅读 · 0 评论 -
SPFA算法求最短路径
vector<Node> adj[maxv];int n,d[maxv],num[maxv];bool inq[maxv];bool SPFA(int s){ memset(inq,false,sizeof(inq)); memset(num,0,sizeof(num)); fill(d,d+maxv,inf); queue<int> q; q.p...原创 2020-01-27 17:44:37 · 336 阅读 · 0 评论 -
pat a1003 Bellman-Ford求最短路径
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376#include<cstdio>#include<cstring>#include<vector>#include<set>#include<algorithm&...原创 2020-01-27 16:40:09 · 271 阅读 · 0 评论 -
Bellman-Ford算法求最短路径
struct Node{ int v; int dis;};vector<Node> adj[maxv]; //图的邻接表 int n; //n为顶点数 int d[maxv]; //从起点到各点的最短路径长度 bool Bellman(int s){ fill(d,d+maxv,inf); d[s]=0; //求数组d for(int ...原创 2020-01-27 16:39:24 · 332 阅读 · 0 评论 -
pat a1030 Travel Plan 【Dijkstra求最短路径】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392https://www.nowcoder.com/pat/1/submission-detail/64261014一、问题描述A traveler's map gives the distances between c...原创 2020-01-26 20:45:30 · 252 阅读 · 0 评论 -
Dijkstra+DFS
1、使用Dijkstra算法记录所有的最短路径vector<int> pre[maxv];void Dijkstra(int s) { fill(d,d+maxv,inf); d[s]=0; for(int i=0;i<n;i++) { int u=-1; int min=inf; for(int j=0;j<n;j++) { if(...原创 2020-01-26 15:57:39 · 309 阅读 · 1 评论 -
pat a1003 Emergency 【dijkstra求最短路径】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376一、问题描述As an emergency rescue team leader of a city, you are given a special map of your country. The map shows ...原创 2020-01-26 14:37:14 · 255 阅读 · 0 评论 -
Dijkstra求最短路径
const int maxv=1000;const int inf=1000000000;1、全局变量int G[maxv][maxv];int n;int d[maxv]; //起点到各点的最短路径长度 bool vis[maxv]={false};int pre[maxv];2、Dijkstra实现void Dijkstra(int s) { fill(d...原创 2020-01-25 18:06:01 · 289 阅读 · 0 评论 -
Dijkstra求最短距离
const int maxv=1000;const int inf=1000000000;1、邻接矩阵版Dijkstraconst int maxv=1000;const int inf=1000000000;//1、邻接矩阵版Dijkstraint G[maxv][maxv];int n;int d[maxv]; //起点到各点的最短路径长度 bool vis[m...原创 2020-01-25 15:16:35 · 674 阅读 · 0 评论 -
pat a1076 微博转发人数_广搜
一、问题描述1076Forwards on Weibo(30分)Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is fo...原创 2020-01-23 21:06:03 · 221 阅读 · 0 评论 -
图的广搜
1、邻接矩阵版BFSint n; int 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&...原创 2020-01-23 12:47:58 · 259 阅读 · 1 评论 -
pat a1034 团伙头目【图的深搜】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624一、问题描述One way that the police finds the head of a gang is to check people's phone calls. If there is a phone ca...原创 2020-01-22 20:37:08 · 313 阅读 · 0 评论 -
string转int函数
string转int函数map<int,string> intToString;map<string,int> stringToInt;int numPerson=0; //总人数 int change(string str){ if(stringToInt.find(str)!=stringToInt.end()) //能找到 { r...原创 2020-01-22 20:36:55 · 569 阅读 · 0 评论 -
图的深搜
通用模板(思路)DFS(u){ visit[u]=true; for(从u出发所能到达的所有顶点v) { if visit[v]==false DFS(v) }}DFSTrave(G){ for(G的所有顶点u) if visit[u]==false DFS(u); }const int maxv=1000;const int inf=100...原创 2020-01-22 19:29:23 · 314 阅读 · 0 评论 -
codeup 21142: 合并果子【哈夫曼树】
一、题目描述在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力...原创 2020-01-22 14:07:22 · 237 阅读 · 0 评论 -
堆的基本操作
const int maxn=100;int heap[maxn]; //堆 int n=10; //元素个数 1、向下调整对heap数组在【low,high】范围进行向下调整其中low为欲调整结点的数组下标,high一般为堆的最后一个元素的数组下标//对heap数组在【low,high】范围进行向下调整 //其中low为欲调整结点的数组下标,high一般为堆...原创 2020-01-21 22:01:58 · 348 阅读 · 0 评论 -
好朋友【并查集】
一、问题描述 有一个叫做“数码世界”奇异空间,在数码世界里生活着许许多多的数码宝贝,其中有些数码宝贝之间可能是好朋友,并且数码宝贝世界有两条不成文的规定: 第一,数码宝贝A和数码宝贝B是好朋友等价于数码宝贝B与数码宝贝A是好朋友 第二,如果数码宝贝A和数码宝贝C是好朋友,而数码宝贝B和数码宝贝C也是好朋友,那么A和B也是好朋友,现在给出这些数码宝...原创 2020-01-21 20:18:53 · 411 阅读 · 0 评论 -
并查集的基本操作
1、初始化for(int i=1,i<=N;i++) { father[i]=i;} 2、查找int findFather(int x) { while(x!=father[x]) { x=father[x]; } return x;}递归版本:int findFather(int x) //递归版本 { if(x==father[x...原创 2020-01-21 18:08:06 · 303 阅读 · 0 评论 -
平衡二叉树的基本操作
1、结点数据结构定义struct Node{ int v; int height; Node* lchild; Node* rchild;};2、新建结点Node* newNode(int v){ Node* node=new Node; node->v=v; node->height=1; node->lchild=node->rch...原创 2020-01-21 16:14:05 · 285 阅读 · 0 评论 -
pat a1043 二叉排序树 镜像树
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856https://www.nowcoder.com/pat/5/submission-detail/64125906一、题目描述PAT a1043Is It a Binary Search Tree(25分)A ...原创 2020-01-20 23:18:02 · 321 阅读 · 0 评论 -
二叉查找树的基本操作
1、查找void search(Node* root,int x){ if(root==NULL) { printf("search failed\n"); return; } if(x==root->data) { printf("%d\n",root->data); } else if(x<root->data) { sear...原创 2020-01-20 21:24:04 · 208 阅读 · 0 评论 -
pat a1053 给定权值和,求路径
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512https://www.nowcoder.com/pat/5/submission-detail/641140481053Path of Equal Weight(30分)Given a non-empty tree...原创 2020-01-20 17:51:49 · 354 阅读 · 0 评论 -
树的遍历
1、树的先根遍历//树的先根遍历 void preOrder(int root) { printf("%d",node[root].data); for(int i=0;i<node[root].child.size();i++) { preOrder(node[root].child[i]); }}2、树的层序遍历//树的层序遍历void LayerO...原创 2020-01-19 21:24:38 · 174 阅读 · 0 评论 -
pat a1020 后序+中序,建树,并求层序序列
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列。#include<cstdio>#include<cstring>#include<queue>#incl...原创 2020-01-19 20:21:01 · 266 阅读 · 0 评论