PREFACE
也许是OI生涯最后一场正式比赛了,说是省选前模板,其实都是非常基础的东西,穿插了英文介绍和部分代码实现
祝各位参加JXOI2019的都加油吧
也希望今年JX能翻身,在国赛中夺金
Some Words:
property n. [C] (数学)性质
数学知识
字符串
KMP算法Knuth-Morris-Pratt Algorithm
KMP算法,又称模式匹配算法,是用来在一个文本串(text string)s
中找到所有模式串(pattern)w
出现的位置.
它是通过当失配(mismatch)发生时,模式串本身能提供足够的信息来决定下一个匹配能从哪里开始,这样就可以避开对前面已经匹配好的字符的再次遍历检查(即暴力做法)
“In computer science, the Knuth–Morris–Pratt string-searching algorithm (or KMP algorithm) searches for occurrences of a “word” W
within a main “text string” S
by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.”
来源: 维基百科
我们需要构造一个函数next
,next[n]
表示的是模式串w
中以第n个字符结尾的非前缀子串(substring)与w
的前缀(prefix)能够匹配的最大长度
具体实现过程与思路见OI WIKI
简单说一下比较难懂的在w[i]!=w[j+1]
,为什么指针j
跳向next[j]
显然w[1~j]
是等于i-1
前j
个字母的字串的,但是我们比较发现w[j+1]!=w[i]
我们想要的新的j
是使得除了第$j+1$个字符需要与w[i]
比较外,w[1]~w[j]
的前缀等于i-1
(包括i-1
)前j
个字母的后缀(suffix)
又由于i-1
前任意k(k<=next[j])
个字符后缀等于next[j]
前任意k
个字母的后缀
又因为w[1~next[next[j]]]
等于next[j]
前next[next[j]]
长度的后缀,即等于i-1
前next[next[j]]
长度的后缀
所以新的j=next[j]
就满足上述条件
1 |
|
哈希Hash Function
不想中文介绍了QAQ,OI中常用于数据的离散化(Discretization)
关于更多离散化内容
但是哈希函数有可能发生哈希冲突(Hash Collision),为了避免这种情况,我们可以采取双哈希或是拉链
“A hash function is any function) that can be used to map data) of arbitrary size onto data of a fixed size. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes. Hash functions are often used in combination with a hash table, a common data structure used in computer software for rapid data lookup. Hash functions accelerate table or database lookup by detecting duplicated records in a large file. One such application is finding similar stretches in DNA sequences. They are also useful in cryptography.”
来源:维基百科
字符串哈希我一般用Rabin-Karp Hash,更多相关内容在链接里
1 |
|
对于两个串,我们可以将它们按上述方法映射(mapping)到哈希表后通过二分(binary serach)或是倍增(QAQ这个英语怎么说啊)来快速匹配
主要根据这个可以$O(1)$地判断两个字串是否相等
$w_{str_{i,j}}$
$=($ $a_i$ $p^{j-i}$+$a_{i+1}$ $p^{j-i-1}$+…+$a_{j}$ $p^0$)
$=$ $w_{pre_{j}}$ $-$ $w_{pre_{i-1}}$ $p^{j-i+1}$
数据结构Data Structure
栈Stack
栈是只有一端能进出元素的线性数据结构,所以它是后进后出(LIFO,Last In First Out)的
有入栈(push)与出栈(pop)两种基本操作
“In computer science, a stack is an abstract data type that serves as a collection) of elements, with two principal operations:
- push, which adds an element to the collection, and
- pop, which removes the most recently added element that was not yet removed.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out). Additionally, a peek) operation may give access to the top without modifying the stack.[1]#cite_note-1)The name “stack” for this type of structure comes from the analogy to a set of physical items stacked on top of each other, which makes it easy to take an item off the top of the stack, while getting to an item deeper in the stack may require taking off multiple other items first.[2]#cite_note-clrs-2)”
来源:维基百科)
队列Queue
队列是一种”先进先出(FIFO,First In Fitst Out)”的线性数据结构.一般元素从右端入队,从左端出队.为了节省空间实现上可以采用循环队列
链表Linked List
(这几个都感觉没什么好说的)
讲一下邻接表(Adjacency List)吧,它可以看成是”带有索引数组的多个数据链表”,这种所引就是链表的表头,表头又构成了一个表头(headers)数组
存图常用邻接表或是邻接矩阵(Adjacency Matrix)
邻接表
1 |
|
字典树Trie
字典树是一种用于实现字符串快速检索的多叉树结构,其节点都拥有字符指针.
“Trie is an efficient information reTrieval data structure.Using trie,search complexities can be brought too optimal limit(O(M),M is key length).However the penalty is on Trie storage requirements.”
1 |
|
二叉堆Binary Heap
二叉堆是一颗满足堆性质的完全二叉树.根据排序方式可以分为大根堆(Max-Heap)和小根堆(Min-Heap)
对于大根堆,任意一个非根节点的权值都小于等于其父节点的权值;小根堆则反之
“A binary heap is a complete binary tree which satisfies the heap ordering property.The ordering can be one of two types:the min-heap property and the max-heap property.The value of each node of the max-heap is less than or equal to the value of its parent,with the maximum-value element at the root”
来源:cs.cmu.edu
1 |
|
线段树Segment Tree
咕
图论Graph Theory
Dijkstra算法Dijkstra Algorithm
DIjkstra算法是为了解决单源最短路径问题(Single Source Shortest Path,SSSP)的,简单来说,就是求原点到其它所有点(vertex pl.vertices)的最短路(shortest path)
它是基于一个贪心思想,要求所有边都是非负的.大致就是不断拓展一颗最短路生成树
“Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree(MST).Like Prim’s MST,we generate a SPT(shortest path tree) with given source as root.We maintain two sets,one set contain vertices included in the shortest path tre,other set includes vertices not yet included in shortest path tree.At every step,we find a vertex which is in the other set and has a mimnum distance from the source.”
1 |
|
Bellman-Ford算法和SPFA算法Bellman-Ford Algorithm And SPFA
忽然发现ddl干不完了,因此介绍咕了
1 |
|
Floyd算法Floyd Warshall Algorithm
Floyd算法是基于动态规划思想来解决任意两点之间最短路径(All Pairs Shortest Path problem,APSP)的算法
亦可用于可传递的关系(传递闭包),这个也先挖坑吧
1 |
|
Kruskal算法Kruskal Algorithm
Kruskal算法是求出一张给定联通边带权无向图的最小生成树算法.那么什么是最小生成树?
“Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree.”
来源:Geeksforgeeks 强烈安利这个英文网站,写的浅显易懂而且最重要的是,it is not blocked
Kruskal算法维护无向图的最小生成森林,每次在连接两个森林的边中找一条权值最小的将两个森林连起来.对于连通性我们使用并查集维护
1 |
|
树链剖分Heavy Light Decomposition
树链剖分是用来解决一类树上问题的,可以将树上的操作转化为序列上的操作,同时使用数据结构维护可以使其复杂度更优,根据链剖方式可分为重链剖分和长链剖分等,这里介绍重链剖分
由于重链数量级是$log N$的,线段树时间复杂度是$log N$,总时间复杂度是$log^2 N$
还可以同时求LCA,但是操作时有许多细节需要注意,尤其是dfn[]
和rnk[]
找它的英文找了好久终于找到了,就是Heavy Light Decomposition
“Suppose we have an unbalanced tree (not necessarily a Binary Tree) of n nodes, and we have to perform operations on the tree to answer a number of queries, each can be of one of the two types:
- change(a, b): Update weight of the ath edge to b.
- maxEdge(a, b): Print the maximum edge weight on the path from node a to node b. For example maxEdge(5, 10) should print 25.
“
但是感觉geeksforgeeks上的介绍写得并不是很好…就不贴了.写一点自己口胡的工地英文
To perform two operations in a more efficient time complexity,we can first find out the heavy son of all nodes.We call a node is heavy only when its size(the number of nodes in its subtree) is larger than any of its siblings.We can make it by one DFS. Then we connects all the heavy-son node and we get several heavy-son chains and light-son chains.And if we want to perform two operations on the path of two nodes,we can perform operations on the segment tree built from the heavy-son chains along the path until we reach LCA.
Since it takes O($logN$) to perform operations on segment tree,apparently the complexity depends on the number of light-son chains.We can prove that it is no more than $log N$ along the path from one node to the tree root.,because when we meet one light-son chain,the number of nodes in his father’s subtree is reduced by at least half.
1 |
|
动态规划Dynamic Programming
估计要咕了
问题具有以下两个性质时,可以用动态规划
1) Overlapping Subproblems重叠子问题
2) Optimal Substructure最优子结构