-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path524.cpp
More file actions
49 lines (43 loc) · 1.32 KB
/
524.cpp
File metadata and controls
49 lines (43 loc) · 1.32 KB
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
__________________________________________________________________________________________________
sample 24 ms submission
static auto x = [](){std::ios::sync_with_stdio(false);cin.tie(nullptr);return nullptr;}();
class Solution{
private:
bool match(const string& str,const string& dir){
int j=0;
for(int k=0;k<dir.size();k++){
while(j<str.size()&&dir[k]!=str[j]) j++;
if(j++==str.size()) return false;
}
return true;
}
public:
string findLongestWord(string s, vector<string>& d){
string ans="";
for(const auto& i: d)
if(i.size()>ans.size()||i.size()==ans.size()&&i<ans)
if(match(s,i)) ans=i;
return ans;
}
};
__________________________________________________________________________________________________
sample 16000 kb submission
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
string res;
for (string& str: d) {
int i = 0;
for (char c: s) {
if (i<str.size() && c==str[i])
++i;
}
if (i==str.size() && str.size()>=res.size()) {
if (str<res || str.size()>res.size())
res = str;
}
}
return res;
}
};
__________________________________________________________________________________________________