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