
算法设计与分析
乔卿
寻找灵感而不是寻找答案。
展开
-
国科大.算法设计与分析.2022期末考试真题回忆版
目录分而治之贪心动态规划线性规划:搬家问题网络流Bonus分而治之找众数贪心找数组中的子数组,和为target,问最多能找到多少个?动态规划字符串A和字符串B,每次可以删除一个字符,问最少的删除次数,能够使得这两个字符串相等。线性规划:搬家问题卡车每次容量为Q,共m个箱子和n个物品都要搬走,每个箱子和每个物品都有容量,物品要放到箱子里,一个箱子可以放多件物品。最小化卡车搬运趟数。网络流比所有的作业题都简单BonusN个学生,每人一个成绩。range为同一组内最大成绩减最小成绩。●原创 2022-02-24 21:39:24 · 2381 阅读 · 1 评论 -
国科大.算法设计与分析:Latex模板(2021)
\documentclass{article}\usepackage{fancyhdr}\usepackage{extramarks}\usepackage{amsmath}\usepackage{amsthm}\usepackage{amsfonts}\usepackage{tikz}% \usepackage[fontset=ubuntu]{ctex}\usepackage[UTF8, scheme=plain, punct=plain, zihao=false]{ctex}\use.原创 2022-01-16 21:59:10 · 1129 阅读 · 0 评论 -
国科大.算法设计与分析:String Partition(C++实现)
Given a string s, we want to partition the string into as many parts as possible so that each letter appears in at most one part. Return a list of integers representing the size of these parts.要求将字符串分成若干部分,每个字符最多出现在一个部分。分成的部分越多越好。解题思路就是一个很简单的贪心:找每个字符原创 2022-01-16 21:55:54 · 352 阅读 · 0 评论 -
国科大.算法设计与分析:Valid Parenthesis String(C++实现)
给定一个由(、)、*组成的字符串,其中*可以是左括号、右括号或空字符,问该字符串是否是合法的。实现如下:#include <iostream>using namespace std;bool judge(string str) { int count=0; for(int i=0; i<str.length(); i++) { if(str[i]==')') count--; else count++; if(count<0) return fal..原创 2022-01-16 21:53:09 · 281 阅读 · 0 评论 -
最短路径:一文学会Bellman-Ford算法与Yen的改进算法
Bellman-Ford算法是一个经典的最短路径算法。这个算法的思想很简单,我们先来简单回顾一下。以w(u,v)表示顶点u出发到顶点v的边的权值,以 d[v] 表示当前从起点 s 到顶点 v 的路径权值,我们不断优化d[v],最终使d[v]存放着从起点s到顶点v的最短路径。那么,如何优化d[v]呢?我们引入松弛函数,并作如下定义:若存在边 w(u,v),使得:d[v] > d[u]+w(u,v)则更新 d[v] 值:d[v] = d[u] + w(u,v)所以松弛函数的作用原创 2021-12-17 18:07:36 · 1331 阅读 · 0 评论 -
国科大.算法设计与分析:Maximum Alternating Subsequence Sum(Python实现)
个人认为这是一道非常精巧的动归题目。The alternating sum of a 0-indexed array is defined as the sum of the elements at even indices minus the sum of the elements at odd indices.For example, the alternating sum of [4,2,5,3] is (4 + 5) - (2 + 3) = 4.Given an array nums, re原创 2021-11-10 16:48:22 · 743 阅读 · 0 评论 -
国科大.算法设计与分析:Triangle(C++实现)
Given a triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i +1on the next row原创 2021-11-10 16:41:04 · 1015 阅读 · 0 评论 -
国科大.算法设计与分析:First and Last Position Search二分查找某一数字的范围(C++实现)
Given an array of integers numbers sorted in ascending order, find the starting and ending position of several given target values. If a target is not found in the array, return [-1, -1]. You must write an algorithm with O(logn) runtime complexity for eac.原创 2021-10-26 23:27:36 · 245 阅读 · 0 评论 -
国科大.算法设计与分析:Fast Mod Exponentiation快速模幂(C++实现)
Bob has encountered a difficult problem, and hope you design an algorithm to calculate pow(a,b) mod 1337, where a is a positive integer, b is a very large positive integer and will be given in the form of an array. For example, pow(2,3) mod 1337 is 8.#.原创 2021-10-26 23:25:02 · 391 阅读 · 0 评论