拥有字符串映射如何将其与给定字符串进行比较

发布于 2024-11-06 23:34:22 字数 758 浏览 1 评论 0原文

我们有字符串对的映射,如 name:location (类似于 UNIX 的绝对位置,如 myfolder/)。我们得到了一些位置,例如myfolder/mysubfolder/myfile。如何找到哪个地图位置最适合给定的网址?

例如,我们有一个像这样的地图:

service1:myfolder/
service2:myfolder/mysubfolder/
service3:myfolder/myothersubfolder/
service4:myfolder/mysubfolder/myfile

我们被赋予值myfolder/mysubfolder/myfile/blablabla/(字符串)。 我们想要找出它与地图中的哪个项目最相关。 搜索结果应为 service4 作为具有最相关内容的地图项。

那么如何根据给定的字符串值找到与哪个映射元素最相关的呢?

请提供一些代码,因为我是 C++ nube,不知道如何实现这样的事情?

所以我稍微简化了一个问题 - 现在我需要的所有关系是给定路径的深度,在字符串情况下,只需迭代所有地图路径,查看长度,搜索给定路径中的出现并记住最长的地图项即可获得在给定路径中找到的路径。

We have map of string pairs like name:location (unix like absolute location a la myfolder/). We are given with some location a la myfolder/mysubfolder/myfile. How to find which of maps location fit to given url most?

Example we have a map like:

service1:myfolder/
service2:myfolder/mysubfolder/
service3:myfolder/myothersubfolder/
service4:myfolder/mysubfolder/myfile

We are given value myfolder/mysubfolder/myfile/blablabla/ (string).
We want to find out to which item in our map it relates the most.
Search result shall be service4 as map item with most related content.

So how to find by given string value to which map element it relates the most?

Please provide some code because I am C++ nube and do not get how to inplement such thing?

So I simplified a problem a bit - now all relation I need is in how deep given path is which in string case can be aceived by just iteratin over all maps paths looking at thare langth , searching for appearence in given path and remembering most long map item path found in given path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽清风入怀 2024-11-13 23:34:22

有两个选项:

  1. 如果您需要运行多个查询:
    1. 构建逆向地图或使用双向地图。
    2. 使用 upper_bound 和查找第一个较大的元素
      • 如果您需要具有最长公共前缀的元素,请检查此元素和上一个(最后一个较小的)元素,然后选择具有较长公共前缀的元素。
      • 如果您需要作为前缀的元素,请向后扫描,直到找到作为前缀的元素。
  2. 如果您只需要一次查询,简单的线性搜索会更快(构建逆映射需要 O(n log(n)),而一次迭代只需 O(n) >),而且更容易实现。只需迭代映射,为每个值计算前缀长度并记住迄今为止的最佳匹配(我想建议使用 std::max_element ,但它通过比较运算符实现最大值,而您需要最大值指标)。

There are two options:

  1. If you need to run many queries:
    1. Build the inverse map or use bidirectional map.
    2. Find first larger element using upper_bound and
      • If you need element with longest common prefix, check this and previous (last smaller) element and choose the one with longer common prefix.
      • If you need element that is a prefix, scan back until you find an element that is a prefix.
  2. If you need just one query, simple linear search will be quicker (building the inverse map takes O(n log(n)), while one iteration takes just O(n)), plus it's easier to implement. Simply iterate over the map, for each value calculate the prefix length and remember the best match so far (I wanted to suggest using std::max_element, but it implements maximum by comparison operator while you need maximum by metrics).
末蓝 2024-11-13 23:34:22

如果您的地图定义如下:

typedef std::map<std::string,std::string> MyMap;
MyMap my_map;

...并且搜索词定义如下:

std::string my_key_to_find = "service4";

...那么您可以获得与此键关联的值,如下所示:

std::string found_val;
MyMap::const_iterator it = my_map.find(my_key_to_find);
if( it != my_map.end() )
  found_val = it->second;
else
  std::cout << "Key not found!\n";

If your map is defined like this:

typedef std::map<std::string,std::string> MyMap;
MyMap my_map;

...and the search term is defined like this:

std::string my_key_to_find = "service4";

...then you can get the value associated with this key like this:

std::string found_val;
MyMap::const_iterator it = my_map.find(my_key_to_find);
if( it != my_map.end() )
  found_val = it->second;
else
  std::cout << "Key not found!\n";
鸠书 2024-11-13 23:34:22

如果我正确理解您的问题,您想按值(字符串)搜索键,其中匹配值是提供的搜索词的子字符串。我认为对于这个普遍问题(即任意字符串及其所有子字符串)没有一个简单的解决方案。

但是,示例中用作值的字符串具有特定的结构(即文件系统路径)。您可以利用这个结构来提出一个干净的解决方案。首先,制作一个双向地图。然后,执行以下查找过程:

  1. 如果路径为空,则失败。
  2. 根据请求路径在map中反向查找
  3. 如果找到则返回关联值。
  4. 将最后一个组件弹出路径。
  5. 环形。

如果列表很短,您可能只想循环(键,值)对列表并选择值最相似的键(即最长的公共子字符串)。

If I understand your question correctly, you want to search for keys by value (string), where the matching values are substrings of the provided search term. I don't think there is an easy solution for this as a general problem (i.e. arbitrary strings and all their substrings).

However, the strings used as values in your example have a specific structure (i.e. file system paths). You can exploit this structure to come up with a clean solution. First, make a bi-directional map. Then, implement the following lookup process:

  1. If path is empty, fail.
  2. Reverse lookup in the map based on the request path
  3. If found, return associated value.
  4. Pop the last component off the path.
  5. Loop.

If the list is short, you might just want to loop over the list of (key,value) pairs and select the key where the value is the most similar (i.e. longest substring in common).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文