首页 > 其他 > 详细

LeetCode Substring with Concatenation of All Words

时间:2014-03-22 19:12:00      阅读:419      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L) {
        vector<int> res;
        unordered_map<string, int> stat;
        unordered_map<string, int> run;
        int len = S.size();
        int num = L.size();
        int per = 0;
 
        if (num == 0 || !(per = L[0].size())) return res;
         
        int part= num * per;
        if (part > len) return res;
         
        int end = len - part;
        unordered_map<string, int>::iterator iter;
        pair<unordered_map<string, int>::iterator, bool> ir;
         
        for (int i=0; i<num; i++) {
            ir = stat.insert(pair<string, int>(L[i], 1));
            if (ir.second == false){
                ir.first->second++;             
            }
        }
         
        int i, j, pos, wc;
        string pre;
        for (i=0; i<=end; i++) {
            pos = i;
            for (j=0; j<num; j++, pos += per) {
                string seg = S.substr(pos, per);
                if (j == 0 || seg != pre) {
                    iter = stat.find(seg);
                    if (iter == stat.end()) break;
                    wc = iter->second;
                    ir = run.insert(pair<string, int>(seg, 1));
                    iter = ir.first;
                    if (ir.second) {
                        pre = seg;
                        continue;
                    }
                }
                iter->second++;
                if (wc < iter->second) break;
            }
            if (j == num) res.push_back(i);
            run.clear();
        }
        return res;
    }
};

暴力解

LeetCode Substring with Concatenation of All Words,布布扣,bubuko.com

LeetCode Substring with Concatenation of All Words

原文:http://www.cnblogs.com/lailailai/p/3617588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!