-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp030.cpp
64 lines (64 loc) · 1.92 KB
/
p030.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> result;
if (words.empty() || !words[0].length()) return result;
int size = words.size();
int len = words[0].length();
int slen = s.length()-len;
unordered_map<string, int*> dict;
int i, j, backlen = (size-1)*len, k = 0;
int *tmp;
int *zeronum = new int[len];
int dictsize;
for (i = 0; i < len; i++)
{
zeronum[i] = 0;
}
for (i = 0; i < size; i++)
{
if (dict.find(words[i]) == dict.end())
{
dict.insert(pair<string, int*>(words[i], new int[len]));
tmp = dict[words[i]];
for (j = 0; j < len; j++)
tmp[j] = 1;
}
else
{
tmp = dict[words[i]];
for (j = 0; j < len; j++)
tmp[j]++;
}
}
dictsize = dict.size();
for (i = 0; i <= slen; i++)
{
string sub2 = s.substr(i, len);
if (dict.find(sub2) != dict.end())
{
if (dict[sub2][k]-- == 0) zeronum[k]--;
if (dict[sub2][k] == 0) zeronum[k]++;
if (zeronum[k] == dictsize)
{
result.push_back(i-backlen);
}
}
if (i-backlen >= 0)
{
string sub1 = s.substr(i-backlen, len);
if (dict.find(sub1) != dict.end())
{
if (dict[sub1][k]++ == 0) zeronum[k]--;
if (dict[sub1][k] == 0) zeronum[k]++;
}
}
k++;
if (k == len)
{
k = 0;
}
}
return result;
}
};