#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
string temp;
vector<string> encrypt, decrypt;
int i,n, co=0;
cin >> n;
for(i=0;i<n;i++)
{
cin >> temp;
encrypt.push_back(temp);
}
for(i=0;i<n;i++)
{
cin >> temp;
decrypt.push_back(temp);
}
for(i=0;i<n;i++)
{
temp = encrypt[i];
if((binary_search(decrypt.begin(), decrypt.end(), temp)) == true) ++co;
}
cout << co << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它读取两个相等的字符串列表,并且应该打印出第一个列表中有多少单词也可以在第二个列表中找到,简单.不给我经过考验的结果,我认为问题出在binary_search中.你能告诉我为什么吗 ?
我是Python的新手,希望在向前推进之前能得到一些建议.我有一组整数,我想检查一个给定的元素是否尽可能快地包含在该组中(速度在这里很重要).
使用Python,我应该看看为这些操作(BST等)定制的自定义数据结构,python技巧,如使用any()包装,还是有任何众所周知的Python/C库是这类事情的标准.我不想在这里重新发明轮子,所以我很想知道在Python中使用它的常用方法.
更多背景,元素都预先插入到组中,之后没有发生,因此插入时间无关紧要.这似乎意味着维护一个已排序的组并执行类似二进制搜索的操作将是最好的方法,但我确信这已经实现得比我实现的更有效,并且可以在Python/C lib中使用.有兴趣听听你们的想法.
谢谢!
请看这段代码:
def chop(array, search):
lo = 0
high = len(array) - 1
while lo <= high:
mid = (high + lo) /2
if array[mid] == search:
return 'true'
elif search > array[mid]:
low = mid + 1
else:
high = mid - 1
return 'false'
if __name__ == '__main__':
a = [1,2,3,4,5,6,7,8,9,10]
print chop(a, 3)
Run Code Online (Sandbox Code Playgroud)
我写了这个小脚本,它应该在数组中搜索数字 - 常规二进制搜索.所以我运行脚本,例如当我输入时chop(a, 1)我得到了真实,当我输入时chop(a, 2)我得到了真实,但是当我放入时chop(a, 3)我没有得到答案,只是Python Shell中的空行.
有没有人知道发生了什么?
好的伙伴在这里是我的简单代码,我在其中构建一个String数组并尝试搜索此数组中的字符串:
String[] arr = new String[5];
arr[0] = "ccc";
arr[1] = "aaa";
arr[2] = "bbb";
arr[3] = "eee";
arr[4] = "ddd";
System.out.println(Arrays.binarySearch(arr,"eee"));
Run Code Online (Sandbox Code Playgroud)
直接来自Java 6 binarySearch文档:"在进行此调用之前必须对数组进行排序.如果未对其进行排序,则结果未定义"!
实际上我运行我的代码几次得到输出总是3这是我的NOT SORTED数组中的eee的位置,但结果似乎没有"未定义",如文档所述.
我错过了什么?
谢谢
I want to modify the famous binary search algorithm to return the index of the next bigger item instead of the key being searched.
So we have 4 cases:
e.g:
data = { 1, 3, 5, 7, 9, 11 };
Run Code Online (Sandbox Code Playgroud)
我有以下课程:
struct EdgeExtended {
int neighborNodeId;
int weight;
int arrayPointer;
bool isCrossEdge;
};
Run Code Online (Sandbox Code Playgroud)
我想要一个这样的对象的向量,通过neighborNodeId对它进行排序.然后我想搜索特定的neighborNodeId并通过二进制搜索返回对向量中找到的对象的引用.以前我用过地图,所以就是这样的:
map<int, EdgeExtended> neighbours;
.....
auto it = neighbours.find(dnodeId);
if (it != neighbours.end()) {
edgeMap = it->second;
}
Run Code Online (Sandbox Code Playgroud)
代替
map<int, EdgeExtended> neighbours;
Run Code Online (Sandbox Code Playgroud)
我希望有
vector<EdgeExtended> neighbours;
Run Code Online (Sandbox Code Playgroud)
并保留尽可能多的旧代码.
我想测量矢量是否比地图快,因为我正在构建数千个矢量(或地图),每个矢量(地图)相对较小(~10个项目).我不知道如何a)通过neighborNodeId对对象进行排序,以及b)如何使用二进制搜索来搜索类的特定成员(neighborNodeId).抱歉,这个菜鸟问题.我指望你的帮助.
问题:当我的节目明星我创建一个List<myClass> myList并用对象填充它.这些对象是使用文件中的日期创建的.之后,我不想更改该列表,也不想再次访问这些文件.但我需要在很多类/方法中访问该List
我提出的解决方案:List<myClass> myList在静态类中创建私有,并使用我的静态类的构造函数填充它,并仅使用返回myList.AsReadOnly()的属性访问它.这样我就无法改变实际的清单.但是AsReadOnly的回归是一个IList,对吧?我在MSDN上检查IList并且没有BinarySearch方法......这会使整个事情变得非常缓慢.
我该如何解决这个问题?复制IList返回正常列表,以便我可以排序和BinarySearch吗?或者可能更改整个"myList.AsReadOnly()"方法?我愿意接受各种建议和方法^^改变整个代码不是问题.
编辑:
我正在尝试编写一种算法,用于查找最接近的值的索引,该索引小于或等于排序数组中的搜索值.在数组[10,20,30]的示例中,以下搜索值应输出以下索引:
我想使用二进制搜索进行对数运行时.我有一个C-esque伪代码的算法,但它有3个基本情况.这3个基本案例是否可以压缩为1以获得更优雅的解决方案?
int function indexOfClosestLesser(array, searchValue, startIndex, endIndex) {
if (startIndex == endIndex) {
if (searchValue >= array[startIndex]) {
return startIndex;
} else {
return -1;
}
}
// In the simplistic case of searching for 2 in [0, 2], the midIndex
// is always 0 due to int truncation. These checks are to avoid recursing
// infinitely from index 0 to index 1.
if (startIndex == endIndex - 1) {
if (searchValue >= …Run Code Online (Sandbox Code Playgroud) 就时间和空间复杂度而言,二元搜索是否优于三元搜索?