-
Notifications
You must be signed in to change notification settings - Fork 0
/
191119-1.cpp
82 lines (79 loc) · 1.87 KB
/
191119-1.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
// https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
#include <cstdio>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
if (words.empty()) return res;
int n = s.size();
int wordCount = words.size();
int wordSize = words[0].size();
unordered_map<string, int> wid; // word => id
unordered_map<int, int> wc; // id => count
for (int i = 0; i < words.size(); ++i) {
if (wid.find(words[i]) == wid.end()) {
wid[words[i]] = i;
}
wc[wid[words[i]]]++;
}
unordered_map<int, int> sid; // pos => id
for (int pos = 0; pos <= n - wordSize; ++pos) {
string w = s.substr(pos, wordSize);
auto it = wid.find(w);
if (it == wid.end()) {
sid[pos] = -1;
} else {
sid[pos] = it->second;
}
}
for (int offset = 0; offset < wordSize; ++offset) {
for (int pos = offset; pos < n; pos += wordSize) {
unordered_map<int, int> counter;
bool bad = false;
int i = 0;
for (; i < wordCount; ++i) {
int pos2 = pos + i * wordSize;
if (pos2 > n - wordSize) break;
int k = sid[pos2];
if (k == -1 || counter[k] >= wc[k]) {
bad = true;
break;
}
counter[k]++;
}
if (!bad && i == wordCount) {
res.push_back(pos);
}
}
}
return res;
}
};
void print(vector<int> a)
{
for (auto& e : a) {
printf("%d ", e);
}
printf("\n");
}
int main()
{
Solution s;
{
vector<string> words{"foo","bar"};
print(s.findSubstring("barfoothefoobarman", words)); // answer: [0, 9]
}
{
vector<string> words{"word","good","best","word"};
print(s.findSubstring("wordgoodgoodgoodbestword", words)); // answer: []
}
{
vector<string> words{"word","good","best","good"};
print(s.findSubstring("wordgoodgoodgoodbestword", words)); // answer: [8]
}
return 0;
}