-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeywords.cpp
47 lines (41 loc) · 1.32 KB
/
Keywords.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
#define MAXVAL (int)1e9
int minimumLength(string text, vector < string > keys) {
int answer = MAXVAL;
text += " ";
int dp[text.length()] = {-1};
vector<pair<int, int>> loc;
string word = "";
for(int j = 0; j < text.size(); j++) {
if(text[j] == ' ') {
for(int k = 0; k < keys.size(); k++) {
if(keys[k] == word) {
dp[j-keys[k].length()] = k;
loc.push_back(make_pair(j-keys[k].length(), (int)keys[k].length()));
//dup.erase(dup.begin() + k);
break;
}
}
word = "";
}
else word += text[j];
}
for(int j = 0; j < loc.size(); ++j){
bool visited[keys.size()] = {false};
int count = 0;
string lastWord;
for(int i = j; i < loc.size(); ++i){
if(!visited[dp[loc[i].first]]){
visited[dp[loc[i].first]] = true;
++count;
lastWord = keys[dp[loc[i].first]];
}
if(count == keys.size()){
answer = min(loc[i].first - loc[j].first + loc[i].second, answer);
break;
}
}
}
if(answer == MAXVAL)
answer = -1;
return answer;
}