我有很多字符串(城市名称),即使用户打错字,我也想找到城市的名称。
例
用户键入“ chcago”,系统将找到“ Chicago”
当然,我可以为列表中的所有字符串计算查询的Levenshtein距离,但这将非常慢。
有什么有效的方法可以执行这种字符串匹配吗?
我确实有来自wikimedia commons的文件名,我想直接访问缩略图.
示例: Tour_Eiffel_Wikimedia_Commons.jpg
我找到了一种方法来获取包含我想要的缩略图的网址的json数据:
https://en.wikipedia.org/w/api.php?action=query&titles=Image:Tour_Eiffel_Wikimedia_Commons.jpg&prop=imageinfo&iiprop=url&iiurlwidth=200
Run Code Online (Sandbox Code Playgroud)
但我不想要另一个请求.有没有办法直接访问缩略图?
我有一个HashSet<int>和一个List<int>(Hashset 大约有 300 万个项目,List 大约有 300k 个项目)。
我目前使用它们相交
var intersected = hashset.Intersect(list).ToArray();
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更快的方法来做到这一点。也许并行?
我想写一个过滤多个标准数据的方法.这些标准应作为函数传递给filter-function,例如:
var products = [/* some data */];
function filterMyProducts(criteria) {
return products.filter(/* I'm asking for this code */);
}
function cheap(product) {
return product.price < 100;
}
function red(product) {
return product.color == "red";
}
// find products that are cheap and red
var result = filterMyProducts([cheap, red]);
Run Code Online (Sandbox Code Playgroud)
如何将数组中传递的条件与过滤器函数结合起来?我希望它们与布尔AND结合使用.
我有一个包含整数的巨大文件(约 20 GB),想用 C# 读取它们。
将文件读取到内存(字节数组)非常快(使用 SSD,整个文件适合内存)。但是,当我使用二进制读取器(通过内存流)读取这些字节时,ReadInt32 方法比将文件读取到内存所需的时间要长得多。我预计磁盘 IO 是瓶颈,但事实是转换!
有没有一种方法可以直接将整个字节数组转换为 int 数组,而不必使用 ReadInt32 方法将其一一转换?
class Program
{
static int size = 256 * 1024 * 1024;
static string filename = @"E:\testfile";
static void Main(string[] args)
{
Write(filename, size);
int[] result = Read(filename, size);
Console.WriteLine(result.Length);
}
static void Write(string filename, int size)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
BinaryWriter bw = new BinaryWriter(new FileStream(filename, FileMode.Create), Encoding.UTF8);
for (int i = 0; i < size; i++)
{ …Run Code Online (Sandbox Code Playgroud) 从一组不重叠(但接触)的多边形计算对偶图。
多边形A、B和C,它们部分共享的坐标 1-22(黄色)和对偶图(蓝色)。
我有一组S多边形。每个多边形P i都表示为坐标的有序列表。多边形P i的边a — b表示为P i,(a,b)
多边形代表对偶图的面和节点。为了识别多边形P i的相邻面,只需将P i的每条边与每个其他多边形P j的每条边进行比较。如果边由另一个多边形共享,则P i和P j相邻。
这将创建大量的多条边,稍后可以将其删除。
该算法效率不高,因为它的运行时间复杂度为O(E 2 ),其中E表示多边形集合S的边数。
第一步创建边缘索引。这会将运行时间减少到O(2×E) = O(E)
删除度数为 2 的每个节点。(我认为这不会影响对偶图?)
algorithm geometry graph computational-geometry planar-graph
我有大约20,000,000 pair<int, int>,我需要与ints联系.我这样做了unordered_map<pair<int, int>, int>.分析我的算法表明检查条目是否存在
bool exists = myMap[make_pair(a, b)] != NULL
Run Code Online (Sandbox Code Playgroud)
是性能瓶颈.我认为从a中检索这些信息unordered_map会非常快,因为它是O(1).但如果常数很大,则恒定时间可能会很慢......
我的哈希函数是
template <>
struct tr1::hash<pair<int, int> > {
public:
size_t operator()(pair<int, int> x) const throw() {
size_t h = x.first * 1 + x.second * 100000;
return h;
}
};
Run Code Online (Sandbox Code Playgroud)
你知道我的问题有更好的数据结构吗?
显然,我不能只将信息存储在矩阵中,因此内存量不适合现有的任何计算机.我所知道的所有分布都是myMap[make_pair(a, a)]不存在的a.并且所有ints都在从0到大约20,000,000的连续范围内.
可以把它想象成20,000,000x20,000,000的稀疏矩阵,大约有20,000,000个条目但从不在主对角线上.
将一vector<pair<int, int>>*(阵列Ñ预期的条目)要快?查找a将是微不足道的(只是数组的索引),然后我将迭代向量,比较对的first值b.
我上传了原始数据,因此您可以看到结构.
我有以下 Tarjan 算法的(递归)实现来查找图中的强连通分量,并且它工作正常:
public class StronglyConnectedComponents
{
public static List<List<int>> Search(Graph graph)
{
StronglyConnectedComponents scc = new StronglyConnectedComponents();
return scc.Tarjan(graph);
}
private int preCount;
private int[] low;
private bool[] visited;
private Graph graph;
private List<List<int>> stronglyConnectedComponents = new List<List<int>>();
private Stack<int> stack = new Stack<int>();
public List<List<int>> Tarjan(Graph graph)
{
this.graph = graph;
low = new int[graph.VertexCount];
visited = new bool[graph.VertexCount];
for (int v = 0; v < graph.VertexCount; v++) if (!visited[v]) DFS(v);
return stronglyConnectedComponents;
}
public void DFS(int v) …Run Code Online (Sandbox Code Playgroud) algorithm ×5
performance ×4
c# ×3
binary ×1
boolean ×1
c++ ×1
casting ×1
concat ×1
filter ×1
geometry ×1
graph ×1
hashset ×1
intersection ×1
javascript ×1
map ×1
match ×1
planar-graph ×1
string ×1
thumbnails ×1