1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     string longestWord(vector<string>& words) 
12     {
13         unordered_set<string> siset(words.begin(),words.end());
14         string res;
15         for(string s:words)
16         {
17             if(s.length()>res.length()||(s.length()==res.length()&&s<res))
18             {
19                 if(judgein(siset,s))
20                     res=s;
21             }
22             else 
23                 continue;
24         }
25         return res;
26     }
27     
28     bool judgein(unordered_set<string> &siset,string s)
29     {
30         while(!s.empty())
31         {
32             if(siset.find(s)!=siset.end())
33                 s.pop_back();
34             else
35                 return false;
36         }
37         return true;
38     }
39 };

用一个set将原容器中的字符串扫描进去,然后顺序扫描字符串容器,只有当当前字符串长度长于res或者长度和res相等但是小于res时,才调用判定函数判定该字符串是否能由容器中的单词序列构成,若判定结果为真,则更新res。

posted on 2018-06-11 09:45  高数考了59  阅读(128)  评论(0)    收藏  举报