- 博客(87)
- 收藏
- 关注
原创 树状数组例题
/* 给定一个有N个正整数的序列A(N<=10^5,A[i]<=10^5),对序列中的每个数, 求出序列中它左边比它小的数的个数 */ #include<cstdio>#include<cstring> const int maxn = 100010;#define lowbit(i) ((i)&(-i)) //lowbit写成宏定义的形式,注意括号 int c[maxn]; //树状数组 //update函数将第x个整数加上y void
2021-08-11 18:49:07
249
原创 PAT-A1057 Stack-分块思想
#include<cstdio>#include<cstring>#include<stack>using namespace std;const int maxn = 100010;const int sqrN = 316; //sqrt(100001),表示块内元素个数 stack<int> st; //栈 int block[sqrN]; //记录每一块中存在的元素个数 int table[maxn]; //hash数组,记录元素当前存在
2021-08-11 14:37:53
166
原创 KMP算法
#include<iostream>#include<cstring>using namespace std;const int MAXN = 10010;int next[MAXN];//getNext求解长度为len的字符串s的next数组 void getNext(char s[], int len){ int j = -1; next[0] = -1; //初始化j = next[0] = -1 for(int i = 1; i < len; i
2021-08-10 14:43:54
200
原创 最长回文子串hash+二分
#include<iostream>#include<cstdio>#include<string>#include<vector>#include<algorithm>using namespace std;typedef long long LL;const LL MOD = 1000000007; //MOD为计算hash值时的模数 const LL P = 10000019; //P为计算hash值时的进制数 const
2021-08-04 16:18:50
306
原创 求最长公共子串的长度
#include<iostream>#include<cstdio>#include<string>#include<vector>#include<map>#include<algorithm>using namespace std;typedef long long LL;const LL MOD = 10000000007; //MOD为计算hash值时的模数 const LL P = 10000019; //
2021-08-04 14:51:57
131
原创 字符串hash
//给出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)
2021-08-04 14:09:54
68
原创 完全背包问题
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100; //物品最大件数 const int maxv = 1000; //V的上限 int w[maxn], c[maxn], dp[maxv];int main(){ int n, V; scanf("%d%d", &n, &V); for(int i = 1; i <= n; i++){
2021-08-03 15:23:17
61
原创 01背包问题
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100; //物品最大件数 const int maxv = 1000; //V的上限 int w[maxn], c[maxn], dp[maxv];int main(){ int n, V; scanf("%d%d", &n, &V); for(int i = 1; i <= n; i++){
2021-08-03 15:09:48
66
原创 DAG最长路
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 1010;const int INF = 1000000000;int dp[maxn];int G[maxn][maxn];int choice[maxn];int vis[maxn];int n;//DAG最长路 int DP(int i){ if(dp[i] > 0) return dp[i]; //dp
2021-08-03 14:25:11
101
原创 最长回文子串
#include<cstdio>#include<cstring>const int maxn = 1010;char S[maxn];int dp[maxn][maxn]; //dp[i][j]表示S[i]至S[j]所表示的子串是否是回文子串 int main(){ gets(S); int len = strlen(S), ans = 1; memset(dp, 0, sizeof(dp)); //dp数组初始化为0 //边界 for(int i =
2021-07-29 17:22:09
56
原创 LCS最长公共子序列
#include<cstdio>#include<cstring>#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长度 int main(){ int n; gets(A + 1); //从下标为1开始读入 gets(B + 1); int lenA = st
2021-07-29 17:11:45
66
原创 LIS最长不下降子序列
#include<cstdio>#include<algorithm>using namespace std;const int N = 100;int A[N], dp[N];int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d", &A[i]); } int ans = -1; //记录最大的dp[i] for(int i = 1;
2021-07-29 16:50:42
100
原创 最大连续子序列和
#include<cstdio>#include<algorithm> using namespace std;const int maxn = 10010;int A[maxn], dp[maxn]; //A[i]存放序列,dp[i]存放以A[i]结尾的连续序列的最大和 int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; i++){ //读入序列 scanf("%d", &am
2021-07-29 16:36:45
61
原创 动态规划数塔问题的递推写法
#include<cstdio>#include<algorithm>using namespace std;//数塔问题,求路径上所有数字相加后得到的和最大是多少 const int maxn = 1000;int f[maxn][maxn], dp[maxn][maxn];int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ for(int j = 1; j <
2021-07-29 16:12:57
108
原创 求解关键路径
#include<cstdio>#include<stack>#include<queue>#include<cstring>using namespace std;const int MAXV = 1000;struct Node{ int v, w; //v为边的目标顶点,w为边权 };int n, inDegree[MAXV]; //顶点数、入度 vector<Node> G[MAXV]; //邻接表int ve[
2021-07-28 16:40:24
81
原创 拓扑排序-判断有向无环图
#include<cstdio>#include<vector>#include<queue>using namespace std;const int MAXV = 1000;vector<int> G[MAXV]; //邻接表 int n, m, inDegree[MAXV]; //顶点数、入度 //拓扑排序 bool topologicalSort(){ int num = 0; //记录加入拓扑序列的顶点数 queue<
2021-07-27 17:50:18
332
原创 prim算法
#include<cstdio>#include<vector>using namespace std;const int MAXV = 1000; //最大顶点数 const int INF = 1000000000; //设INF为一个很大的数 //邻接矩阵版 int n, G[MAXV][MAXV]; //n为顶点数,MAXV为最大顶点数 int d[MAXV]; //顶点与集合S的最短距离 bool vis[MAXV] = {false}; //标记数组
2021-07-27 16:12:38
88
原创 Floyd算法
#include<cstdio>#include<algorithm>using namespace std;const int INF = 1000000000;const int MAXV = 200; //MAXV为最大顶点数 int n, m; //n为顶点数,m为边数 int dis[MAXV][MAXV]; //dis[i][j]表示顶点i和顶点j的最短距离 void Floyd(){ for(int k = 0; k < n; k++){
2021-07-23 15:17:34
89
原创 SPFA算法 判断可达负环
#include<cstdio>#include<vector>#include<queue>using namespace std;const int MAXV = 10010; //MAXV为最大顶点数 const int INF = 1000000000;struct Node{ int v, dis; //v为邻接边的目标顶点,dis为邻接边的边权 };vector<Node> Adj[MAXV]; //图G的邻接表 int n
2021-07-23 14:38:02
95
原创 PAT-A1003 Emergency-Bellman-Ford算法
#include<cstdio>#include<cstring>#include<vector>#include<set>#include<algorithm>using namespace std;const int MAXV = 510;const int INF = 0x3fffffff;struct Node{ int v, dis; //v为邻接边的目标顶点,dis为邻接边的边权 Node(int _v, in
2021-07-22 17:26:59
125
原创 PAT-A1030 Travel Plan(Dijkstra)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXV = 510; //最大顶点数 const int INF = 1000000000; //无穷大//n为顶点数,m为边数,st为起点,ed为终点//G为距离矩阵,cost为花费矩阵 //d[]记录最短距离,c[]记录最小花费 int n, m, st, ed, G[MAXV][MA
2021-07-16 14:56:04
86
原创 PAT-A1003 Emergency-Dijkstra算法
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXV = 510; //最大顶点数 const int INF = 1000000000; //无穷大 //n为顶点数,m为边数,st为起点,ed为终点//G为邻接矩阵,weight为点权 int n, m, st, ed, G[MAXV][MAXV], weight[MAXV];//
2021-07-15 14:51:14
141
原创 Dijkstra算法
#include<cstdio>#include<vector>using namespace std;const int MAXV = 1000; //最大顶点数 const int INF = 1000000000; //设INF为一个很大的数 //邻接矩阵版 int n, G[MAXV][MAXV]; //n为顶点数 int d[MAXV]; //起点到达各点的最短路径长度 bool vis[MAXV] = {false}; //标记数组,vis[i]
2021-07-13 17:19:55
62
原创 PAT-A1076 Forwards on Weibo 图BFS
#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}
2021-07-13 15:55:16
85
原创 图的BFS
#include<queue> using namespace std;const int MAXV = 1000; //最大顶点数 const int INF = 1000000000; //INF一个很大的数,两点不可到达struct Node{ int v; //顶点编号 int layer; //顶点层号 }; //邻接矩阵版 int n, G[MAXV][MAXV]; //n为顶点数,MAXV为最大顶点数 bool inq[MAXV] = {false};
2021-07-10 17:38:42
65
原创 图的DFS
#include<vector>using namespace std;const int MAXV = 1000; //最大顶点数 const int INF = 1000000000; //INF一个很大的数,两点不可到达 //邻接矩阵版 int n, G[MAXV][MAXV]; //n为顶点数 bool vis[MAXV] = {false}; //如果顶点i已被访问,则vis[i] == true void DFS(int u, int depth){ //u为当
2021-07-10 16:37:49
58
原创 PAT-A1034 Head of a Gang
#include<iostream>#include<string>#include<map>using namespace std;const int maxn = 2010; //总人数 map<int, string> intToString; //编号->姓名 map<string, int> stringToInt; //姓名->编号 map<string, int> Gang; //head-&
2021-07-10 16:37:07
75
原创 codeup-21142 合并果子(哈夫曼树)
/*在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,
2021-07-09 15:23:18
69
原创 堆的基本操作
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100;//用数组来存储完全二叉树,第一个结点从1开始int heap[maxn] = {0, 85, 55, 82, 57, 68, 92, 99, 98, 66, 56};int n = 10;//low为欲调整结点的数组下标,在[low, high]范围进行向下调整 void downAdjust(int low,
2021-07-09 14:44:24
62
原创 并查集例题-好朋友
/*有一个叫做“数码世界”奇异空间,在数码世界里生活着许许多多的数码宝贝, 其中有些数码宝贝之间可能是好朋友,并且数码宝贝世界有两条不成文的规定:第一,数码宝贝A和数码宝贝B是好朋友等价于数码宝贝B与数码宝贝A是好朋友第二,如果数码宝贝A和数码宝贝C是好朋友, 而数码宝贝B和数码宝贝C也是好朋友,那么A和B也是好朋友现在给出这些数码宝贝中所有好朋友的信息问:可以把这些数码宝贝分成多少组,满足每组中的任意两个数码宝贝都是好朋友,而且任意两组之间的数码宝贝都不是好朋友*/#include&l
2021-07-08 15:38:24
93
原创 并查集的基本操作
#include<cstdio>//并查集用数组实现//father[a] = b, 元素a的父亲结点为b//father[i] = i, 元素i是该集合的根结点,同一个集合只存在一个根结点 const int N = 100;int father[N] = {0};int findFather(int x){ while(x != father[x]){ x = father[x]; } return x;}//递归实现 int findFather2(in
2021-07-08 15:17:36
53
原创 二叉查找树BST的基本操作
#include<cstdio>struct node{ int data; node *left, *right;};node* newNode(int v){ node* Node = new node; Node->data = v; Node->left = Node->right = NULL; return Node;}void search(node* root, int x){ if(root == NULL){ //空树,查找失败
2021-07-07 17:13:58
70
原创 PAT-A1043 Is It a Binary Search Tree
#include<cstdio>#include<vector>using namespace std;struct node{ int data; node *left, *right;}; void insert(node* &root, int data){ if(root == NULL){ root = new node; root->data = data; root->left = root->right = N
2021-07-07 17:03:46
64
原创 PAT-A1053 Path of Equal Weight
#include<cstdio> #include<vector>#include<algorithm>using namespace std;const int MAXN = 110;struct node{ int weight; vector<int> child;} Node[MAXN];bool cmp(int a, int b){ return Node[a].weight > Node[b].weight;}i
2021-07-07 16:51:40
65
原创 树的静态实现
#include<cstdio>#include<vector>#include<queue>using namespace std;const int maxn = 100;struct node{ int layer; //记录层号 int data; //数据域 vector<int> child; //指针域,存放所有子结点的下标 } Node[maxn]; //结点数组,maxn为结点上限个数 int index = 0
2021-07-07 14:15:22
65
原创 二叉树的静态实现
#include<cstdio>#include<queue>using namespace std;const int maxn = 100;struct node{ int data; int lchild; int rchild;} Node[maxn]; int index = 0;int newNode(int v){ Node[index].data = v; Node[index].lchild = -1; Node[index].rc
2021-07-06 15:59:13
57
原创 PAT-A1020 TreeTraversals
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn = 50;struct node{ int data; node* lchild; node* rchild; };int pre[maxn], in[maxn], post[maxn];int n;node* create(
2021-07-06 15:31:52
58
原创 二叉树的基本操作
#include<iostream>#include<queue>using namespace std;//二叉树的存储结构 struct node{ int data; int layer; //层次 node* lchild; node* rchild;};//新建结点 node* newNode(int v){ node* Node = new node; Node->data = v; Node->lchild = Node-
2021-07-06 14:54:55
66
原创 BFS例题3-STL的queue,入队的是元素的副本
/* 使用STL的queue时,元素入队的push操作只是制造了该元素的一个副本入队 入队后对原元素的修改不会影响队列中的副本,对队列中副本的修改不会改变原元素 */ #include<cstdio>#include<queue>using namespace std;struct node{ int data;}a[10];int main(){ queue<node> q; for(int i = 1; i <= 3; i++){
2021-07-02 11:43:13
109
原创 BFS例题2-迷宫最少步数
/* n*m的迷宫,"*"代表墙壁,"."代表平地,"S"代表起点,"T"代表终点。 求从起点到终点的最少步数 */#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 100;struct node{ int x, y; //位置(x, y) int step; //step为最少步数,即层数 } S, T, Node;
2021-07-02 11:26:21
238
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人