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

[Leetcode] 请教一题Substring with Concatenation of All Words

全局:

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

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

x
本帖最后由 wendychueng 于 2014-6-23 02:56 编辑

Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).


这个是discuss里面一个人写的算法,看不太懂中间那个if... else if... else... 有没有同学可以解释一下? 谢谢!

//use hash table to store the list of words L
//for each m char in the string, check if it is a valid word

class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L) {
        vector<int> result;
        map<string, int> cntL;
        map<string, int> cn;
        int n = S.length();
        int wordlen = L[0].length();
        int listsize = L.size();
        int count = 0;    // number of words in the list
        //store the list of words in hash table
        for(int i=0; i<listsize; i++)
        {
            //if this key has not been occupied
            if(cn.count(L) == 0)
        
    {
                cn[L] = 1;
                count++;
            }
            else
            {
                cn[L] += 1;
                count++;
            }
        }

        string tr, du;
        int r = 0;
        int st = 0;

        for(int j=0; j<wordlen; j++)   //for each char in a word
        { r = 0; st = j;
          for(int i=j; i<n; i += wordlen)  //check the string for every word length substring
          {
              tr = S.substr(i, wordlen);  //copy the next m char in the string
              if(cn.count(tr) == 0 || cn[tr] == 0)  //?? if this is not a valid word
              {
                  cntL.clear();
                  r = 0;
                  st = i+wordlen;
              }
              else if(cntL[tr] < cn[tr]) // ???
              {
                  cntL[tr] += 1;
                  r++;
              }
              else
              {
                  du = S.substr(st, wordlen);
                  while(du != tr)
                  {
                      cntL[du]--;
                      r--;
                      st += wordlen;
                      du = S.substr(st, wordlen);
                  }
                  st += wordlen;
              }
              if(r == count)   //if r == count meaning all the words have appeared
              {
                  result.push_back(st);
                  du = S.substr(st, wordlen);
                  cntL[du]--;
                  r--;
                  st += wordlen;
              }
          }
          cntL.clear();
        }
        sort(result.begin(), result.end());
        return result;
    }
};


上一篇:【第三轮】6.23-6.29 CareerCup 2.6
下一篇:如何计算空间复杂度?
🔗
 楼主| wendychueng 2014-6-24 02:27:53 | 只看该作者
全局:
做过这题的同学来说一下思路也行啊
回复

使用道具 举报

🔗
 楼主| wendychueng 2014-6-27 04:15:33 | 只看该作者
全局:
做过这题的同学来说一下思路吧
回复

使用道具 举报

全局:
这题基本就是暴力解吧
用L里面的词建一个字典 c++就是map 记录每个出现的词以及出现几次 然后遍历字符串所有可能的起点 每个起点扫一遍 一旦发现不在字典中的词或者出现频率超过字典里的值 就退出进行下一个循环即可
回复

使用道具 举报

🔗
readman 2014-6-28 01:12:04 | 只看该作者
全局:
本帖最后由 readman 于 2014-6-28 01:13 编辑

这题我做的方法和以上不太一样.
我把L排序, 然后在一个loop中用L的长度扫S. 每次用boyer moore看L里的字串是不是substring, 不过复杂度好高..O(outer loop) * O(n+m) bm
回复

使用道具 举报

🔗
xiaobenben 2015-1-21 13:48:51 | 只看该作者
全局:
不太明白题意,有人能解释下不?
回复

使用道具 举报

🔗
Sayings 2016-7-20 11:35:35 | 只看该作者
全局:
wizard19900509 发表于 2014-6-28 00:17
这题基本就是暴力解吧
用L里面的词建一个字典 c++就是map 记录每个出现的词以及出现几次 然后遍历字符串所 ...

暴力解现在超时了
回复

使用道具 举报

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

本版积分规则

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