2023 Blue Bridge Cup algorithm learning route and basic knowledge compilation

Preface

The Blue Bridge Cup is a well-known computer competition. To stand out in the competition, you must master extensive algorithm knowledge. This blog will provide you with a complete learning route to help you gradually master the mathematical foundation, data structure, basic algorithms and other key knowledge required for the Blue Bridge Cup.

Mathematical basis

1.1 Simple Mathematics

  • Basic operations: multiplication, division, power, logarithms.
  • Mathematical concepts: factorial, date processing.

1.2 Case

Case 1: Use factorial to calculate the number of permutations
Problem description: Given n elements, calculate the number of their permutations.
Solution: Use factorial to calculate the number of permutations, which is n!.

Case 2: Calculation of the number of days difference between dates
Problem description: Given two dates, calculate the number of days difference between them.
Workaround: Use date processing methods to calculate the difference in days between two dates.

2. 1 Combinatorial Mathematics

  • Permutations: Basic concepts and applications.
  • Special sequences and numbers: Yang Hui's triangle, staggered formulas, Cattleya numbers.
  • Mathematical principles: recursion, pigeonhole principle, inclusion-exclusion principle.

2.2 Case

Case 1: Using Cattleya numbers to solve the path counting problem
Problem description: There is a grid, from the upper left corner to the lower right corner, only Can move to the right or down, find the number of paths that do not pass through the diagonal.
Solution: Use Cattleya's number to calculate the number of paths.

Case 2: Apply the pigeonhole principle to solve distribution problems
Problem description: There are m items and n boxes, and the items must be distributed evenly into the box and find the number of allocation options.
Solution: Use the pigeonhole principle to solve the allocation problem.

3. 1 Linear algebra

  • Gaussian elimination: solving systems of linear equations.
  • Matrix operations: matrix multiplication and its applications.

3.2 Case

Case 1: Use Gaussian elimination to solve the encryption problem
Problem description: Given a linear equation system and a key, decrypt the message.
Solution: Use Gaussian elimination to solve the system of linear equations to decrypt the message.

Case 2: Using matrix operations for image processing
Problem description: Perform blur processing or specific transformation on an image.
Solution: Use matrix operations to implement image processing algorithms.

4. 1 Advanced Mathematics

  • Distance measures: Hamming distance, Manhattan distance, Euclidean distance.
  • Integration method: Simpson integration.
  • Analytical geometry: positional relationships and properties of points, lines, and surfaces.

4.2 Case

Case 1: Use Euclidean distance for the closest point pair problem
Problem description: Given a set of points, find the closest point a pair of points.
Solution: Use Euclidean distance to calculate the distance between points and then find the minimum distance.

Case 2: Applying Simpson's integral to solve practical physical problems
Problem description: Solve the area under a complex curve, which represents a certain physical quantity.
Solution: Use Simpson's integral method to estimate the area under the curve.

computational geometry

1.1Basic concepts

  • Distance: Introduces the distance calculation method from point to point, point to line, and point to plane.
  • Hamming distance and Manhattan distance: Give examples to illustrate their application in practical problems, such as edit distance, path planning, etc.
  • Projection: Emphasizes the importance of projection in geometric problems, such as point-to-line projection, and its applications in computation.

1.2 Case

Case 1: Calculation of distance from point to line
Problem description: Given a point and a straight line, calculate the shortest distance from the point to the line.
Solution: Use the distance formula from point to line to calculate.

Case 2: Application of Manhattan distance in path planning
Problem description: Plan a path in the city to minimize the total driving distance.
Solution: Use Manhattan distance as the distance metric to solve the path planning problem.

2.1 Basic operations

  • Cross product and dot product: Provide examples. For example, cross product is used to calculate area, normal vector, etc., and dot product is used to determine angles and projections, etc.
  • Rotation: Covers two- and three-dimensional rotations and their applications in graphics processing.
  • Collinearity and straight line intersection: examples of applications to geometric problems and computer graphics.
  • Area and Perimeter: Provides calculation formulas for different shapes, such as rectangles, triangles, polygons, etc.

2.2 Case

Case 1: Application of cross product and dot product in three-dimensional space
Question description: Calculate the angle between two vectors, area etc.
Solution: Use cross products and dot products to calculate the relevant geometric quantities.

