查看: 2065| 回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

Remove substrings from a string to get the minimum length

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
原贴:http://www.1point3acres.com/bbs/ ... 6orderby%3Ddateline
题目是这样的:
You are given a string S and a set of n substrings. You are supposed to remove every instance of those n substrings from S so that S is of the minimum length and output this minimum length. Eg:
S- ccdaabcdbb . 1point 3acres 璁哄潧
n=2 - substrings-- (ab, cd). 涓

上一篇:一道Hulu DP 面试问题
下一篇:求解关于hashmap时间复杂度
🔗
 楼主| 草袋豆子 2017-1-26 11:00:25 | 只看该作者
全局:
不知道为什么,问题里没有完全显示我问的内容。
我想说的是,我看了原帖主的解答,但是完全没看懂……能请哪位指教一下思路吗?
回复

使用道具 举报

🔗
waynetang 2017-7-25 05:51:15 | 只看该作者
全局:
public int minLength(String s, Set<String> dict) {
        if(s.length() == 0){
            return 0;
        }
        Queue<String> queue = new LinkedList<>();
        queue.offer(s);
        Set<String> set = new HashSet<>();
        int result = Integer.MAX_VALUE;
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size; i ++){
                String head = queue.poll();
                for(String word : dict){
                    int index = head.indexOf(word);
                    while(index != -1){
                         String newStr = head.substring(0, index) +
                               head.substring(index + word.length(), head.length());
                    if(!set.contains(newStr)){
                        set.add(newStr);
                        queue.offer(newStr);
                        result = Math.min(result, newStr.length());
                    }
                    index = head.indexOf(word, index + 1);
                    }
                }
            }
        }
        return result;
    }
回复

使用道具 举报

🔗
saklyn 2017-7-25 10:05:42 | 只看该作者
全局:
直接dfs + memo。。暴力总能出来的



  1. from sets import Set

  2. def removeSubstringsFromDictDriver(s, dic):
  3.     if not s or not dic: return 0
  4.     m = Set()
  5.     l = [2**32-1]

  6.     def removeSubstringsFromDict(s, dic, memo):
  7.         if not s:
  8.             l[0] = 0
  9.             return
  10.         if s in memo: return
  11.         memo.add(s)
  12.         if all([s.find(w) == -1 for w in dic]):
  13.             l[0] = min(l[0], len(s))

  14.         candidates = [s.replace(w, "", 1) for w in dic]
  15.         for c in candidates:
  16.             removeSubstringsFromDict(c, dic, memo)
  17.         return

  18.     removeSubstringsFromDict(s, dic, m)

  19.     return l[0]

  20. def main():
  21.     s = 'ccdaabcdbb'
  22.     d = Set(['ab', 'cd'])

  23.     n = removeSubstringsFromDictDriver(s, d)

  24.     print n

  25. if __name__ == "__main__":
  26.     main()
复制代码
回复

使用道具 举报

🔗
saklyn 2017-7-25 10:12:38 | 只看该作者
全局:
看原来发的帖也不是dp,至少要利用前面的结果来计算下一个。和我的一样本质上是dfs + memo。‘
这个问题和8皇后是一样的。
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
2011051305 2017-7-28 13:42:44 | 只看该作者
全局:
saklyn 发表于 2017-7-25 10:12
看原来发的帖也不是dp,至少要利用前面的结果来计算下一个。和我的一样本质上是dfs + memo。‘
这个问题和 ...

请问设置这个 l = [2**32-1] 的目的是什么  不能直接以一个变量记录当前的最小值吗?
回复

使用道具 举报

🔗
saklyn 2017-7-29 11:11:28 | 只看该作者
全局:
2011051305 发表于 2017-7-28 13:42
请问设置这个 l = [2**32-1] 的目的是什么  不能直接以一个变量记录当前的最小值吗?

应该是 l = [2**31-1]。这个是python的特点。如果c++/java应该是 l = INT_MAX 这样。
回复

使用道具 举报

🔗
hzlzu4213 2017-8-2 03:09:38 | 只看该作者
全局:
本帖最后由 hzlzu4213 于 2017-8-2 03:28 编辑

lintcode 624  Remove Substrings  应该是这个吧
回复

使用道具 举报

🔗
staycrazy 2017-8-5 12:43:17 | 只看该作者
全局:
本帖最后由 staycrazy 于 2017-8-5 12:45 编辑

时间复杂度上比较好的解法是这样的(写起来会很长)
先用KMP找到所有的匹配 (其实应该用AC自动机)

然后在每一个可能的匹配点上dp

总的来说dp部分和memo是一样的,但是AC自动机比暴力匹配算法还是要快不少。。
回复

使用道具 举报

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

本版积分规则

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