自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

小魚兒

天下皆白,唯我独黑!

  • 博客(28)
  • 收藏
  • 关注

原创 PAT A1068 Find More Coins (30 分)(背包问题)

1068 Find More Coins (30 分)Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds o...

2019-11-14 15:25:00 154

原创 11.7 背包问题(完全背包)基础算法

#include <cstdio>//完全背包问题——物品件数无穷型 for(int i=1;i<=n;++i){ for(int v=w[i];v<=V;++v) { //有两种选择,选第i件物品,不选第i件物品 dp[i][v]=max(dp[i-1][v],dp[i][v-w[i]]+c[i]); //与01背包唯一区别是第二个参数是dp[i]...

2019-11-14 14:53:01 184

原创 11.7 背包问题(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",&...

2019-11-14 14:41:37 5901

原创 11.7 背包问题(01背包)基础算法

#include <cstdio>//01背包问题for(int i=1;i<=n;++i){ for(int v=w[i];v<=V;++v) { //有两种选择,选第i件物品,不选第i件物品 dp[i][v]=max(dp[i-1][v],dp[i-1][v-w[i]]+c[i]); }}//利用滚动数组进行空间优化for(int i=...

2019-11-14 14:29:54 144

原创 PAT A 1040 Longest Symmetric String (25 分)

1040 Longest Symmetric String (25 分)Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub...

2019-11-13 15:41:51 96

原创 11.5 最长回文字串(动态规划)

#include <cstdio>#include <cstring>const int maxn=1010;char s[maxn];int dp[maxn][maxn];int main(){ gets(s); int len =strlen(s),ans=1; memset(dp,0,sizeof(dp)); //边界 for(int i=0;...

2019-11-13 15:11:10 96

原创 11.4 最长公共子序列(LCS)——动态规划

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=100;char a[N],b[N];int dp[N][N];int main(){ gets(a+1);//从下标1开始读入 gets(b+1); int len...

2019-11-13 14:37:02 76

原创 28 、判断子串在str中出现的次数

#include <cstdio>#include <cstring> using namespace std;//判断子串出现的次数 int occurNum(string str,string substr){ int i=j=k=sum=0; //k记录每次判断的起始位置 while(i<str.size()) { if(str[i]==...

2019-11-12 20:24:56 111

原创 PAT A 1045 Favorite Color Stripe (30 分)(变体形式的最长不下降子序列)

1045 Favorite Color Stripe (30 分)Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pi...

2019-11-12 15:30:43 158

原创 11.3 最长不下降子序列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])...

2019-11-12 14:59:54 111

原创 PAT A 1007 Maximum Subsequence Sum (25 分)(最大连续子列和——动态规划)

1007 Maximum Subsequence Sum (25 分)Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Su...

2019-11-12 14:18:23 119

原创 11.2 Code Up 问题 A: 最大连续子序列(动态规划——含起始与结束点的确定)

问题 A: 最大连续子序列题目描述给定K个整数的序列{N1,N2,...,NK},其任意连续子序列可表示为{Ni,Ni+1,...,Nj},其中1<=i<=j<=K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{-2,11,-4,13,-5,-2},其最大连续子序列为{11,-4,13},最大...

2019-11-10 15:33:08 258

原创 11.2 最大连续子序列和——动态规划

如何求最大连续子序列和?1、如果只有一个数,则便是其本身。2、有多个数的数组A,如-2,11,-3,13;则最大子序列和为 11-3+13=20.我们知道此序列第二个序列前的最大值为11,前三个的最大和为11-3=8,若利用dp数组存储第i个位置的最大子序列和,则dp[3]=dp[2]+A[3];由此可得状态转移方程:dp[i]=max(A[i],dp[i-1]+A[i]);参考代...

2019-11-10 14:39:29 190 1

原创 11.1 code up 问题 A: Fibonacci(动态规划递归写法)

问题 A: Fibonacci题目描述The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence:F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2Write a program to calculate the Fibonacci Numbers.输入Eac...

2019-11-09 15:28:28 125

原创 11.1.3 动态规划递推写法(数塔问题)

//数塔问题——从塔顶向下的路径最大值#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...

2019-11-09 15:12:30 113

原创 11.1 动态规划初步(递归写法)