Case 2: Application of rotation matrix
Problem description: Rotate a graphic.
Solution: Use the rotation matrix to realize the rotation operation of the graphics.

3.1 circle

  • Add circle-related formulas, such as the calculation of the area and circumference of a circle.
  • Provides calculation examples of intersections between circles and straight lines, circles and circles.

3.2 Case

Case 1: Calculate the intersection point of a circle and a straight line
Problem description: Given the equations of a circle and a straight line, calculate the intersection point.
Solution: Use the equation of the circle and the equation of the line to solve for the intersection.

Number Theory

1.1 Basic knowledge

  • Parity: Explain the properties of odd and even numbers, such as the properties of adding and multiplying odd numbers.
  • Base: Detailed introduction to conversion between binary, decimal, and hexadecimal.
  • Divisibility: Provides examples of divisibility properties, such as prime factorization, etc.
  • Fundamental Theorem of Arithmetic: Explains the concept of the theorem and how to use prime factorization to solve problems.
  • Fast exponentiation by half: Provides a fast method for exponential power operations.

1.2 Case

Case 1: Application of divisibility properties
Problem description: Determine whether a number is a prime number.
Solution: Use the divisibility property to determine prime numbers.

Case 2: Use prime factorization to solve problems
Problem description: Find the greatest common divisor and least common multiple of two numbers.
Solution: Use prime factorization and the properties of greatest common divisor and least common multiple to calculate.

2.1 prime number

  • Prime Number Sieving: Introduce the sieve of Eratosthenes and the linear sieve method, and demonstrate how to generate a list of prime numbers.
  • Extended Euclidean Algorithm: Explains how to solve linear indefinite equations, and its application in modular arithmetic.
  • Greatest common divisor/least common multiple: Provides methods for calculating the greatest common divisor and least common multiple.
  • Prime number determination: Introducing primality testing algorithms, such as the Miller-Rabin algorithm.

2.2 Case

Case 1: Generate a list of prime numbers
Problem description: List all prime numbers in a certain range.
Solution: Use the prime sieve method to generate a list of prime numbers.

Case2: Application of extended Euclidean algorithm
Problem description: Solve linear indefinite equations.
Solution: Use the extended Euclidean algorithm to solve linear indefinite equations.

3.1 Congruence theory

  • Inverse elements: Explain the concept of inverse elements and how they are calculated in modular arithmetic.

3.2 Case

Case 1: Calculate the inverse element
Problem description: Solve the inverse element in modular operation.
Solution: Use congruence theory to calculate the inverse element.

4.1 Bit operations

  • Bit AND, Bit OR, XOR: Explain the basic principles and applications of bit operations, such as mask operations, state compression, etc.
  • Linear Basis: Give an example of how to use linear basis to solve linear indefinite equation problems.
  • Bitwise negation, left shift, right shift: Provides common use cases of bit operations, such as accelerated calculation of bit operations and representation of binary states.

4.2 Case

Case 1: Application of bit operations in state compression
Problem description: Solve the state space search problem.
Solution: Use bitwise operations for state compression to optimize the search algorithm.

Case 2: Use linear basis to solve linear indefinite equations
Problem description: Solve linear indefinite equations.
Solution: Use the linear basis method to solve linear indefinite equations.

probability theory

1.1 Probability

Explain the basic concepts of probability in detail, including sample space, events, probability distribution, etc.
Give examples to illustrate concepts such as conditional probability, joint probability, and marginal probability.

1.2 Case

Case 1: Calculation of conditional probability
Problem description: Calculate the probability under given conditions.
Solution: Use the conditional probability formula to calculate.

Case 2: Calculation of joint probability
Problem description: Calculate the joint probability of multiple random variables.
Solution: Use the joint probability formula to calculate.

2.1 Expectations

  • Explain the concept of expectation, including expectations of discrete and continuous random variables.
  • Provide expected calculation methods and applications, such as the linear properties of expected value, the relationship between expectation and variance, etc.

2.2 Case

Case 1: Calculation of expectations of discrete random variables
Problem description: Calculate the expected value of discrete random variables.
Solution: Use the expected definition for calculation.

Case 2: The relationship between expectation and variance
Problem description: Calculate the variance of random variables.
Solution: Use the relationship formula between expectation and variance to calculate the variance.

data structure

