having a map of strings how to compare it to given string

孤人 提交于 2019-12-11 03:47:54

问题


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.


回答1:


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).



回答2:


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";



回答3:


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).



来源:https://stackoverflow.com/questions/5979442/having-a-map-of-strings-how-to-compare-it-to-given-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!