[日常套路] Boyer–Moore majority vote algorithm
来源:互联网 发布:淘宝烽火体育sneaker 编辑:程序博客网 时间:2024/06/11 18:08
The algorithm maintains in its local variables a sequence element and a counter, with the counter initially zero. It then processes the elements of the sequence, one at a time. When processing an element x, if the counter is zero, the algorithm stores x as its remembered sequence element and sets the counter to one. Otherwise, it compares x to the stored element and either increments the counter (if they are equal) or decrements the counter (otherwise). At the end of this process, if the sequence has a majority, it will be the element stored by the algorithm. This can be expressed in pseudocode as the following steps:
- Initialize an element m and a counter i with i = 0
- For each element x of the input sequence:
- If i = 0, then assign m = x and i = 1
- else if m = x, then assign i = i + 1
- else assign i = i − 1
- Return m
Even when the input sequence has no majority, the algorithm will report one of the sequence elements as its result. However, it is possible to perform a second pass over the same input sequence in order to count the number of times the reported element occurs and determine whether it is actually a majority. This second pass is needed, as it is not possible for a sublinear-space algorithm to determine whether there exists a majority element in a single pass through the input.[3]
- [日常套路] Boyer–Moore majority vote algorithm
- Boyer–Moore majority vote algorithm
- 摩尔投票算法(Boyer–Moore majority vote algorithm)
- The Boyer-Moore Majority Vote Algorithm
- Boyer-Moore Majority Vote Algorithm的更一般性问题
- Boyer–Moore majority vote algorithm 博耶-穆尔多数投票算法 leetcode第229题
- LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)
- Moore's majority vote algorithm
- Boyer–Moore–Horspool algorithm
- Boyer–Moore string search algorithm
- Boyer–Moore string search algorithm
- Boyer-Moore algorithm
- Boyer-Moore algorithm ---c++
- Boyer-Moore algorithm
- Boyer Moore Pattern Matching Algorithm
- BM算法(Boyer-Moore algorithm)
- 简单的Boyer Moore Algorithm
- 多数投票算法(Boyer-Moore Algorithm)详解
- python爬虫----初次使用selenium
- BZOJ2956:模积和
- Scala---Akka Actor(一)tutorial-2
- C++虚函数及虚函数表在内存中的分配和访问
- 随笔一
- [日常套路] Boyer–Moore majority vote algorithm
- 1003 最大连续子序列
- CF825E:Minimal Labels(拓扑排序)
- HDU— 5968 异或密码
- android 服务
- HDU-2064-汉诺塔III
- Android Arcgis(13)、Gdal简单写个shp文件
- Jdbc事务笔记
- torch mac openmp