前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1268. 搜索推荐系统(Trie树/multiset)

LeetCode 1268. 搜索推荐系统(Trie树/multiset)

作者头像
Michael阿明
发布2020-07-13 15:21:00
5490
发布2020-07-13 15:21:00
举报
文章被收录于专栏:Michael阿明学习之路

1. 题目

给你一个产品数组 products 和一个字符串 searchWord ,products 数组中每个产品都是一个字符串。

请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。 如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。

请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。

代码语言:javascript
复制
示例 1:
输入:products = ["mobile","mouse","moneypot","monitor","mousepad"], 
searchWord = "mouse"
输出:[
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
解释:按字典序排序后的产品列表是 ["mobile","moneypot","monitor","mouse","mousepad"]
输入 m 和 mo,由于所有产品的前缀都相同,所以系统返回字典序最小的三个产品
 ["mobile","moneypot","monitor"]
输入 mou, mous 和 mouse 后系统都返回 ["mouse","mousepad"]

示例 2:
输入:products = ["havana"], searchWord = "havana"
输出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]

示例 3:
输入:products = ["bags","baggage","banner","box","cloths"],
searchWord = "bags"
输出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]

示例 4:
输入:products = ["havana"], searchWord = "tatiana"
输出:[[],[],[],[],[],[],[]]
 
提示:
1 <= products.length <= 1000
1 <= Σ products[i].length <= 2 * 10^4
products[i] 中所有的字符都是小写英文字母。
1 <= searchWord.length <= 1000
searchWord 中所有字符都是小写英文字母。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/search-suggestions-system 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

2.1 Trie树

参考:Trie树

代码语言:javascript
复制
class trie
{
public:
	trie* next[26] = {NULL};
	bool isend =  false;
	int count = 0;
	void insert(string& s)
	{
		trie* cur = this;
		for(int i = 0; i < s.size(); i++)
		{
			if(cur->next[s[i]-'a'] == NULL)
				cur->next[s[i]-'a'] = new trie();
			cur = cur->next[s[i]-'a'];
		}
		cur->count++;
		cur->isend = true;
	}
};
class Solution {
public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
    	trie* t = new trie();
    	for(string& p : products)
    		t->insert(p);//单词插入trie树
    	vector<vector<string>> ans(searchWord.size());
    	int i = 0, idx = 0, j = 0, n = searchWord.size();
    	string prefix = "";
    	trie* cur = t;
    	for(i = 0; i < n; ++i)
    	{
    		prefix += searchWord[i];
    		for( ; j < prefix.size(); ++j)
    		{
    			if(cur->next[prefix[j]-'a'] == NULL)
    			    return ans;
    			cur = cur->next[prefix[j]-'a'];
    		}//找到前缀
    		dfs(cur,prefix,ans[i]);
    	}
    	return ans;
    }

    void dfs(trie* cur, string& str, vector<string>& list)
    {	//遍历前缀以下的节点,找到单词
    	if(list.size() == 3)
    		return;
    	if(!cur) return;
    	if(cur->isend)
    	{
    		int n = cur->count;
    		while(list.size() < 3 && n--)
    			list.push_back(str);
    	}
    	for(int i = 0; i < 26; ++i)
    	{
    		str += i+'a';
    		dfs(cur->next[i],str,list);
    		str.pop_back();
    	}
    }
};

1140 ms 68.4 MB

2.2 multiset

代码语言:javascript
复制
class Solution {
public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
        multiset<string> s;
        for(string& p : products)
    		s.insert(p);//插入multiset
        vector<vector<string>> ans(searchWord.size());
        string prefix = "";
        for(int i = 0; i < searchWord.size(); ++i)
    	{
    		prefix += searchWord[i];//输入单词前缀
            auto start = s.lower_bound(prefix);//二分查找下界
    		for(auto it = start; it != s.end() && ans[i].size() < 3; ++it)
    		{	//把后序3个包含前缀的单词加入答案
    			if((*it).find(prefix) == 0)
                    ans[i].push_back(*it);
                else
                    break;
    		}
    	}
    	return ans;
    }
};

80 ms 24.4 MB

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/06/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解题
    • 2.1 Trie树
      • 2.2 multiset
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档