1. 1 Linear table

  • Array: You can consider how to perform binary search, sorting algorithm, etc. in an array.
  • One-dimensional array: You can consider the sum of elements in the array and the maximum subarray sum problem.
  • Two-dimensional array (matrix): Operations such as matrix transpose and matrix multiplication can be considered.

1.2 Case

Case 1: Binary search of array
Problem description: Given an ordered array, implement a function to find the position of the specified element. Returns the index if found, otherwise returns -1.
Example: Input array [1, 2, 3, 4, 5], find element 3, return index 2.

Case2: Maximum subarray sum problem
Problem description: Given an integer array, find a subarray with the maximum sum (continuous subsequence).
Example: Input array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the maximum sum of subarrays is 6 (subarrays [4, - 1, 2, 1]).

2. 1 string

  • String basics: basic operations on strings, such as splicing, interception, length, etc.
  • Application of string functions: Use built-in functions to solve string problems, such as strtok() and strchr().
  • String comparison: String comparison operation, which can be used for sorting and searching.
  • String matching: Classic string matching algorithms include KMP and Boyer-Moore, which can be used for string searches.
  • Palindrome string: To determine whether a string is a palindrome string, you can use dynamic programming or double pointer method.
  • String splitting: Split a string into multiple substrings, often used to parse input.
  • Character counting: Counting the number of occurrences of characters in a string, which can be implemented using a hash table.
  • String reversal: Reverse the string, which can be implemented using the stack.

2.2 Case

Case 3: String matching - KMP algorithm
Problem description: Implement the KMP string matching algorithm to find the first occurrence in the text The position of the pattern string.
Example: text string "ABABDABACDABABCABAB", pattern string "ABABCABAB", matching position 10.

Case 4: String reversal
Problem description: Given a string, output it in reverse order.
Example: Input the string "hello", reverse it and output "olleh".

3. 1 linked list

  • One-way linked list: implements basic operations of linked lists, such as insertion, deletion, reversal, etc.
  • Doubly linked list: Application of doubly linked list, such as LRU cache algorithm.

3.2 Case

Case 5: Reversing a singly linked list
Problem description: Reversing a singly linked list.
Example: input linked list 1->2->3->4->5, after inversion it is 5->4->3->2-> ;1.

Case 6: LRU caching algorithm
Question description: Implement LRU (least recently used) caching algorithm, support acquisition and insertion operations, and maintain cache The elements in are the most recently visited elements.
Example: Simulate a series of operations (getting, inserting) to verify the correctness of the LRU cache algorithm.

4. 1 Queue

  • FIFO queue: The basic operation of the queue can be implemented with an array or linked list.
  • Circular queue: Solve the problem of space waste in the queue.
  • Monotonic queue: often used in sliding window maximum problems.

4.2 Case

Case 7: Monotone Queue Application - Sliding Window Maximum
Problem Description: Given an integer array and a window size, find all The maximum value in the window.
Example: input array [1,3,-1,-3,5,3,6,7], window size 3, output [3,3,5,5,6,7 ].

5.1 Stack

  • FILO stack: The basic operations of the stack can be implemented using arrays or linked lists.
  • Monotone stack: Solve some specific problems, such as finding the next larger element of each element in an array.

5.2 Case

Case 8: Monotone stack application - next larger element
Problem description: Given an integer array, for each element, find The first element to its right that is larger than it.
Example: Input array [4, 2, 10, 1, 5, 6], output [10, 10, -1, 5, 6, -1].

6. 1 Hash table

  • Integer hashing: Maps integers to indexes in a hash table to resolve collisions.
  • String hashing: Calculates the hash value of a string, used for string comparison and search.
  • Rolling hashing: handles sliding window problems such as substring matching of strings.

6.2 Case

CaseExample 9: String Hash Application - Detecting Repeated Characters
Problem Description: Given A string, checked to see if it has no repeating characters.
Example: Enter the string "abcdefg" and return true; enter the string "abcabc" and return false.

7. 1 tree

  • Binary tree: Implement binary tree traversal, such as pre-order, in-order, and post-order traversal.
  • Binary search tree: implement operations such as insertion, deletion, and search.
  • Heap (priority queue): implements heap sorting, Top K problem, etc.
  • Union-find: Solve set merging and search problems, and be used for connection problems.

7.2 Case

