问题很简单...
已知:
- 一个脏话列表,称为List1。 - 一个需要搜索这些脏话的字符串(或短语)列表,称为List2。
期望输出: 匹配至少一个脏话的短语数量。
样例:
List1: "cat", "dog", "mouse", "Nice animal"
List2: "A cat is good". "a dog is bad", "cat and dog are good", "Nice animal", "Hello", "Hello mouse", "This is bad"
输出: 5个短语包含至少1个脏话。
我的做法:
已知:
- 一个脏话列表,称为List1。 - 一个需要搜索这些脏话的字符串(或短语)列表,称为List2。
期望输出: 匹配至少一个脏话的短语数量。
样例:
List1: "cat", "dog", "mouse", "Nice animal"
List2: "A cat is good". "a dog is bad", "cat and dog are good", "Nice animal", "Hello", "Hello mouse", "This is bad"
输出: 5个短语包含至少1个脏话。
我的做法:
int sum = list1.Sum(s => list2.Count(t => t.Contains(s)));
这需要大约38秒的时间来搜索5600个敏感词短语,并在大约4000个字符串中进行搜索。(四核,4 GB RAM)... 太慢了!
我寻找过可能存在的解决方案或算法... 但没有找到。
即使有人能指引我正确的方向,命名一个算法,显示一段代码片段或者只是简单地指引一下,那也太好了!