#include <cstdio>using namespace std;//11 动态规划 //1、坡博纳妾数列//1.1递归写法int F(int n){ if(n==0||n==1) return 1; else return F(n-1)+F(n-2);}// 1.2利用dp数组存储已经计算过的结果——记忆化搜索 int dp[maxn]...

2019-11-09 14:54:57 215

原创 10.6 拓扑排序(判断给定图是否是有向无环图)

//拓扑排序#include <cstdio>#include <vector>#include <queue>using namespace std;vector<int> G[maxv]; //邻接表int n,m,inDegree[maxv]; //顶点数、入度 bool topologicalSort() //拓扑排序 {...

2019-11-09 14:31:08 383

原创 10.5.3 最小生成树——kruskal算法(测试用例)

#include <cstdio>#include <algorithm>using namespace std;const int maxv=110;const int maxe=10020;//边集定义部分struct edge{ int u,v; //边的两个端点编号 int w; //边权 }E[maxe]; bool cmp(edge a ...

2019-11-07 15:27:22 477

原创 10.5.3 最小生成树——kruskal算法

#include <cstdio>#include <algorithm>using namespace std;//最小生成树——kruskal算法struct edge{ //边的定义 int u,v;//边的两个端点编号 int w; //边的边权 }E[maxe];//最多有maxe条边bool cmp(edge a,edge b)//排序函数...

2019-11-07 15:08:52 151

原创 10.4 最小生成树——prim算法(测试用例)

#include <cstdio>#include <algorithm>using namespace std;const int maxv=1010;const int INF=0x3fffffff;int n ,m,G[maxv][maxv];int d[maxv];bool vis[maxv]={false};int prim(){ fill...

2019-11-07 14:38:14 1751

原创 10.5 最小生成树——prim算法

#include<cstdio>#include <vector>using namespace std;//求最小生成树——Prim算法——从一点出发逐步扩散求解//1、伪代码 prim(G,d[]){ 初始化; for(循环n次) { u=使d[u]最小的还未被访问的顶点的编号; 记u以访问; for(从u出发能到达的所有顶点v) {...

2019-11-06 15:51:30 266

原创 10.4.3 全源最短路径——Floyd算法

#include <cstdio>#include <algorithm>using namespace std;//Floyd 算法——全源最短路径//伪代码/*枚举顶点k∈[1,n] 以顶点k作为中介点,枚举所有顶点对i和j(i∈[1,n],j∈[1,n] ) 如果dis[i][k]+dis[k][j]<dis[i][j]成立 //找到以k为中...

2019-11-06 15:04:36 182

原创 SPFA算法——优化后的Bellman算法

#include <cstdio>#include <queue>#include <vector>using namespace std;//优化后的Bellman算法——SPFA//1、伪代码queue <int> q;q.push(s) ;//源点s入队while(!q.empty()) //队列非空{ int u=q...

2019-11-06 14:38:57 128

原创 10.4 PAT A1003 Emergency (25 分)(最短路径及最大点权——Bellman算法实现)

1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams...

2019-11-05 15:24:19 192

原创 10.4 Bellman-Ford 算法求最短路径(可求解负权值)(基础算法)

//Bellman-Ford 算法求最短路径(可求解负权值)//伪代码for(int i=0;i<n-1;++i) //执行n-1轮操作 { for(each edge u->v) { if(d[u]+length[u->v]<d[v]) //以u为中介点可以使d[v]更小 d[v]=d[u]+length[u->v]; //松弛操作 }...

2019-11-05 14:55:06 149

原创 PAT 10.4 A 1030 Travel Plan (30 分)(最短路径及最小花费——Dijkstra算法)

1030 Travel Plan (30 分)A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to d...

2019-11-03 15:55:24 151

原创 最短路径第二尺度求解优化算法(Dijkstra+DSF)

#include <cstdio>#include <cstring> #include <algorithm>#include <vector>using namespace std;const int maxv=520;const int INF=1000000000;//利用pre数组先求出每个点的点权或边权之和的模板。可直...

2019-11-02 16:04:42 362

原创 PAT A 1003 Emergency (25 分)(图的最短路径条数及最大点权)(Dijkstra算法)

1003 Emergency (25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams...

2019-11-02 15:20:52 160 1

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除