Case 10: Binary search tree application - finding the Kth small element
Problem description: Given a binary search tree, find where The Kth smallest element.
Example: For the binary search tree as follows, when K=3, return 3.

    3
   / \
  1   4
   \
    2

Case 11: Minimum spanning tree - Kruskal algorithm
Problem description: Given an undirected graph with weights, find a minimum spanning tree , so that all nodes are connected together and the total weight is minimum.
Example: For a given graph and weights, use Kruskal's algorithm to find the minimum spanning tree.

8. 1 Figure

  • Directed graph: Solve the path problem of directed graph, such as topological sorting.
  • Undirected graph: implements graph traversal, such as depth-first search and breadth-first search.
  • Minimum spanning tree (Prim, Kruskal): Solve the minimum weight tree connecting all nodes.
  • Maximum complete subgraph: Find the maximum complete subgraph of a graph.

8.2 Case

Case 12: Undirected graph - finding the shortest path
Problem description: Given an undirected graph and two nodes, find two The shortest path between nodes.
Example: For a given undirected graph and nodes, find the shortest path and output the path.

Case 13: Topological sorting - course arrangement
Question description: Given courses and their dependencies, determine whether a course can be arranged Sequence so that all courses can be completed.
Example: Determine whether the courses can be sequenced given the courses and dependencies.

9. 1 Tree Array

Tree Array (BIT): Solve interval query and update problems, such as solving the prefix sum of an array.

9.2 Case

Case 14: Interval query - array prefix sum
Problem description: Implement a tree array that supports interval query and single-point update operations. Used to calculate the prefix sum of an array.
Example: Simulate a series of queries (range sum, single point update) to verify the correctness of a tree array.

10.1 Design of data structure

  • Two queues implement the stack: implement the operation of a stack and use two queues to simulate the behavior of the stack.
  • Two stacks to implement queues: implement the operation of a queue and use two stacks to simulate the queue's operations
  • LRU, LFU: Design and implement LRU cache and LFU cache algorithms.

10.2 Case

Case 15: Implementing a stack with two queues
Problem description: Use two queues to simulate stack operations, including push, pop, top, etc. .
Example: Simulate stack operations and verify their correctness.

Case 16: Two stacks to implement queues
Question description: Use two stacks to simulate queue operations, including enqueue, dequeue, etc.
Example: Simulate queue operations and verify their correctness.

Case 17: LRU caching algorithm
Question description: Design and implement LRU caching algorithm, support acquisition and insertion operations, and maintain elements in the cache The most recently visited element.
Example: Simulate a series of operations (getting, inserting) to verify the correctness of the LRU cache algorithm.

11. 1 Huffman coding

Huffman coding: Use Huffman trees to compress and decompress data.

11.2 Case

Case 18: Data compression - Huffman coding
Problem description: Use Huffman coding to compress and decompress data.
Example: Compress the given data and decompress it to verify correctness.

12. 1 B-tree and B+ tree

B-tree and B+ tree: Understand the structure and application scenarios of B-tree and B+ tree, such as database indexes.

12.2 Case

Case 19: Database index - B-tree application
Problem description: Simulate database index operations, including insertion, deletion, search, etc., using B tree structure.
Example: Simulate a series of database operations and verify the correctness of the B-tree.

13. 1Trie tree

Trie tree: implements prefix matching and word search for autocomplete and dictionaries.

13.2 Case

Case 20: Dictionary and automatic completion - Trie tree application
Problem description: Implement a dictionary and automatic completion function, using the Trie tree structure Store words.
Example: Build a dictionary that supports word insertion, search and auto-complete functions.

14. 1 Interval query and update of line segment tree

Line segment tree: solves interval query and update problems, such as array interval minimum value query.

14.2 Case

Case 21: Interval query - line segment tree application
Problem description: Implement a line segment tree that supports interval query and update operations to solve Array interval minimum value query problem.
Example: Simulate a series of queries (interval minimum, interval update) to verify the correctness of the segment tree.

15.1 AVL trees and red-black trees

AVL trees and red-black trees: Understand self-balancing binary search trees and implement operations such as insertion and deletion.

15.2 Case

Case 22: Self-balancing binary search tree - AVL tree application
Problem description: Implement an AVL tree that supports insertion, deletion and other operations , to maintain the balance of the tree.
Example: Simulate a series of insertion and deletion operations to verify the correctness of the AVL tree.

16.1 String matching algorithm

