-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
102 lines (94 loc) · 2.83 KB
/
solution.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class Solution
{
private:
int count[1000];
int countSize;
map<string, int> index;
vector<int> ret;
public:
vector<int> findSubstring(string S, vector<string> &L)
{
ret.clear();
if (L.size() == 0)
return ret;
index.clear();
countSize = 0;
for(int i = 0; i < L.size(); i++)
if (index.count(L[i]) > 0)
count[index[L[i]]]++;
else
{
index[L[i]] = countSize;
count[countSize++] = 1;
}
int step = L[0].size();
vector<int> a;
for(int i = 0; i < step; i++)
{
a.clear();
for(int j = i; j < S.size(); j += step)
{
if (j + step <= S.size())
{
string s(S, j, step);
if (index.count(s) > 0)
a.push_back(index[s]);
else
a.push_back(-1);
}
}
int beg = -1;
int end = 0;
int size = L.size();
while(end < a.size())
{
if (a[end] != -1)
{
if (count[a[end]] > 0)
{
if (beg == -1)
beg = end;
size--;
count[a[end]]--;
}
else
{
while(beg < end)
{
count[a[beg]]++;
size++;
if (a[beg++] == a[end])
break;
}
count[a[end]]--;
size--;
}
}
else
{
size = L.size();
if (beg != -1)
{
for(int i = beg; i < end; i++)
count[a[i]]++;
}
beg = -1;
}
end++;
if (size == 0)
{
ret.push_back(beg * step + i);
size++;
count[a[beg]]++;
beg++;
}
}
if (beg != -1)
{
for(int i = beg; i < end; i++)
count[a[i]]++;
}
}
return ret;
}
};