📣 独立日限时特惠: VIP通行证立减$68
楼主: happysshao
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 电面

🔗
kuyen 2014-11-10 14:17:38 | 只看该作者
全局:
贴个c++ Trie

class Trie{
public:
        int count;
        unordered_map<char, Trie*> children;

        Trie(){
                count = 0;
        }

        void insert(string word){
                if (children.find(word[0]) == children.end()){
                        children[word[0]] = new Trie();
                }

                children[word[0]]->count++;
                if (word.size() != 1)
                        children[word[0]]->insert(word.substr(1));
        }

        string find(string word){
                if (children[word[0]]->count == 1) return string(1, word[0]);
                if (word.size() == 1) return string(""); // there're more branches, but the word has ended, return NULL

                string temp = children[word[0]]->find(word.substr(1));
                return temp.empty() ? string("") : word[0]+temp;
        }

        void remove(); // remove function in case of memory leak; not implemented here
};

map<string, string> shortestPre(vector<string> &arr){

        Trie trie;
        map<string, string> res;

        int size = arr.size();
        for (int i=0; i<size; i++) trie.insert(arr[i]);
        for (int i=0; i<size; i++) res[arr[i]] = trie.find(arr[i]);
        return res;
}
回复

使用道具 举报

🔗
hecssy 2014-11-11 04:25:28 | 只看该作者
全局:
不太明白,为什么bear的unique prefix是空而不应该是bear?
[bearcat, bear]
{bearcat: bearc, bear: ""}
回复

使用道具 举报

🔗
hecssy 2014-11-11 04:25:43 | 只看该作者
全局:
不太明白,为什么bear的unique prefix是空而不应该是bear?
[bearcat, bear]
{bearcat: bearc, bear: ""}
回复

使用道具 举报

🔗
dragonmigo 2014-11-20 14:32:05 | 只看该作者
全局:
averillzheng 发表于 2014-11-3 14:12
这种情况下,{ab:“”, abb: "abb", abbb:"abbb"}

我觉得,这种结果不是很合理吧?
{ab:“ab”, abb: "abb", abbb:"abbb"} 是不是更合理些?
这么想的原因是,请考虑这样的case: {"ab", "abb", "abbb", "cd", "cdd", "cddd"}
如果按照前一种解法,那  "ab" 和 "cd" 不是最后都得出是  "" ? 是不是就不Unique了?
回复

使用道具 举报

🔗
averillzheng 2014-11-20 14:52:14 | 只看该作者
全局:
dragonmigo 发表于 2014-11-20 14:32
我觉得,这种结果不是很合理吧?
{ab:“ab”, abb: "abb", abbb:"abbb"} 是不是更合理些?
这么想的原 ...

你说的这种情况下,应该被压缩成如下的形式:
[ab:a, abb:abb, abbb:abbb, cd:c, cdd:cdd, cddd:cddd]
回复

使用道具 举报

🔗
whitesunday 2014-11-24 07:57:19 | 只看该作者
全局:
zhenggao1986 发表于 2014-11-3 13:13
class PrefixTreeNode {
        public int count;
        public PrefixTreeNode[] next;

感谢!非常喜欢你的代码
回复

使用道具 举报

🔗
zhenggao1986 2014-11-24 13:08:30 | 只看该作者
全局:
whitesunday 发表于 2014-11-24 07:57
感谢!非常喜欢你的代码

哈哈,谢谢咯
回复

使用道具 举报

🔗
yuqinlear 2014-12-8 01:31:06 | 只看该作者
全局:
为啥都认为prefix tree难写呢,这么直截了当,brute force还难写呢。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表