String Matching Algorithms: Learn different string matching algorithms, such as KMP and Boyer-Moore, for text search and matching problems.

16.2 Case

Case 23: String matching - Boyer-Moore algorithm
Problem description: Implement Boyer-Moore string matching algorithm to find in text The position of the first occurrence of the pattern string.
Example: text string "ABABDABACDABABCABAB", pattern string "ABABCABAB", matching position 10.

Basic algorithms and cases

1. Enumeration

Linear enumeration: Given an array of integers and a target value, find all combinations of two numbers in the array whose sum is equal to the target value.
Multidimensional enumeration: In an n , 1 represents an obstacle.

2. Prefix sum

Prefix sum array: Given an array containing n positive integers and a positive integer k, find all consecutive subarrays in the array such that the sum of the subarrays is equal to k.

3. Double pointers

  • Double pointer method: Find three numbers in an ordered integer array so that their sum is equal to the target value.
  • Double pointer method: merge two ordered linked lists to obtain a new ordered linked list.

4. Ruler method (sliding window)

Ruler method: In an array containing n positive integers, find the shortest continuous subarray such that the sum of the subarrays is greater than or equal to the given value k.

5. Linked list speed pointer

  • Linked list speed pointer: Determine whether a linked list has a ring, and if so, find the entry node of the ring.
  • Linked list speed pointer: Find the k-th node from the bottom of the linked list.

6. Divide and conquer

  • Binary search: Find a specific element in an ordered array.
  • Point divide and conquer: given the coordinates of n points, find the minimum distance between two points.
  • Tree divide and conquer: Find a node in a tree that minimizes the sum of paths from this node to other nodes.

7. Sorting

  • Introduction to comparative sorting: Sort the scores of a group of candidates and require stable sorting.
  • Quick sort: Use the quick sort algorithm to sort a set of student information according to total score.
  • Non-comparative sorting: Use counting sorting to solve a statistical problem, counting the number of times each number appears in a set of integers.

8. 1 Greedy

  • Interval selection problem: Given n time intervals, select as many intervals as possible so that they do not overlap with each other.
  • Greedy backpack: There is a backpack with a capacity C and n items. Each item has a weight and value. Select items to put into the backpack so that the total value of the items in the backpack is maximized.

9. Search

  • Depth First Search (DFS): Find the path from the starting point to the end point in a maze. The maze is composed of 0 and 1, with 0 indicating passability and 1 indicating an obstacle.
  • Breadth First Search (BFS): Find the shortest path from the upper left corner to the lower right corner in a matrix, where the values ​​in the matrix represent the weight of the path.
  • Shortest path algorithm: Solve the shortest path problem between cities, such as map navigation.
  • Topological sorting: Given a set of tasks and their dependencies, find a reasonable order of task execution.

10. Dynamic programming

  • Memoized search: Calculate the nth number in the Fibonacci sequence.
  • Longest monotonic subsequence: Given an array of integers, find the length of the longest monotonically increasing subsequence.
  • Knapsack problem: Solve the 0/1 knapsack problem. Given the weight and value of the items, find the optimal solution.

11. Miscellaneous Algorithms

  • Random algorithm: Implement an algorithm for randomly generating mazes.
  • Pond sampling: Randomly select a certain amount of data from a big data stream, such as randomly selecting a number of people from a pile of candidates.
  • Discretization: Mapping a set of continuous data to a set of discrete values, such as mapping a set of test scores to five grades: A, B, C, D, and E.
  • High precision: Implement a high-precision division algorithm to calculate the quotient of two large integers.
  • Game: Solve the coin flip game and find a winning strategy.
  • Scan lines: Calculate the area and union of multiple rectangles, or find overlapping portions of line segments.
  • State compression: Use state compression to solve all subsets of a set.
  • Brain Teasers: Solve logic puzzle problems, such as deciphering a code.
  • Interaction: Implement an online multiplayer board game to handle the interaction between the operations of multiple players and the referee system.

Conclusion

By systematically studying the above content, you will establish a solid foundation in algorithms and lay a solid foundation for programming competitions such as the Blue Bridge Cup. I hope this blog will be helpful to your learning journey, and I wish you excellent results in the field of programming!

Contact me: hxgsrubxjogxeeag

Guess you like

Origin blog.csdn.net/weixin_46211269/article/details/134701462