C# 使用键查找值,反之亦然
首先,为这个讨厌的标题道歉。我稍后会更正它 我有一些数据如下:C# 使用键查找值,反之亦然,c#,algorithm,data-structures,C#,Algorithm,Data Structures,首先,为这个讨厌的标题道歉。我稍后会更正它 我有一些数据如下: “林荫大道”、“林荫大道”、“林荫大道”、“林荫大道” 我需要一个O(1)的数据结构,用于通过其他方式查找任何单词。例如,如果我使用字典,我需要像这样存储这些键/值,这对我来说很奇怪 abbr.Add("BLVD", new List<string> { "BOULEVARD","BOUL","BOULV", "BLVD" }); abbr.Add("BOUL", new List<string> { "BO
“林荫大道”、“林荫大道”、“林荫大道”、“林荫大道”
我需要一个O(1)的数据结构,用于通过其他方式查找任何单词。例如,如果我使用字典,我需要像这样存储这些键/值,这对我来说很奇怪
abbr.Add("BLVD", new List<string> { "BOULEVARD","BOUL","BOULV", "BLVD" });
abbr.Add("BOUL", new List<string> { "BOULEVARD", "BOUL", "BOULV", "BLVD" });
abbr.Add("BOULV", new List<string> { "BOULEVARD", "BOUL", "BOULV", "BLVD" });
abbr.Add("BOULEVARD", new List<string> { "BOULEVARD", "BOUL", "BOULV", "BLVD" });
abbr.Add(“BLVD”,新列表{“BOULEVARD”、“BOUL”、“BOULV”、“BLVD”);
缩写添加(“BOUL”,新列表{“BOULEVARD”、“BOUL”、“BOULV”、“BLVD”);
缩写添加(“BOULV”,新列表{“BOULEVARD”、“BOUL”、“BOULV”、“BLVD”);
缩写添加(“林荫大道”,新列表{“林荫大道”、“林荫大道”、“林荫大道”、“林荫大道”);
使用哪种数据结构来保持此数据与我的查询条件相匹配
提前感谢创建两个
HashMap
-一个将单词映射到组号。另一个将组号映射到单词列表。这样可以节省一些内存
Map<String, Integer> - Word to Group Number
Map<Integer, List<String>> - Group Number to a list of words
将字映射到组号
将组编号映射到单词列表
您需要两次
O(1)
查找-首先获取组号,然后通过组号获取单词列表。假设abbr是一本字典,您可以使用以下功能:
public static void IndexAbbreviations(IEnumerable<String> abbreviations) {
for (var a in abbreviations)
abbr.Add(a, abbreviations);
}
公共静态无效索引修订(IEnumerable缩写){
for(缩写为var a)
缩写添加(a,缩写);
}
这将使用提供的缩写列表填充字典,以便在字典中查找任何缩写时。它比您提供的示例代码稍好一些,因为我没有为每个值创建新对象
从“使用其键检索值非常快,接近O(1),因为Dictionary(Of TKey,TValue)类是作为哈希表实现的。”对Dictionary的选择对我来说很好。如上所述,您应该使用字典中引用的相同列表。代码可以是这样的:
var allAbrList = new List<List<string>>
{
new List<string> {"BOULEVARD", "BOUL", "BOULV", "BLVD"},
new List<string> {"STREET", "ST", "STR"},
// ...
};
var allAbrLookup = new Dictionary<string, List<string>>();
foreach (List<string> list in allAbrList)
{
foreach (string abbr in list)
{
allAbrLookup.Add(abbr, list);
}
}
var abbr = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
abbr.Add("BLVD", "BLVD"); // this line may be optional
abbr.Add("BOUL", "BLVD");
abbr.Add("BOULV", "BLVD");
abbr.Add("BOULEVARD", "BLVD");
var groupedDictionary = new GroupedDictionary<string>();
groupedDictionary.Add("BLVD", new List<string> {"BOUL", "BOULV"}); // after that three keys exist and one list of three items
groupedDictionary.Add("BOULEVARD", new List<string> {"BLVD"}); // now there is a fourth key and the key is added to the existing list instance
var items = groupedDictionary["BOULV"]; // will give you the list with four items
var allAbrList=新列表
{
新列表{“林荫大道”、“林荫大道”、“林荫大道”、“林荫大道”},
新名单{“街”、“街”、“街”},
// ...
};
var allAbrLookup=新字典();
foreach(allAbrList中的列表)
{
foreach(列表中的字符串缩写)
{
添加(缩写,列表);
}
}
最后一部分可以转换为LINQ以减少代码,但这样更容易理解。我不认为有理由将字典的值部分定义为列表
对象,但这可能是您的要求。这个答案假设你只想知道这个词是否本质上是“Boulevard”的意思
我会选择一个值作为“官方”值,并将所有其他值映射到它,如下所示:
var allAbrList = new List<List<string>>
{
new List<string> {"BOULEVARD", "BOUL", "BOULV", "BLVD"},
new List<string> {"STREET", "ST", "STR"},
// ...
};
var allAbrLookup = new Dictionary<string, List<string>>();
foreach (List<string> list in allAbrList)
{
foreach (string abbr in list)
{
allAbrLookup.Add(abbr, list);
}
}
var abbr = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
abbr.Add("BLVD", "BLVD"); // this line may be optional
abbr.Add("BOUL", "BLVD");
abbr.Add("BOULV", "BLVD");
abbr.Add("BOULEVARD", "BLVD");
var groupedDictionary = new GroupedDictionary<string>();
groupedDictionary.Add("BLVD", new List<string> {"BOUL", "BOULV"}); // after that three keys exist and one list of three items
groupedDictionary.Add("BOULEVARD", new List<string> {"BLVD"}); // now there is a fourth key and the key is added to the existing list instance
var items = groupedDictionary["BOULV"]; // will give you the list with four items
var abbr=新字典(StringComparer.CurrentCultureIgnoreCase);
缩写添加(“大道”、“大道”);//此行可能是可选的
缩写添加(“BOUL”、“BLVD”);
缩写添加(“BOULV”、“BLVD”);
缩写添加(“大道”、“大道”);
或者,您可以为字典的值部分定义枚举,如下所示:
enum AddressLine1Suffix
{
Road,
Street,
Avenue,
Boulevard,
}
var abbr = new Dictionary<string, AddressLine1Suffix>(StringComparer.CurrentCultureIgnoreCase);
abbr.Add("BLVD", AddressLine1Suffix.Boulevard);
abbr.Add("BOUL", AddressLine1Suffix.Boulevard);
abbr.Add("BOULV", AddressLine1Suffix.Boulevard);
abbr.Add("BOULEVARD", AddressLine1Suffix.Boulevard);
enum AddressLine1Suffix
{
路,,
街道,
大街
林荫大道,
}
var abbr=新字典(StringComparer.CurrentCultureInoRecase);
缩写添加(“BLVD”,地址为Suffix.Boulevard);
缩写添加(“BOUL”,地址为1 Suffix.Boulevard);
缩写添加(“BOULV”,地址为1 Suffix.Boulevard);
缩写为Add(“BOULEVARD”,地址为1 Suffix.BOULEVARD);
如果不为每个键创建一个新列表,那么只要数据量不是很大,字典将是快速且合理的内存效率。您还可以从重用字符串本身中获得一些额外的好处,尽管优化器可能会为您解决这一问题
var abbr = new Dictionary<string, List<string>>;
var values = new List<string> { "BOULEVARD","BOUL","BOULV", "BLVD" };
foreach(var aValue in values) abbr.add(value, values);
var abbr=新字典;
var值=新列表{“BOULEVARD”、“BOUL”、“BOULV”、“BLVD”};
foreach(var aValue in values)缩写为add(value,value);
正如Petar Minchev所说,您可以将列表拆分为组列表和指向该组的键列表。为了简化这一点(在使用中),您可以编写自己的IDictionary
实现,并使用Add
方法构建这些组。我试了一下,它似乎奏效了。以下是实施的重要部分:
public class GroupedDictionary<T> : IDictionary<T,IList<T>>
{
private Dictionary<T, int> _keys;
private Dictionary<int, IList<T>> _valueGroups;
public GroupedDictionary()
{
_keys = new Dictionary<T, int>();
_valueGroups = new Dictionary<int, IList<T>>();
}
public void Add(KeyValuePair<T, IList<T>> item)
{
Add(item.Key, item.Value);
}
public void Add(T key, IList<T> value)
{
// look if some of the values already exist
int existingGroupKey = -1;
foreach (T v in value)
{
if (_keys.Keys.Contains(v))
{
existingGroupKey = _keys[v];
break;
}
}
if (existingGroupKey == -1)
{
// new group
int newGroupKey = _valueGroups.Count;
_valueGroups.Add(newGroupKey, new List<T>(value));
_valueGroups[newGroupKey].Add(key);
foreach (T v in value)
{
_keys.Add(v, newGroupKey);
}
_keys.Add(key, newGroupKey);
}
else
{
// existing group
_valueGroups[existingGroupKey].Add(key);
// add items that are new
foreach (T v in value)
{
if(!_valueGroups[existingGroupKey].Contains(v))
{
_valueGroups[existingGroupKey].Add(v);
}
}
// add new keys
_keys.Add(key, existingGroupKey);
foreach (T v in value)
{
if (!_keys.Keys.Contains(v))
{
_keys.Add(v, existingGroupKey);
}
}
}
}
public IList<T> this[T key]
{
get { return _valueGroups[_keys[key]]; }
set { throw new NotImplementedException(); }
}
}
公共类GroupedDictionary:IDictionary
{
私用字典(keys);;
私人词典(valuegroup),;
公共分组词典()
{
_keys=新字典();
_valueGroups=新字典();
}
公共作废添加(KeyValuePair项)
{
添加(item.Key、item.Value);
}
公共无效添加(T键,IList值)
{
//看看是否有些值已经存在
int existingGroupKey=-1;
foreach(价值的tV)
{
if(_keys.keys.Contains(v))
{
existingGroupKey=_keys[v];
打破
}
}
如果(existingGroupKey==-1)
{
//新组
int newGroupKey=\u valueGroups.Count;
_添加(newGroupKey,newlist(value));
_valueGroups[newGroupKey]。添加(键);
foreach(价值的tV)
{
_添加(v,newGroupKey);
}
_key.Add(key,newGroupKey);
}
其他的
{
//现有组
_valueGroups[existingGroupKey]。添加(键);
//添加新项目
foreach(价值的tV)
{
如果(!\u valueGroups[existingGroupKey]。包含(v))
{
_valueGroups[existingGroupKey]。添加(v);
}
}
//添加新键
_key.Add(key,existingGroupKey);
foreach(价值的tV)
{